VMware: Mass change portgroup VM’s in cluster with PowerCLI

Last week I configured for a customer some new VLAN’s and created on each host new vSwitches with new portgroups, to migrate all VM’s in cluster with network label “VM Network” to “Production” I’ve created this script to automate this for all virtual machines with specific network label in cluster:

Original situation:

image


Script
:

#====================================================================#
#   SCRIPT:        PowerCLI_Mass_change_network_label.ps1            #
#   FUNCTION:      Mass change network label Network A to Network B  #
#   OWNER:         Sander Daems                                      #
#   VSPHERE:       5.0 Update 1 build 623860                         #
#   CREATED:       10/01/2013                                        #
#   MODIFIED:      10/01/2013                                        #
#   VERSION:       v.1.0                                             #
#====================================================================#
#   CHANGELOG:                                                       #
#                                                                    #
#    v.1.0                                                           #
#    - Script created;                                               #
#                                                                    #
#====================================================================#
#   HOST - Specify host and connect                                  #
#====================================================================#
$vCenterServer = Read-Host "Enter vCenter Hostname / IP"
Connect-VIServer $vCenterServer
#
#====================================================================#
# CUSTOM DEFINITIONS                                                 #
#====================================================================#
$Cluster = "Cluster01" 
$SourcePortgroup = "VM Network" 
$DestinationPortgroup = "Production" 
#
#====================================================================#
# SCRIPT                                                             #
#====================================================================#
Get-Cluster $Cluster | Get-VM | Get-NetworkAdapter | Where {$_.NetworkName -eq $SourcePortgroup } | Set-NetworkAdapter -NetworkName $DestinationPortgroup -Confirm:$false#====================================================================#

 

Result:


New situation
 (All VM’s in cluster):

image

sanderdaems

Sander Daems is founder and author of this blog and working as a Lead (Sr.) Consultant by UNICA ICT Solutions. Sander has 15+ years experience in IT, primary focus: virtualization and modern worksplace.

More Posts - Website

Follow Me:
TwitterLinkedIn

5 thoughts on “VMware: Mass change portgroup VM’s in cluster with PowerCLI

  1. Keep up the fantastic work , I read few blog posts on this website and I think that your site is rattling interesting and contains sets of fantastic info .

  2. Great Stuff , anybody have a sample script of how to handle MANY Port Groups . As in , not a static value to OLD and NEW but an array . I have created an imported a CSV , but have no test environment . Don’t want to experiment in PROD 🙂

    If I have this
    $vNetworks = Import-Csv .\LDSvdsvss.csv | select OLDNetwork , NewNetwork

    Can i just foreach VM or VMadapter

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

  3. Hello , just wanted to share the answer >

    $vNetworks = Import-Csv .\MANvdsvss.csv | select OLDNetwork , NewNetwork
    $ESXiHost = “hrlmgex5057.emea.hays.loc”
    $VMs = Get-VMHost -name $ESXiHost | Get-VM | select name

    Foreach ($ThisVM in $VMs){ # [0] run only 1st one in loop
    write-host “$($ThisVM.Name)”

    $Adaptors = Get-VM -name $ThisVM.Name |Get-NetworkAdapter
    ForEach($Adaptor in $Adaptors){
    If($Adaptor.NetworkName -in $vNetworks.OldNetwork){
    $ThisLookup = $vNetworks | Where{$_.OldNetwork -eq $Adaptor.NetworkName}
    write-host “–> $($Adaptor.Name) –> $($Adaptor.NetworkName) –> $($ThisLookup.NewNetwork)”

    $Adaptor |Set-NetworkAdapter -NetworkName $ThisLookup.NewNetwork -whatif -Confirm:$true
    }
    }

  4. I love you! Not a complexe statement at all but would have taken me an hour at leeeeast to get there, as I don’t use PowerCLI commonly.

    (Have added a quick check before the mutation on my side, which additionally shows the names of the VMs:
    Get-Cluster $Cluster | Get-VM | Get-NetworkAdapter | Where {$_.NetworkName -eq $SourcePortgroup } | Select Parent, Name, Type, NetworkName
    )

    Thanks a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *