简体   繁体   中英

Beginner issues with PowerCLI, Multiple arrays, for loops

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.

I think you could probably consolidate it down to looping through the variable of the imported CSV with a named variable for each line.

Example:

$VMData = Import-CSV C:\Users\-user-\Desktop\test.csv
foreach ($line in $VMData){
   Get-VM $line.VM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $line.Network -Confirm:$false
}

I tried the script from Kyle on a different vCenter and it worked perfectly. The odd part is I can't find the difference in PowerCLI between the two

Errors out:

  • PowerCLI C:> Get-PowerCLIVersion
  • PowerCLI Version
  • VMware PowerCLI 6.5 Release 1 build 4624819
  • Component Versions
  • VMware Cis Core PowerCLI Component 6.5 build 4624453
  • VMware VimAutomation Core PowerCLI Component 6.5 build 4624450
  • VMWare ImageBuilder PowerCLI Component 6.5 build 4561891
  • VMWare AutoDeploy PowerCLI Component 6.5 build 4561891
  • VMware Vds PowerCLI Component 6.5 build 4624695
  • VMware Cloud PowerCLI Component 6.5 build 4624821
  • VMware HA PowerCLI Component 6.0 build 4525225
  • VMware HorizonView PowerCLI Component 7.0.2 build 4596620
  • VMware Licensing PowerCLI Component 6.5 build 4624822
  • VMware PCloud PowerCLI Component 6.5 build 4624825
  • VMware Storage PowerCLI Component 6.5 build 4624820
  • VMware vROps PowerCLI Component 6.5 build 4624824
  • VMware vSphere Update Manager PowerCLI 6.5 build 4540462

Works perfectly:

  • PowerCLI Version
  • VMware PowerCLI 6.5 Release 1 build 4624819
  • Component Versions
  • VMware Cis Core PowerCLI Component 6.5 build 4624453
  • VMware VimAutomation Core PowerCLI Component 6.5 build 4624450
  • VMWare ImageBuilder PowerCLI Component 6.5 build 4561891
  • VMWare AutoDeploy PowerCLI Component 6.5 build 4561891
  • VMware Vds PowerCLI Component 6.5 build 4624695
  • VMware Cloud PowerCLI Component 6.5 build 4624821
  • VMware HA PowerCLI Component 6.0 build 4525225
  • VMware HorizonView PowerCLI Component 7.0.2 build 4596620
  • VMware Licensing PowerCLI Component 6.5 build 4624822
  • VMware PCloud PowerCLI Component 6.5 build 4624825
  • VMware Storage PowerCLI Component 6.5 build 4624820
  • VMware vROps PowerCLI Component 6.5 build 4624824
  • VMware vSphere Update Manager PowerCLI 6.5 build 4540462

They're both on vSphere Client version 6.7.0.20000 so I really can't point to any reason it fails on one not the other

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM