无法在Get-ADUser命令的输出中获取描述

时间:2017-07-14 02:21:27

标签: powershell powershell-v4.0

我正在尝试以下脚本,但我没有在输出中获得AD用户的描述。我只在输出中获取用户ID。能不能让我知道我在下面的代码中做错了什么?如何在输出中获得描述。

CLS
[INT]$NumberOfUsers=0
$ListOfUsers=@()

$TotalListOfUsers = get-aduser -SearchBase "OU=Users,OU= Accounts,DC=ABC,DC=XYZ,DC=local" -filter * | sort-object | Select Name,Description
foreach ($User in $TotalListOfUsers) {
if ($User -like "*Nikhil*") {
}
else {
$NumberOfUsers = $NumberOfUsers+1
$ListOfUsers = $ListOfUsers + $($User).Name + $($User).Description + "`r`n"
}
}

write-host "The total Number of users is $NumberOfUSers" 
write-host "$ListOfUsers"
#exit

if ($NumberOfUsers -gt 200) {

write-host "The total number of  user accounts is $NumberOfUsers"
}
Else {
write-host "Less than 200"
}

我输出的上述代码如下:

Account1 
Account2
Account3

我希望输出如下:

Account1   Description1
Account2   Description2
Account3   Description3

2 个答案:

答案 0 :(得分:0)

试试这个:

Get-AdUser -Identity $user -Properties Description | Select-Object -ExpandProperty Description

在你的情况下,它应该是:

Get-AdUser -SearchBase "OU=Users,OU= Accounts,DC=ABC,DC=XYZ,DC=local" -filter * -Properties Description | Select-Object -ExpandProperty Description

答案 1 :(得分:-1)

这可以很容易地纠正并简化。

    $ListOfUsers = Get-ADUser -SearchBase "OU=Users,OU= Accounts,DC=ABC,DC=XYZ,DC=local" -Filter 'Name -notlike "*Nikhil*"' -Properties Description | select Name, Description | Sort-Object

    write-host "The total Number of users is $($ListOfUSers.Count)"
    write-host "$ListOfUsers"
    #exit

    # Not sure why/if you need both this and the previous count.
    if ($ListOfUsers.Count -gt 200) {

    write-host "The total number of  user accounts is $($ListOfUsers.Count)"
    }
    Else {
    write-host "Less than 200"
    }