如何将多个用户或值添加到数组

我想创build一个PowerShell脚本,我想添加多个值到数组(添加到数组)。

例如:我有10个用户在我的公司,并希望创build他们的AD帐户(不使用CSV文件),如果我使用“读主机”如下。 我可以循环“读主机”显示10次,并将值添加到数组和列表如下?

$FirstName = Read-Host "Enter the first name of the employee" $LastName = Read-Host "Enter the last name of the employee" [INT]$empid = Read-Host "Enter the employee number" $group = Read-Host "Enter the group name" $homedrive = Read-Host "Enter the home drive" $NewHire = @{} $NewHire.Name = $FirstName $NewHire.Empid = $empid $NewHire.LastName = $LastName $NewHire.homedrive = $homedrive $Objectname = New-Object PSobject -Property $NewHire $objectname 

这个文件的输出如下,

 姓名Empid homedrive
 ---- -------- ----- ---------
 phani ukkalam 3333 FDS 

你可以添加一个wscript shell来提示用户是否继续,并把它放在do while循环中。

这里的循环条件取决于用户按是来添加更多的用户:

 $wsh = new-object -comobject wscript.shell do { $FirstName = Read-Host "Employee name" #and so on, and so on, end with this: $answer = $wsh.popup("Do you want to add more users?", 0,"More users?",4) If ($answer -eq 6) { $continue = $True } else { $continue = $False } } while ($continue -eq $True) 

是。 这是一个例子。

 $userCount = 10 $users = @() 1..$userCount | ForEach-Object { $FirstName = Read-Host "Enter the first name of the employee" $LastName = Read-Host "Enter the last name of the employee" [INT]$empid = Read-Host "Enter the employee number" $group = Read-Host "Enter the group name" $homedrive = Read-Host "Enter the home drive" $NewHire = @{} $NewHire.Name = $FirstName $NewHire.Empid = $empid $NewHire.LastName = $LastName $NewHire.homedrive = $homedrive $Objectname = New-Object PSobject -Property $NewHire $users += $Objectname } $users 

在下面的文章中试试函数Read-HostArray,它提供了更好的解决scheme,直接将数组值粘贴到文本框中并传递给脚本。

http://powershelldude.blogspot.com/2015/08/read-hostarray-function-to-paste-multi.html