I've been Googling around all day and I can't seem to crack this nut. I've got a csv from RVTools with the VM name and the VLAN listed (Column names VM & Network) , my goal is to have PowerCLI change the VLAN in vCenter to match the VLAN in the CSV.
I first did it the hard way for our test environment, by putting both columns into their own separate array
$HadesList = "C:\Users\-user-\Desktop\test.csv"
$TestList = Import-CSV $HadesList
$result = ForEach($Line in $TestList) { $Line.VM }
$result2 = ForEach($Line2 in $TestList) { $Line2.Network }
get-vm $result[0] | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $result2[0] -Confirm:$false
...manually changing the numbers...
get-vm $result[24] | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $result2[24] -Confirm:$false
Which worked fine in the testlab, since it was only 25 VMs. But when we go to do this change in production, I wanted something more concise. I searched around and found someone doing for loops that will return data, so I tried to use it to change the network adapter, but it errored out:
$VMData = Import-CSV C:\Users\-user-\Desktop\test.csv
for ($i=0; $i -lt $VMData.count; $i++){
Get-VM $VMData.VM[$i] | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $VMData.Network[$i] -Confirm:$false
}
Set-NetworkAdapter : 2/24/2020 2:15:41 PM Set-NetworkAdapter Cannot find the environment browser for VMHost with Id: 'HostSystem-host-14'.
At line:2 char:46 + ... rkAdapter | Set-NetworkAdapter -NetworkName $VMData.Network[$i] -Conf ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (HostSystem-host-14:String) [Set-NetworkAdapter], ViError + FullyQualifiedErrorId : Client20_VmHostServiceImpl_TryGetHostEnvironmentBrowser_EnvironmentBrowserNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.SetNetworkAdapter
Is there anything that could make something like this work? I've been beating my head against this all day and I can't even figure out a way to phrase it to get better search results.
...Cannot find the environment browser for VMHost with Id: 'HostSystem-host-14'...
is a known bug and has no resolution. – Cameron Ellis