PowerCLI: Mass VM Portgroup Change

I was asked in my Script List if it was possible to change Multiple VMs to attach to a new PortGroup, this task is easy enough so long as you remember that every VM can have up to 4 virtual nics in VI3 and up to 10 in vSphere.

So to change all virtual machines we would do the following:

$OldNetwork = "PG1 192.168.0"
$NewNetwork = "PG2 10.1.1"

Get-VM |Get-NetworkAdapter |Where {$_.NetworkName -eq $OldNetwork } |Set-NetworkAdapter -NetworkName $NewNetwork -Confirm:$false

Or to change all of the VMs for a particular cluster we could use:

$Cluster = "Non-Production"
$OldNetwork = "PG1 192.168.0"
$NewNetwork = "PG2 10.1.1"

Get-Cluster $Cluster |Get-VM |Get-NetworkAdapter |Where {$_.NetworkName -eq $OldNetwork } |Set-NetworkAdapter -NetworkName $NewNetwork -Confirm:$false

Note: By default the Set-NetworkAdapter cmdlet would prompt you for each change it was going to make, this is overwritten by using the –Confirm:$false parameter.

VESI

This can also be achieved from the VESI by:

  • Connect to a managed host
  • Select Virtual Machines
  • Select the virtual machines from the grid view which you would like to change
  • Click ‘Network Adapters’ from the links pane on the right
  • Filter the grid view to only display the adapters attached to a certain Network Name
  • Click ‘Change Properties’ from the Actions pane on the right and enter a new NetworkName as seen below

image

9 thoughts on “PowerCLI: Mass VM Portgroup Change”

  1. Hi Alan,
    Request you to provide a script to add a vNIC to multiple VMs with same portgroup.
    Thanks in advance.

  2. @Faaris – How did you given the old and new port group name in same .csv file.

    Can i get the format of PGMigration.csv file

  3. How did you given the old and new port group name in same .csv file.

    Can i get the format of PGMigration.csv file

  4. I would like to import a csv with defined Old Port Groups and New Port Groups broken by line. So far, I have not gotten to function. Here’s a sample of what I’m trying to use:

    #### Variables
    $VC = Read-Host “Enter Your vCenter Server”
    $Cluster = Read-Host “Enter Your Cluster Name”
    $VFILE = Import-Csv c:\pcli\PGMigration.csv

    #### Connect to Your vCenter
    Connect-viserver $VC

    #### Execution Area

    $VFILE | % {

    $OLDPG = $_.OldPG
    $NEWPG = $_.NewPG
    Get-Cluster $Cluster | Get-VM | Get-NetworkAdapter | Where {$_.NetworkName -eq $OLDPG } | Set-NetworkAdapter -NetworkName $NEWPG -Confirm:$false

    }

    #### Disconnect from Your vCenter
    Disconnect-VIServer $VC -Confirm:$false

  5. Hi there,

    VESI’s site seems to be redirecting to [http://www.powergui.org/index.jspa]

    Is it the same software?

    btw, love your scripts!!!!!!!

  6. What if i had to change only the name of the portgroup, for example from a to b, the minute i change it in the host level, all the vm’s will lose the network, will this script still work? or it will fail to get the old network?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.