a user suggested in a previous post, which was to put a , in front of the values if its a single item array,
I had to put the comma after the values. You need to build the $x array dynamically.
cls
.\test.ps1 -servers ('72','Server1\DEV2','1.0') -folder "F:\files"
.\test.ps1 -servers (('72','Server1\DEV2','1.0'),('88','Server8\DEV7','2.0')) -folder "F:\files"
.\test.ps1 -servers ('72','Server1\DEV2','1.0'), -folder "F:\files"
Updated code.
param ([array[]]$servers = $(throw "Input array of servers.") , $folder )
"-------------------------------"
[System.Collections.ArrayList]$x = @() # start with an empty array
"Parameter count is {0}" -f $servers.count
$AddServers = $false # flag to add entire servers array
$servers | foreach {
"This entry contains {0} items. " -f $_.count
if ($_.count -eq 1) {
"Unable to process single dimentional array"
$AddServers = $true
} else {
$itemcount = $x.add($_)
}
}
if ($AddServers -and $x.count -eq 0) { # add a single entry but only if we haven't already added other entries
"Adding entire servers arry."
$itemcount = $x.add($servers)
}
"X count is {0}" -f $x.count
if ($x.count -eq 0) {
"Nothing to process"
exit
}
#$x = $servers # this update builds the x array dynamically
$k = 'serverid','servername','locationid','appid' # key names correspond to data positions in each array in $x
$h = @{}
For($i=0;$i -lt $x[0].length; $i++){
$x |
ForEach-Object{
[array]$h.($k[$i]) += [string]$_[$i]
}
}
$all_server_ids = $h['Serverid']
foreach ($server_id in $all_server_ids)
{
$severid = $h["serverid"][$all_server_ids.indexof($server_id)]
$servername = $h["servername"][$all_server_ids.indexof($server_id)]
$locationid = $h["locationid"][$all_server_ids.indexof($server_id)]
Write-Output "This $severid and this $servername and this $locationid"
}
Output:
Parameter count is 3
This entry contains 1 items.
Unable to process single dimentional array
This entry contains 1 items.
Unable to process single dimentional array
This entry contains 1 items.
Unable to process single dimentional array
Adding entire servers arry.
X count is 1
This 72 and this Server1\DEV2 and this 1.0
Parameter count is 2
This entry contains 3 items.
This entry contains 3 items.
X count is 2
This 72 and this Server1\DEV2 and this 1.0
This 88 and this Server8\DEV7 and this 2.0
Parameter count is 2
This entry contains 3 items.
This entry contains 1 items.
Unable to process single dimentional array
X count is 1
This 72 and this Server1\DEV2 and this 1.0
PS C:\Temp>