最近有人和我反应,无法查看所有员工的Office365的密码过期时间。最好可以批量查看,如果能在过期前几天发个提醒邮件就更好了。
$pw = ConvertTo-SecureString 'your_pwd' -AsPlainText -Force $creds = New-Object -Typename System.Management.Automation.PSCredential -Argumentlist 'your_id@company.cn', $pw $mailpw = ConvertTo-SecureString 'your_pwd' -AsPlainText -Force $mailcreds = New-Object -Typename System.Management.Automation.PSCredential -Argumentlist 'your_mail', $mailpw Connect-MsolService -Credential $creds $now = Get-Date #get-msoluser | select UserPrincipalName, @{name='lastpasswordchangetimestamp';expression={$_.lastpasswordchangetimestamp.tostring().split(' ')[0]}} | Export-Csv -Path f:lastpasswordchangetimestamp.csv -Encoding Default $smtpserver = "your_smtp_server" $sender = "your_mail" $cc = "servicedesk@company.cn" $body = ‘ <html><body> <h1>提醒</h1> 您的Office 365账号密码即将过期,逾期不修改密码您将无法继续使用邮件功能。关于如何修改Office 365账号密码,请访问<a href="https://company.sharepoint.cn/IT/IT1/Office 365/如何更改office 365密码.docx?d=w6b52d070c5db4d518e76e726fffafbdc">这里</a>。 </body></html> ’ Get-MsolUser | select UserPrincipalName, @{name='delta';expression={($now - $_.lastpasswordchangetimestamp).days}} | foreach { if ($_.delta -ge 355) { $to = $_.UserPrincipalName $subject = "您的Office 365账号密码在" + (365 - $_.delta) + "天后即将过期,请及时修改!" Send-MailMessage -SmtpServer $smtpserver -Credential $mailcreds -From $sender -To $to -Cc $cc -Subject $subject -Body $body -BodyAsHtml -Encoding default Write-Host $to, "mail sent!" } }