rather new to PowerShell and Office365 cmdlets.
Currently I've created a script which gets all the e-mail addresses from my Office365 service (around 140,000). After it has retrieved all the emails using get-mailbox cmdlet I use the get-mailboxStatistics cmdlet to get the Last-Logon-Date. It outputs all of this to a CSV file so I end up with an e-mail address that users last-logon-date.
The script is currently running as intended but a PowerShell Credential Request will randomly prompt after the script has been running. From what I've seen it will prompt completely randomly, sometimes it takes hours to prompt and other times it takes only 10 minutes. The script is meant to run about once a year which will be scheduled, I don't want to have to manually run it and re-enter in my details every time.
I've looked around and can't find anything to solve my problem. Office 365 Login Code below.
$password = ConvertTo-SecureString -String $arg -AsPlainText -Force
$msolcred = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $username, $password
connect-msolservice -credential $msolcred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $msolcred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Notes: -I'm pretty sure there is no loss of connection on my end.
-The script will most likely take around 10 hours in total to run.
If anyone needs further details feel free to ask.
One approach you can use is to reconnect after X number of records have been processed. In the code below a new connection will be made after 1000 records have been processed.
# Save credential to a file
Get-Credential | Export-Clixml credentialFile.xml
# Load credential
$credential = Import-Clixml credentialFile.xml
# Set reconnect threshold
$reconnectThreshold = 1000
foreach($element in $array)
{
# Reconnect if threshold is reached
if($processedCount -ge $reconnectThreshold)
{
# Close all sessions
get-pssession | Remove-PSSession -Confirm:$false
# Start a new session
Import-PSSession $(New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $credential -Authentication Basic -AllowRedirection)
# Reset processed counter
$processedCount = 0
}
# Action on an item
Action-Item $element
# Increment processed counter
$processedCount++
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句