In the script I've pieced together I am currently using a host array by specifying each host FQDN, but of course that doesn't scale very well with lots of hosts. Is there a better alternative that I can use to slightly tweak the array to include a range or something?
I'm using a bunch of for each $vmhost in $vmhost_array loops throughout the script and would prefer not to break them if possible.
What I'm using now:
$vmhost_array = @("host01com","host02com","host03com")
I don't think I can use this within the array, can I?
01..03 | Foreach-Object
Ok, if you want to use it that way, the script can look something like this.
With the ForEach, it will take the array elements one by one.
$vmhost_array = 01..03 | Foreach-Object {"host0$_.com"}
foreach ($vmhost in $vmhost_array) {
Add-VMHost $vmhost -Location $cluster -User root -Password $passwd -force:$true
Set-VMHost -VMHost $vmhost -LicenseKey $esx_license
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can, but remember that arrays have a zero-based index, i.e. the first element is $vmhost_array[0]
The loop would become 0..($vmhost_array.Count - 1)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Alright, thats confusing for me...I'm assuming what I've put together below is pretty far off track?
$vmhost_array = @(01..03 | Foreach-Object "host0$_.com")
foreach ($vmhost in 0..($vmhost_array.Count -1)) {
Add-VMHost $vmhost -Location $cluster -User root -Password $passwd -force:$true
Set-VMHost -VMHost $vmhost -LicenseKey $esx_license
}
Ok, if you want to use it that way, the script can look something like this.
With the ForEach, it will take the array elements one by one.
$vmhost_array = 01..03 | Foreach-Object {"host0$_.com"}
foreach ($vmhost in $vmhost_array) {
Add-VMHost $vmhost -Location $cluster -User root -Password $passwd -force:$true
Set-VMHost -VMHost $vmhost -LicenseKey $esx_license
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD! Worked like a charm! I wasn't too far off I guess.