2014-01-17 50 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