我试图从Office 365获取邮箱统计信息.这是当前脚本: # Get credentials$O365Creds = New-Object -Typename System.Management.Automation.PSCredential -ArgumentList "reports@o365.example.com",$SecurePassword# Create session$O365S
# Get credentials $O365Creds = New-Object -Typename System.Management.Automation.PSCredential -ArgumentList "reports@o365.example.com",$SecurePassword # Create session $O365Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionURI https://ps.outlook.com/powershell -Credential $O365Creds -Authentication Basic -AllowRedirection Import-PSSession $O365Session -AllowClobber # Create report Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | FT @{n="UserID";e={(Get-Mailbox $_.LegacyDN).Name}},LastLogonTime | Out-File -FilePath o365_logons.csv -Encoding utf8 -append
查看内存使用情况,看起来像Get-Mailbox -ResultSize Unlimited在下载前已加载到内存中;超过1GB的内存使用量.大部分时间它只是超时.这非常低效,因为我只对两列感兴趣.
任何人都有关于如何以更有效的方式完成此任务的任何建议?
根据TheCleaner和MichelZ,我修改了脚本以按字母范围分页查询:# Create credentials $O365Creds = New-Object -Typename System.Management.Automation.PSCredential -ArgumentList "reports@o365.example.com",$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 } } }
将进行一次试运行一夜.
如果有人对如何提高效率或优雅有任何建议,请发表评论.