Options

PowerCLI help change port group

odysseyeliteodysseyelite Member Posts: 504 ■■■■■□□□□□
Just learning Powercli and i'm having a problem changing the network on a vm.

I've used examples, and powerGUI and get the same error.
VM test08 has network adapter 1 set to "internal". I would like to change that by powercli to the network "vm network"

Name Type NetworkName MacAddress WakeOnLan
Enabled
---- ----


Network adapter 1 Vmxnet3 VM Network 00:50:56:85:42:11 True



PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-vm test08 |Get-NetworkAdapter |Set-NetworkAdapter -NetworkName "vm network"

Confirm
Are you sure you want to perform this action?
Performing operation "Setting NetworkName: vm network" on Target "Network adapter 1".
[Y] Yes [A] Yes to All [N] No [L] No to All Suspend [?] Help (default is "Y"): y
Set-NetworkAdapter : 3/8/2012 8:30:37 PM Set-NetworkAdapter The network "vm network" doesn't exist on the host.
At line:1 char:54
+ Get-vm test08 |Get-NetworkAdapter |Set-NetworkAdapter <<<< -NetworkName "vm network"
+ CategoryInfo : ResourceUnavailable: (vm network:String) [Set-NetworkAdapter], ViError
+ FullyQualifiedErrorId : Client20_VmHostServiceImpl_TryGetHostNetworkByName_NonexistentNetwork,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.S

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>
Currently reading: Start with Why: How Great Leaders Inspire Everyone to Take Action

Comments

  • Options
    QHaloQHalo Member Posts: 1,488
    This worked for me. Obviously chop it up to match your environment. But this will move portgroups on the same vSwitch. What I've found with PowerCLI is that most of the time if you want to do something complicated that involves lots of pipes, create a variable. There's probably another way to do this but this does work. Good luck with PowerCLI. I love it.
    $virtualportgroup = get-vmhost esxi1.wilmo.local | get-virtualswitch -Name vSwitch2 | get-virtualportgroup -name "VM Network 2"
    Get-VM DC | get-networkadapter | set-networkadapter -Networkname $virtualportgroup
    
  • Options
    odysseyeliteodysseyelite Member Posts: 504 ■■■■■□□□□□
    QHalo wrote: »
    This worked for me. Obviously chop it up to match your environment. But this will move portgroups on the same vSwitch. What I've found with PowerCLI is that most of the time if you want to do something complicated that involves lots of pipes, create a variable. There's probably another way to do this but this does work. Good luck with PowerCLI. I love it.
    $virtualportgroup = get-vmhost esxi1.wilmo.local | get-virtualswitch -Name vSwitch2 | get-virtualportgroup -name "VM Network 2"
    Get-VM DC | get-networkadapter | set-networkadapter -Networkname $virtualportgroup
    

    I'll try this. I believe I did figure it out. I was using someone else's exact code and getting errors. Of course everything out there is for 4.1 and not five. I had to pipe a where statement for the network adapter 1 even where there is only one adapter. Powergui tried to do that but failed.

    We will see when my clone finishes if it works.

    I'm starting to have a love\hate realationship with PS. Wish they had help like cisco. put a simple ? and it tells you your next argument.
    Currently reading: Start with Why: How Great Leaders Inspire Everyone to Take Action
  • Options
    odysseyeliteodysseyelite Member Posts: 504 ■■■■■□□□□□
    QHalo wrote: »
    This worked for me. Obviously chop it up to match your environment. But this will move portgroups on the same vSwitch. What I've found with PowerCLI is that most of the time if you want to do something complicated that involves lots of pipes, create a variable. There's probably another way to do this but this does work. Good luck with PowerCLI. I love it.
    $virtualportgroup = get-vmhost esxi1.wilmo.local | get-virtualswitch -Name vSwitch2 | get-virtualportgroup -name "VM Network 2"
    Get-VM DC | get-networkadapter | set-networkadapter -Networkname $virtualportgroup
    

    Also had to use a filter with your code:
    get-vm test_5 |Get-NetworkAdapter |where {$_.Name -eq "Network adapter 1"} |Set-NetworkAdapter -NetworkName $virtualportgroup
    
    Currently reading: Start with Why: How Great Leaders Inspire Everyone to Take Action
  • Options
    QHaloQHalo Member Posts: 1,488
    Are you using the latest version of PowerCLI? I'm not sure why you needed the filter, but it's probably best anyway from a coding perspective. I thought about that last night as I posted that I should probably ensure that the network adapter I wanted to change was Network Adapter 1 but since I only had one, it worked either way.
  • Options
    odysseyeliteodysseyelite Member Posts: 504 ■■■■■□□□□□
    QHalo wrote: »
    Are you using the latest version of PowerCLI? I'm not sure why you needed the filter, but it's probably best anyway from a coding perspective. I thought about that last night as I posted that I should probably ensure that the network adapter I wanted to change was Network Adapter 1 but since I only had one, it worked either way.

    Yes, I'm using 5. I think its working now, I also had a issue with capitalization. Now I am trying to get the wait task look to work so I can spin up several VM's at a time.
    Currently reading: Start with Why: How Great Leaders Inspire Everyone to Take Action
Sign In or Register to comment.