Về cơ bản, những gì tôi đang cố gắng làm là lấy tất cả người dùng từ Active Directory và lưu chúng trong tệp .csv, sử dụng tập lệnh PowerShell. Ngoài ra, tôi chỉ muốn các thuộc tính "name" và "samaccountname" được liệt kê. Vì vậy, đây là các mã:Xuất một mảng với các đối tượng tùy chỉnh
$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
Console Output trông như thế này:
Name SAMAccountname
---- --------------
{IUSR_PFTT-DC1} {IUSR_PFTT-DC1}
{IUSR_PFVM-DC1} {IUSR_PFVM-DC1}
{IUSR_PFXX-DC1} {IUSR_PFXX-DC1}
Nhưng .csv xuất khẩu trông như thế này:
"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"
Bất kỳ ý tưởng/giải pháp này?
Tôi không thể tin rằng tôi đã không tình cờ gặp phải điều này, trong khi tìm kiếm trên Internet một giải pháp ... Cảm ơn rất nhiều. – Michael
Vui mừng được giúp đỡ :) – Richard