Por TheCleaner e MichelZ, modifiquei o script para paginar a consulta por intervalos de letras:
# Create credentials
$O365Creds = New-Object -Typename System.Management.Automation.PSCredential -ArgumentList "[email protected]",$SecurePassword
# Create session
$O365Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Creds -Authentication Basic -AllowRedirection
Import-PSSession $O365Session -AllowClobber
# Initiate file
$CSVExport = "o365_logons.csv"
If (Test-Path $CSVExport){
Remove-Item $CSVExport
}
"UserID,LastLogonTime" | Out-File -FilePath $CSVExport
# Loop through alphabet
foreach ($letter1 in [char]'a'..[char]'z') {
foreach ($letter2 in [char]'a'..[char]'z') {
$AccountNames = Get-Mailbox -Filter "{SamAccountName -like '$([char]$letter1)$([char]$letter2)*'}" -ResultSize Unlimited | Select -Expand Name
# Skip if no accounts
if (!$AccountNames) {
Continue
}
foreach ($account in $AccountNames) {
## Some last logon could be null, using ForEach as workaround
$last_logon = Get-MailboxStatistics -Identity $account | ForEach { $_.LastLogonTime }
## Print to CSV file
$account,$last_logon -Join ','| Out-File -Append -FilePath $CSVExport
}
}
}
Vai fazer um teste durante a noite.
Se alguém tiver alguma recomendação sobre como tornar isso mais eficiente ou elegante, comente.