我需要在用户的列表,通过并取回与名称,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
我知道这不是在原来的要求,但是是否有可能放入一行说,如果用户没有找到,那么用户不存在传入的名称? – trueimage
添加了对答案的更新(但试图避免将来出现“范围蔓延”)。 – mjolinor
这是有效的。我唯一的调整是“名称= $ _。名称”,而不是“名称= $ _”。谢谢! – trueimage