因此,我试图将计算机列表从 AD 提取到 .csv 文件中。我正在尝试使用 powershell 提取 cn、description、distinguishedName、operatingSystem、whenCreated 和 whenChanged 字段。
我正在使用的代码是这样的:
# Start of script
Cls
$DomainRootPath='LDAP://DC=<name>,DC=ORG'
$adsearch = New-Object DirectoryServices.DirectorySearcher([adsi]$DomainRootPath)
$adsearch.PageSize = 1000
$adsearch.filter = ("(objectCategory=Computer)")
$adsearch.PropertiesToLoad.AddRange(@("cn"))
$adsearch.PropertiesToLoad.AddRange(@("description"))
$adsearch.PropertiesToLoad.AddRange(@("distinguishedName"))
$adsearch.PropertiesToLoad.AddRange(@("operatingSystem"))
$adsearch.PropertiesToLoad.AddRange(@("whenCreated"))
$adsearch.PropertiesToLoad.AddRange(@("whenChanged"))
$computers = $adsearch.findall()
$computers.Count
$report = @()
foreach ($objResult in $computers)
{
$objItem = $objResult.Properties
$temp = New-Object PSObject
$temp | Add-Member NoteProperty cn $($objitem.cn)
$temp | Add-Member NoteProperty description $($objitem.description)
$temp | Add-Member NoteProperty distinguishedName $($objitem.distinguishedName)
$temp | Add-Member NoteProperty operatingSystem $($objitem.operatingSystem)
$temp | Add-Member NoteProperty whenCreated $($objitem.whenCreated)
$temp | Add-Member NoteProperty whenChanged $($objitem.whenChanged)
$report += $temp
}
$csvfile="AD-All-Computers.csv"
$report | export-csv -notypeinformation $csvfile
"Wrote file for All Computers"
现在,此代码确实使用列名的正确字段创建 .csv。但是它只提取目录中每台计算机的 cn 和描述。填充 .csv 时,所有其他字段均为空白
不知道代码中出了什么问题,因为我没有收到任何错误,非常感谢任何帮助。
附带说明一下,我正在运行 Windows 2008 R2 服务器并且无法在我的环境中运行 powershell 2.0 cmdlet,因为某些系统阻止我们能够配置 AD 来执行此操作。