我需要传入一个用户列表,然后返回一个包含名称、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: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
复制相似问题