Hello folks,
I am having a hard time properly sorting the values stored in an array. Pardon if I am not accurately using the right terminology. I am not a developer but just someone who is passionate about efficiency.
The $output variable is storing all the networks available for an ESX host. These are being displayed as a menu with number values assigned to each network. See code and output below.
#Getting network info from host
$output = Get-VMHost HOST | Get-VirtualPortGroup | Select -ExpandProperty Name
#Convert output into array
$network = $output -split “`n” | Sort-Object
#Display the array as a menu
Write-Host “Networks Available” Write-Host “---------” for ($i = 12; $i -le $network.Count; $i++) { $NetworkNumber = $i + 1 Write-Host “$NetworkNumber. $($network[$i])” }
#Menu output from the $output array Networks Available
- TEST_10.10.10.0
- DEV_10.11.10.0
- PRD_10.12.10.0
#Prompt user for network selection** `$vmSubnet = Read-Host -Prompt “Network Name”
Network Name:
From the output above, I have a prompt that prompts a user for an input. The input is expecting a numeric value such as 1-3. Example, 1 should be TEST_10.10.10.0 and 2 would be DEV_10.11.10.0.
The issue that I can’t get around is that the number being assigned to each network label inside the array does not match the network once I try to apply the array as a value. See below.
#Applying portgroup values `Get-VM -name VM01 | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $network[$vmSubnet] -StartConnected $true -confirm:$false
What ends up happening is the VM will be updated and a network will be assigned but not the one that corresponds to the number I chose. It is randomly assigned and does not match the original $output variable. What am I missing here?
Thanks!