1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| # 导入AD模块
$s= New-PSSession -ComputerName "syddc01"
Invoke-Command -Session $s {Import-Module activedirectory}
Import-PSSession -Session $s -Module activedirectory
#导入MSOnline模块
$cred = Get-Credential "yli@syd.ddb.com"
Import-Module MSOnline
Set-ExecutionPolicy remotesigned
Connect-MsolService -Credential $cred
#连接到Office365
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
Import-PSSession $session
$when1=((get-date).AddDays(-90)).Date
$when2=((get-date).AddDays(-30)).Date
#获取90天内未登陆的账号信息
$users=get-aduser -Filter {(lastlogontimestamp -like '*') -and (emailaddress -like '*')} -Properties * | select name, lastlogontimestamp, @{n="Logon";e={[datetime]::FromFileTime($_.lastlogontimestamp.tostring())}} | Where-Object {$_.Logon -lt $when1}
#获取其中30天未登录邮箱的账号信息
foreach($user in $users){
if($a=Get-Mailbox $user.Name -ErrorAction SilentlyContinue ){
$a | get-mailboxstatistics | Where-Object { $_.lastlogontime -lt $when2} | select displayname,lastlogontime
}
}
|