Powershell compares user logon hours for AD and exchange

Source: Internet
Author: User
Tags email account

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.