I'm trying to pass a range as an array so I can return an array in PowerShell:
function iterHostsToArray($arr, $str) {
$hostz = @()
$arr | % { $hostz = "$($str)$($_)" }
return $hostz
}
$skybarHosts = iterHostsToArray((1..2), 'BAR')
$skyboxHosts = iterHostsToArray((1..6), 'BOX')
And I'm expecting the following:
PS> $skybarHosts
BAR1
BAR2
PS> $skyboxHosts
BOX1
BOX2
BOX3
BOX4
BOX5
BOX6
I'm refactoring from something like this, which works:
$skybarHosts = @()
(1..2) | % { $skybarHosts += "HCPNSKYBAR$($_)" }
$hostzvariable in each iteration. also, when a function should return an array, preceed that with a comma. This unary comma ensures the returned value is an array, even if it has just one element.