关于活动目录:使用PowerShell列出OU中所有计算机的信息

Listing information for all computers in an OU using PowerShell

对于在PowerShell上运行的实验室,我必须定位特定的OU,并在文本文件中列出以下信息。

1
2
3
4
5
6
7
8
9
DistinguishedName
DNSHostName
Enabled
Name
ObjectClass
ObjectGUID
SamAccountName
SID
UserPrincipleName

无论如何格式化,我都在网上找到了大量资源,并且不断发现错误。

这是我的代码:

1
2
3
4
5
$ou = 'OU=Testing,OU=Labs,OU=UWEC Computers DC=uwec, DC=edu'
$Computers = Get-ADComputer -Filter '*' -SearchBase $ou
$Computers | foreach {
    $_.DNSHostName
} | Out-File -Filepath"C:\\Windows\\Temp\\Lab7.txt"

无论使用哪种语法,我都会不断收到此错误:

1
2
3
4
5
6
Get-ADComputer : The object name has bad syntax
At line:1 char:1
+ Get-ADComputer -Filter '*' -SearchBase 'OU=Testing,OU=Labs,OU=UWEC Co ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ADComputer], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

您发布的代码与您发布的错误不匹配。 但是,最可能导致该错误的原因是您的OU中缺少逗号:

1
2
$ou = 'OU=Testing,OU=Labs,OU=UWEC Computers DC=uwec, DC=edu'
#                                          ^right here

改成

1
$ou = 'OU=Testing,OU=Labs,OU=UWEC Computers,DC=uwec, DC=edu'

问题应该消失了。