Reputation: 1996
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.
Upvotes: 3
Views: 9539
Reputation: 11
$Username = "[email protected]"
$password= "xxx@12345"
$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,
$secureStringPwd
Upvotes: 1
Reputation: 341
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++
}
Upvotes: 2
Reputation: 47862
If you are scheduling this by calling powershell.exe, use the -NonInteractive
switch. This will throw an error instead of prompting, but that's something you can presumably Catch
and deal with programmatically.
Also see my answer here: https://stackoverflow.com/a/25172234/3905079
Upvotes: 2