我需要創建一個Powershell對象,數組或哈希表來存儲用戶列表和各種細節,這些都是從CSV文件中提取的,並使用Get-ADUser進行定位。如下所示:從包含多個用戶的循環創建Powershell對象
$userList = Import-CSV $CSVInputFile
$users = @{}
Foreach ($csvUser in $userList)
{
$userSearchString = $csvUser | Select -ExpandProperty SamAccountName
$currentUser = (Get-ADUser -Filter {SamAccountName -eq $userSearchString} `
-Properties PasswordExpired,PasswordLastSet,EmailAddress |
Where {$_.Enabled -eq "True"})
If ($currentUser.EmailAddress -ne $null)
{
$currentUserEmailString = $csvUser | Select -ExpandProperty EmailAddress
$currentUserEmailString = ($currentUserEmailString -as [string])
$currentUser.EmailAddress = $currentUserEmailString
}
$Users = New-Object PSObject -Property @{
DistinguishedName = $currentUser.DistinguishedName
EmailAddress = $currentUser.EmailAddress
Enabled = $currentUser.Enabled
GivenName = $currentUser.GivenName
Name = $currentUser.Name
PasswordExpired = $currentUser.PasswordExpired
PasswordLastSet = $currentUser.PasswordLastSet
SamAccountName = $currentUser.SamAccountName
Surname = $currentUser.Surname
}
$Users
}
如何將每個用戶的詳細信息添加到循環的每個迭代到對象中。
我想直接從GET-ADUser便有包含用戶數,相同輸出的細節的目的是結束:
Name SamAccountName EmailAddress
---- -------------- ------------
User1 user1 [email protected]
User2 user2 [email protected]
User3 user3 [email protected]
任何幫助將不勝感激!
太簡單了,爲什麼我不這麼想! 工程處理,感謝您的幫助:) – seirved 2014-10-27 03:10:25