基本上,我想要做的是使用PowerShell脚本从Active Directory中检索所有用户并将它们保存在一个.csv文件中。此外,我只想列出“名称”和“samaccountname”这两个属性。以下是代码:
$strFilter = "somefilter"
$objCollection = @()
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$colProplist = "name", "samaccountname"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults) {
$objItem = $objResult.Properties
$object = New-Object PSObject
$object | Add-Member -MemberType NoteProperty -Name Name -Value $objItem.name
$object | Add-Member -MemberType NoteProperty -Name SAMAccountname -Value $objItem.samaccountname
$objCollection+=$object
}
$objCollection # this gives me the output as wished
$objCollection | Export-CSV -NoTypeInformation -Path C:\temp\exportfile.csv # this doesn't work
控制台输出如下:
Name SAMAccountname
---- --------------
{IUSR_PFTT-DC1} {IUSR_PFTT-DC1}
{IUSR_PFVM-DC1} {IUSR_PFVM-DC1}
{IUSR_PFXX-DC1} {IUSR_PFXX-DC1}
但导出的 .csv 文件看起来像这样:
"Name","SAMAccountname"
"System.DirectoryServices.ResultPropertyValueCollection","System.DirectoryServices.ResultPropertyValueCollection"
"System.DirectoryServices.ResultPropertyValueCollection","System.DirectoryServices.ResultPropertyValueCollection"
"System.DirectoryServices.ResultPropertyValueCollection","System.DirectoryServices.ResultPropertyValueCollection"
"System.DirectoryServices.ResultPropertyValueCollection","System.DirectoryServices.ResultPropertyValueCollection"
有关这个问题,您有什么想法或解决方案吗?