My friend asked me this morning how to get AD user logon time and associated mailbox logon time for a specified period of time.
Beans Company Exchange has been transferred to OFFICE365, the first remote import AD module, local import msonline module, so I can remotely access the AD and Office365
The following is an example of the 90-day non-login ad account, and the 30 days without a login email account to find out
# Import AD Module $s= new-pssession -computername "SYDDC01" invoke-command -session $s {Import-Module activedirectory}Import-PSSession -Session $s -Module activedirectory #导入MSOnline模块 $cred = Get-Credential "[email protected]" Import-module MSOnlineSet-ExecutionPolicy remotesignedConnect-MsolService -Credential $cred # Connect to office365$session = new-pssession -configurationname microsoft.exchange - connectionuri https://outlook.office365.com/powershell/ -credential $Cred -authentication Basic -AllowRedirectionImport-PSSession $session $when1= (get-date). AddDays (-90)). Date$when2= ((get-date). AddDays (-30)). date# Get account information not logged in within 90 days $users=get-aduser -filter {(lastlogontimestamp -like ' * ') -and (emailaddress -like ' * ')} -properties * | select name, Lastlogontimestamp, @{n= "Logon";E={[datetime]::fromfiletime ($_.lastlogontimestamp.tostring ())}} | where-object {$_. logon -lt $when 1} #获取其中30天未登录邮箱的账号信息foreach ($user in $users) {if ($a =get-mailbox $user. name -erroraction silentlycontinue ) {$a | get-mailboxstatistics | where-object { $_.lastlogontime -lt $when 2} | select displayname, lastlogontime }}
The results are as follows
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/74/A1/wKiom1YkfLHxU10eAADQ_BC3IbQ335.jpg "title=" Capture.png "alt=" Wkiom1ykflhxu10eaadq_bc3ibq335.jpg "/>
This is just a test of the script, to verify the idea, not perfect, such as the filtering of information can be considered with filter instead of where-object, the lack of exception handling, user login account encryption, the latter automatically send results to the administrator, lack of parameterization and so on, if necessary, you can slowly optimize processing.
This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1704152
Powershell compares user logon hours for AD and exchange