2014-01-17 83 views
0

我需要在用戶的列表,通過並取回與名稱,SAM帳戶名,電子郵件基於用戶名輸入CSV PowerShell腳本導出廣告信息CSV

我輸入CSV是如下一個CSV:

"John Doe" 
"Jane Doe" 

下面是我正在使用的當前代碼。我不確定問題是什麼。用戶實際做下的「DC」指定的存在......

Import-Module ActiveDirectory 
Function Get-ADUsersDetailsCSV 
{ 
    [CmdletBinding()] 
    Param 
    (
    [Parameter(Mandatory=$True,Position=1)] 
    [String]$InCSV, 

    [Parameter(Mandatory=$True)] 
    [String]$OutCSV 
    ) 

If($InCSV) 
{ 
    If(Test-Path -Path $InCSV) 
    { 
     $USERS = Import-CSV $InCSV -Header Name 
     $USERS|Foreach{Get-ADUser $_.Name -Properties * |Select Name, SAMAccountName, mail}|Export-CSV -Path $OutCSV 

    } #End Test that the files exist 

    Else 
    { 
     Write-Warning "Cannot find path '$InCSV' because it does not exist." 
    } 


} #End Ensure Input and Output files were provided 

} #End Function Get-UsersDetailsCSV 

這裏的錯誤:

Get-ADUser : Cannot find an object with identity: 'John Doe' under: 'DC=blah,DC=com'. 
At U:\data\GetADUserInfo PS Script\GetADUsersDetailsCSV.psm1:19 char:28 
+    $USERS|Foreach{Get-ADUser $_.Name -Properties * |Select Name, SAMAcc ... 
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   : ObjectNotFound: (Name:ADUser) [Get-ADUser], ADIdentityNotFoundException 
+ FullyQualifiedErrorId : Cannot find an object with identity: 'John Doe' under: 'DC=blah,DC=com'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser 

回答

1

如果您運行獲取幫助獲取-ADUser便有,你會發現這說明Identity參數:

-Identity <ADUser> 
     Specifies an Active Directory user object by providing one of the following property values. The identifier in parentheses is the LDAP display name for the attribute. 

     Distinguished Name 
     Example: CN=SaraDavis,CN=Europe,CN=Users,DC=corp,DC=contoso,DC=com 
     GUID (objectGUID) 
     Example: 599c3d2e-f72d-4d20-8a88-030d99495f20 
     Security Identifier (objectSid) 
     Example: S-1-5-21-3165297888-301567370-576410423-1103 
     SAM account name (sAMAccountName) 
     Example: saradavis 

請注意,名稱不是它將接受的身份之一。名稱不是AD中的索引屬性,因爲它不保證是唯一的。它可能在你的域名中,但AD不知道。要通過任何其他屬性得到一個用戶,你需要使用一個過濾器,讓你的腳本會是這個樣子(我把摺疊的可讀性的自由)

$USERS | Foreach{ 
Get-ADUser -filter "Name -eq '$($_.name)'" -Properties mail | 
Select Name, SAMAccountName, mail}| 
Export-CSV -Path $OutCSV 

還要注意的是名稱和SAM帳戶名是中Get-ADUser將返回的常見屬性,所以您必須指定的唯一其他屬性是Mail。

我認爲這將採取的要求,產生額外的照顧,但我沒有測試它:

$USERS | Foreach{ 
    $getuser = 
    Get-ADUser -filter "Name -eq '$($_.name)'" -Properties mail | 
    Select Name, SAMAccountName, mail 

    if ($getuser) {$getuser} 
    else {[PSCustomObject]@{Name=$_;SAMAccountName='Not found';mail=$null}} 
} | 
Export-CSV -Path $OutCSV 
+0

我知道這不是在原來的要求,但是是否有可能放入一行說,如果用戶沒有找到,那麼用戶不存在傳入的名稱? – trueimage

+0

添加了對答案的更新(但試圖避免將來出現「範圍蔓延」)。 – mjolinor

+0

這是有效的。我唯一的調整是「名稱= $ _。名稱」,而不是「名稱= $ _」。謝謝! – trueimage

1

這是不工作的原因是,該Get-ADUser cmdlet使用-Identity參數搜索AD上的SamAccount屬性,而不是Name屬性來檢索用戶。因此,搜索「張三」是行不通的,而是期待您與SamAccount名稱搜索:「JDOE」

按名稱搜索,你必須過濾結果的名稱是這樣的:

Get-ADUser -Filter {Name -eq "John Doe"} 

因此,你的代碼就變成了:

$USERS|Foreach{Get-ADUser -Filter {Name -eq $_.Name} -Properties * |Select Name, SAMAccountName, mail}|Export-CSV -Path $OutCSV 
+0

GET-ADUser便有:房產:「名稱」型的對象未找到:「System.Management。 Automation.PSCustomObject」。 在U:\ data \ GetADUserInfo PS Script \ GetADUsersDetailsCSV.psm1:19 char:28 + $ USERS | Foreach {Get-ADUser -Filter {Name -eq $ _。Name} -Properties * | ... – trueimage

+0

我在哪裏運行它與一個用戶它的工作原理... Get-ADUser -Filter {名稱 - 當然「John Doe」} - 屬性* |選擇名稱,SAMAccountName,郵件 – trueimage