简体   繁体   English

如何创建Powershell脚本来提取活动目录用户名,帐户用户名和lastlogondate?

[英]How do I create a powershell script to pull active directory usernames, account they are a member of and lastlogondate?

This is what I have so far but I'm having no luck. 这是我到目前为止所拥有的,但是我没有运气。 What do you all think? 你们怎么想

Get-ADUser -LDAPFilter "(name=*)" -SearchScope Subtree `
  -SearchBase "OU=adminaccounts,OU=Managed Objects,DC=testdomain,DC=Com" ` |
  % {
    $user = $_
    $user | Get-ADPrincipalGroupMembership | 
    Select @{N="User";E={$user.sAMAccountName}},
           @{N="Group";E={$_.Name}},
           @{N="Last‌​Logon";E={[DateTime]::FromFileTime($user.LastLogon)}}
  } |
  Select User,Group,LastLogon |
  Export-Csv C:\temp\report.csv -nti

LastLogon is not among the properties that Get-ADUser returns by default, unlike Name and SamAccountName . NameSamAccountName不同, LastLogon不在默认情况下Get-ADUser返回的属性中。 You need to explicitly tell the cmdlet to include that property: 您需要明确告诉cmdlet包括该属性:

Get-ADUser -LDAPFilter "(name=*)" -SearchScope Subtree `
  -SearchBase "OU=adminaccounts,OU=Managed Objects,DC=testdomain,DC=Com" `
  -Properties LastLogon | ...

Also replace $_.LastLogon with $user.LastLogon inside the first select statement, because the current object variable ( $_ ) in the nested pipeline has a different value than you expect (the principal group of the user): 还要在第一条select语句内用$user.LastLogon替换$_.LastLogon ,因为嵌套管道中的当前对象变量( $_ )的值与您期望的值(用户的主体组)不同:

... | select ...,@{N="Last‌​Logon";E={[DateTime]::FromFileTime($user.LastLogon)}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM