基于用户名导出广告信息CSV输入CSV powershell脚本

基于用户名导出广告信息CSV输入CSV powershell脚本,powershell,csv,active-directory,Powershell,Csv,Active Directory,我需要传入一个用户列表,然后返回一个CSV,其中包含名称、SamAccountName和电子邮件 我的输入如下: "John Doe" "Jane Doe" 这是我目前使用的代码。我不确定是什么问题。用户确实存在于指定的“DC”下 Import-Module ActiveDirectory Function Get-ADUsersDetailsCSV { [CmdletBinding()] Param ( [Parameter(Mandatory=$True,P

我需要传入一个用户列表,然后返回一个CSV,其中包含名称、SamAccountName和电子邮件

我的输入如下:

"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

这不起作用的原因是
Get-ADUser
cmdlet使用的
-Identity
参数是在
SamAccount
属性上搜索AD,而不是在
Name
属性上搜索AD以检索用户。因此,搜索“John Doe”将不起作用,相反,它希望您使用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

如果运行Get-Help Get-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
还要注意,Name和SAMAccountName是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

Get-ADUser:在类型为“System.Management.Automation.PSCustomObject”的对象中找不到属性:“Name”。在U:\data\GetADUserInfo PS Script\GetADUsersDetailsCSV.psm1:19 char:28+$USERS | Foreach{Get ADUser-Filter{Name-eq$U.Name}-Properties*|…在这里我用一个用户运行它…Get ADUser-Filter{Name-eq“John Doe”}-Properties*| Select Name,SAMAccountName,mailI我知道这不在最初的要求中,但是如果找不到传入的名称,是否可以在一行中说该用户不存在?在答案中添加了一个更新(但尽量避免将来出现“范围爬行”)。这是有效的。我唯一的调整是“Name=$\uuux.Name”而不是“Name=$\”。谢谢!
$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