我需要传入用户列表并返回包含名称,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:dataGetADUserInfo PS ScriptGetADUsersDetailsCSV.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 两次,因为您可以使用 Get-ADUser 对象获取名称和电子邮件属性。
$user = Get-ADuser -Filer { Name -eq $Name }
此外,您还可以将不同的对象通过管道连接在一起。试试这个,看看你会得到什么
$ExportCSVPath = "C:Exportedcsv.csv"
$users = (Import-Csv -Path $CsvFilePath) |
% {Get-ADUser -Filter { Name -eq $_.DisplayName } |
Select -ExpandProperty SamAccountName, mail } |
Export-Csv $ExportCSVPath