代码之家  ›  专栏  ›  技术社区  ›  Gowtham Ramamoorthy

如何从Active Directory获取组名和EmployeeID?

  •  0
  • Gowtham Ramamoorthy  · 技术社区  · 6 年前

    我有这个代码,我试图获得所有的用户详细信息在一个活动目录中存在。

    $path = "C:\ServerDetails"
    $LogDate = get-date -f yyyyMMddhhmm
    $csvfile = $path + "\ALLADUsers_$logDate.csv"
    
    Import-Module ActiveDirectory
    
    $ADServer = 'xx.xx.x.x'
    
    $username = "abc"
    $password = "alpha"
    $secstr = New-Object -TypeName System.Security.SecureString
    $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
    
    Get-ADUser -server $ADServer -Credential $cred -Properties msDS-UserPasswordExpiryTimeComputed*  -Filter * | 
    
    Select-Object @{Label = "First Name";Expression = {$_.GivenName}},
    @{Label = "Last Name";Expression = {$_.Surname}},
    @{Label = "Display Name";Expression = {$_.DisplayName}},
    
    @{Label = "EmployeeID";e={$_.employeeID}},
    @{Label = 'GroupName';e={($_.memberof | %{(Get-ADPrincipalGroupMembership $_).sAMAccountName}) -join ";"}},
    
    @{Label = 'Description';e={$_.Description}},
    @{Label = 'PasswordExpired';e={if($_.PasswordExpired){$true} else{$false}}},
    @{Label = "PasswordExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}},
    @{Label = "Logon Name";Expression = {$_.sAMAccountName}},
    @{Label = "Phone";Expression = {"Ext - $(-Join $_.TelephoneNumber[-4..-1])"}},
    @{Label = "Email";Expression = {$_.Mail}},
    @{Label = "Account Status";Expression = {if (($_.Enabled -eq 'TRUE')  ) {'Enabled'} Else {'Disabled'}}}, # the 'if statement# replaces $_.Enabled
    @{Label = "Last LogOn Date";e={[datetime]::FromFileTime($_.lastLogonTimestamp)}}| 
    
    #Export CSV report
    
    Export-Csv -Path $csvfile -NoTypeInformation
    

    除了用户所在的组名和employeeID号之外,其他所有细节都非常好。

    非常感谢您的帮助。

    1 回复  |  直到 6 年前
        1
  •  1
  •   henrycarteruk    6 年前

    你要的是这个。它将搜索用户的组,提取名称并根据需要加入它们。

    {($_  | %{(Get-ADPrincipalGroupMembership $_.SamAccountName).Name -join ";"})}
    

    你所拥有的就是这个

    # $_.memberof is using the full name of groups the user is in
    # the groups do not have a .SamAccountName for this type
    {($_.memberof | %{(Get-ADPrincipalGroupMembership $_).sAMAccountName}) -join ";"}
    

    至于雇员ID,你能确认 .EmployeeID extensionAttribute