生活随笔
收集整理的這篇文章主要介紹了
Powershell 比较AD和Exchange的用户登录时间
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
今天早上朋友問我如何獲取指定某個時間段的AD用戶登錄時間和相關(guān)的郵箱登錄時間。
豆子公司的Exchange已經(jīng)轉(zhuǎn)移到Office365上,首先遠(yuǎn)程的導(dǎo)入AD模塊,本地導(dǎo)入MSOnline的模塊,這樣我就可以遠(yuǎn)程訪問AD和Office365了
下面是一個例子,判斷90天沒有登錄的AD賬號,并把其中30天沒有登陸郵箱的賬號都找出來
#?導(dǎo)入AD模塊
$s=?New-PSSession?-ComputerName?"syddc01"
Invoke-Command?-Session?$s?{Import-Module?activedirectory}
Import-PSSession?-Session?$s?-Module?activedirectory?#導(dǎo)入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天內(nèi)未登陸的賬號信息
$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?
}
}
結(jié)果如下
這只是一個測試的腳本,驗(yàn)證一下思路,并不完善,比如信息的過濾可以考慮用filter 而不是where-object;缺少異常處理,用戶登錄賬號加密,后期自動發(fā)送結(jié)果給管理員,缺少參數(shù)化等等;如果需要的話可以慢慢進(jìn)行優(yōu)化處理。
總結(jié)
以上是生活随笔為你收集整理的Powershell 比较AD和Exchange的用户登录时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。