我需要传入一个用户列表,然后返回一个包含名称、SamAccountName、电子邮件地址的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发布于 2014-01-17 08:49:46
如果运行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请注意,名称不是它将接受的身份之一。名称在AD中不是索引属性,因为它不能保证是唯一的。它可能在您的域中,但AD不知道这一点。要根据任何其他属性获取用户,您需要使用过滤器,以便您的脚本看起来像这样(我随意折叠以提高可读性)
$USERS | Foreach{
Get-ADUser -filter "Name -eq '$($_.name)'" -Properties mail |
Select Name, SAMAccountName, mail}|
Export-CSV -Path $OutCSV还请注意,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发布于 2014-01-17 08:48:06
这不起作用的原因是,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 $OutCSVhttps://stackoverflow.com/questions/21175652
复制相似问题