2014-10-27 134 views
0

我需要創建一個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] 

任何幫助將不勝感激!

回答

2

不知道如果我錯過了這個點,但我看到你正在建設一個自定義對象就在你的循環。我所看到的唯一問題是你沒有在每個循環之後保留結果。而是你正在摧毀對象的歷史。

我會將$users的聲明更改爲數組$users = @(),而不是將用戶散列表填充到用戶中,將當前對象添加到數組中。然後,您將有哈希表的數組:

$Users += New-Object PSObject -Property @{... 

然後你可以在循環外$Users輸出線,你將有整個事情。然後,您可以輸出到Select以獲得您想要的輸出。

$Users | Select-Object name,SamAccountName,EmailAddress 

雖然這種方法存在潛在的主要缺陷。在數組上使用+=時,會爲新元素創建新數組並重新調整大小,並丟棄舊數組。這對於較大的陣列具有巨大的性能影響。


解決這個問題的更好方法是利用管道。當你有更大的用戶組時,這將會提升性能。現在

Import-CSV $CSVInputFile | ForEach-Object{ 
    $userSearchString = $_.SamAccountName 
    $currentUser = Get-ADUser -Filter {SamAccountName -eq $userSearchString} ` 
     -Properties PasswordExpired,PasswordLastSet,EmailAddress | 
      Where {$_.Enabled -eq "True"} 

    If ($currentUser.EmailAddress -ne $null){ 
     $currentUser.EmailAddress = $_.EmailAddress 
    } 

    [pscustomobject][ordered]@{ 
     DistinguishedName = $currentUser.DistinguishedName 
     # ..... truncated 
     Surname = $currentUser.Surname 
    } 
} 

,你可以發送到像Export-CSV或只是將它保存到一個變量。您的選擇現已打開。 [pscustomobject][ordered]是PowerShell v3.0中可用的類型加速器+

+0

太簡單了,爲什麼我不這麼想! 工程處理,感謝您的幫助:) – seirved 2014-10-27 03:10:25

1

定義的$用戶爲陣列

$users = @() 

和新物體追加到$用戶。

$Users += New-Object 
+0

我在你面前這麼接近 – Matt 2014-10-27 03:00:16

+0

我在答覆中只寫了4行。我想你打敗了我。 – 2014-10-27 05:04:42

0

不能相信你們兩個都加入了我之前!好吧。 希望這有助於反正。

$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 
} 

#clears the properties of the previous object and starts collecting properties 
$UserObj = New-Object PSObject 
    Add-Member -InputObject $UserObj -MemberType NoteProperty -Name "DistinguishedName" -Value $($currentUser.DistinguishedName) 
    Add-Member -InputObject $UserObj -MemberType NoteProperty -Name "EmailAddress" -Value $($currentUser.EmailAddress)  
    Add-Member -InputObject $UserObj -MemberType NoteProperty -Name "Enabled" -Value $($currentUser.Enabled) 
    Add-Member -InputObject $UserObj -MemberType NoteProperty -Name "GivenName" -Value $($currentUser.GivenName)  
    Add-Member -InputObject $UserObj -MemberType NoteProperty -Name "UserName" -Value $($currentUser.Name) 
    Add-Member -InputObject $UserObj -MemberType NoteProperty -Name "PasswordExpired" -Value $($currentUser.PasswordExpired) 
    Add-Member -InputObject $UserObj -MemberType NoteProperty -Name "PasswordLastSet" -Value $($currentUser.PasswordLastSet) 
    Add-Member -InputObject $UserObj -MemberType NoteProperty -Name "SamAccountName" -Value $($currentUser.SamAccountName) 
    Add-Member -InputObject $UserObj -MemberType NoteProperty -Name "Surname" -Value $($currentUser.Surname) 

#saves the properties in an array that exists outside of the loop to preserve information beyond one interation 
$users += $UserObj 
} 

$users | Format-Table -Property UserName,SamAccountName,EmailAddress