Wednesday, August 14, 2013

Generate DR Portgroups for SRM with Powercli

The Problem

Today I was working at a customer site doing a SRM setup and was hit with a situation. The customer had setup a new DHCP vlan and portgroup at their DR site for SRM and wanted to fail over all their Production Servers to this network.
 As we went through their resource mappings and layout we came to an issue were a many to one mapping for Prod to DR for network resources was good but fail back from DR to Prod was not possible. This was because a one to one relationship was needed.

The Solution

In order to work around this I recommended to the client we setup a virtual port group for each prod network.  This new label at DR would be composed of the Prod virtual portgroup and DR DHCP network (i.e. VLAN113-DR-DHCP). Each of these new virtual portgroups will have the same VLAN ID for their newly created DHCP network.

Script was born

 So that being said, I went to my trusty script editor and started to write this powershell script that will take all production networks and apply them to DR esxi hosts. This script is for use with standard vswitch. Hope people find it useful. This will be handy script for adding new virtual portgroups or could be easily modified to use for migrations. Download here.

 #Production Cluster Name   
 $ProdClustername = "PROD CLUSTER"  
 #DR Cluster Name  
 $DRClustername = "DR CLUSTER"  
 #DR Virtual Switch Name used to create new portgroups to  
 $DRVswitchName = "vSwitch0"  
 #Vlan id to be used at DR  
 $vlanid = 20  
 #Virtual Portgroup Suffix  
 $vportgroupSuffix = "DR-DHCP"  
 #Connect to production Vcenter  
 Connect-VIServer PRODVCENTER  
 #Get portgroups from production  
 $portgroups = get-cluster $ProdClustername | get-vmhost | Get-VirtualPortGroup  
 #Disconnect from Production VCenter  
 Disconnect-VIServer -Confirm $false  
 #Connect to DR vcenter  
 Connect-VIServer DRVCENTER  
 #Loop through the list of hosts at DR site  
 foreach ($vmhost in (get-cluster $DRClustername | Get-VMHost)){  
      $vswitch = $vmhost | Get-VirtualSwitch -name $DRVswitchName  
      #Add the portgroups to each host at DR site  
      foreach ($portname in $portgroups){  
      Write-Host "Adding Portgroup $portname-$vportgroupSuffix to DR"  
      New-VirtualPortGroup -Name "$portname-$vportgroupSuffix" -VirtualSwitch $vswitch -VLanId $vlanid   
      }  
 }  
 #Disconnect DR Vcenter  
 Disconnect-VIServer -confirm $false