Friday, September 27, 2013

vCAC - how to change the VM network after provisioning

Sometimes we are asked for some special test cases in pilot scenarios. On of my colleagues asked me how we can change a virtual machine network after the virtual machine is provisioned in vCAC. The reason for this was a provisioning environment in which the virtual machine is deployed in a special network and attached to the production afterwards.

So at first you have to set the "ExternalWFStubs.MachineProvisioned" property at the virtual machine blueprint.

As an additional input i created a custom property called "VirtualMachine.NewNetwork" which holds the name of the new network. I created a custom property with a dropdown list (Values: VM Network, DVPortGroup) to let the user select the network.

Next step was the creation of a powershell script (ChangeNetwork.ps1):

#Variable detection
$OldNetwork = $Properties["VirtualMachine.Network0.Name"]
$NewNetwork = $Properties["VirtualMachine.NewNetwork"]
$Hostname = $Properties["VirtualMachineName"]

#Check for the variables
$OldNetwork >> C:\test.txt
$NewNetwork >> C:\test.txt
$Hostname >> C:\test.txt

#Calls the powershell actions
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer YourVcenterServer -User YourUser -Password YourPass
Get-VM -Name $Hostname | Get-NetworkAdapter | Where {$_.NetworkName -eq $OldNetwork }|Set-NetworkAdapter -NetworkName $NewNetwork -Confirm:$false

I uploaded the powershell script using the CloudUtil command line:

CloudUtil File-Import -n ChangeNetwork -f ChangeNetwork.ps1

Now i created a new workflow in the Design Center for the MachineProvisioned stub like Zack described here: Calling Powershell/PowerCLI Scripts from a vCAC workflow.

Please note that you need to initialize the Dictionary array like described in the blog post. After sending the workflow to the vCAC server the stub should call the Powershell script and change the network adapter from "DVPortGroup" to "VM Network".

After the deployment (my template has DVPortGroup as network when deployed)

the network should change with a reconfigure task.

Have fun with this!