我需要在用戶的列表,通過並取回與名稱,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