2013-08-08 43 views
5

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?

Trả lời

2

Bạn có thể nhận được tất cả người dùng trên tên miền của bạn bằng cách sử dụng các mô-đun Active Directory:

import-module activedirectory 
get-ADuser -filter * | select name,SamAccountName | ConvertTo-CSV | ac "C:\yourCSVFile.csv" 

này sẽ cung cấp cho bạn một kết quả như thế này của tất cả người dùng của bạn.

"name","SamAccountName" 
"MATTHE_G","matthe_g" 
"PUTINE_I","putine_i" 
"COBB_C","cobb_c" 
"BULL_T","bull_t" 
"BAYOL_B","bayol_b" 
"CAPPON_P","CAPPON_P" 
.... 

Note: Bạn sẽ cần phải bật cửa sổ thư mục hoạt động đặc trưng để có thể sử dụng các mô-đun activedirectory. Điều này có thể được tìm thấy dưới tab 'Remote Sever Administration' trong các tính năng của windows.

Link: For Cmdlet's in the AD module

+0

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

+0

Vui mừng được giúp đỡ :) – Richard

6

Nếu bạn muốn gắn bó với cách tiếp cận DirectorySearcher, thay đổi này:

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 
    } 

vào đây:

$objCollection = $colResults | select -Expand Properties | 
    select @{n='Name';e={$_.name}}, @{n='SAMAccountName';e={$_.samaccountname}} 
+0

Hoạt động hoàn hảo. Cảm ơn bạn rất nhiều vì đã giúp đỡ của bạn! – Michael

+0

@AnsgarWiechers, có cách nào để chuyển đổi 'Giá trị' của thuộc tính từ' ResultPropertyValueCollection' thành chuỗi không, thay vì phải liệt kê từng thuộc tính riêng lẻ (như bạn đã làm)? – craig

Các vấn đề liên quan