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.