首页 > 解决方案 > Powershell 脚本未从 Office 365 (MsolService) 获取所需的所有信息

问题描述

我正在尝试从我们的 Office 365 获取某些信息,但没有获得所需的所有信息。

下面是我使用的脚本:

Get-MsolUser -All | select DisplayName, LastPasswordChangeTimeStamp, LastLogonTime, PrimaryEmailAddress | Export-CSV UserList.csv -NoTypeInformation

我从上述脚本中获得的信息只是显示名称最后一次密码更改。对于LastLogonTimePrimaryEmailAddress我一无所获。

有什么我做错了吗?

请帮忙。

谢谢

标签: powershelloffice365

解决方案


上次登录时间可以从 Get-MailboxStatistics 中检索,但它仅显示上次访问的 Exchange 邮箱。它不跟踪其他 Office 365 服务。您可以根据您的要求尝试以下代码。

$Result=""  
$Output=@()
Get-mailbox -All | foreach{
$UPN=$_.UserPrincipalName
$DisplayName=$_.DisplayName
$PrimaryEmailAddress=$_.ProxyAddresses.where{$_ -clike "SMTP:*"} -creplace "SMTP:"
$LastPwdChange=$_.LastPasswordChangeTimeStamp
$LastLogonTime=(Get-MailboxStatistics -Identity $upn).lastlogontime
$Result= @{'DisplayNme'=$DisplayName;'LastLogonTime'=$LastLogonTime;'PrimaryEmailAddress'=$PrimaryEmailAddress;'LastPwdChange'=$LastPwdChange}
$Output= New-Object PSObject -Property $Result 
$Output | Select-Object DisplayName,LastLogonTime,PrimaryEmailAddress,LastPwdChange | Export-CSV UserList.csv -Notype -Append
}

脚本来源:将 Office 365 用户上次登录时间导出到 CSV


推荐阅读