pages

Showing posts with label PowerCLI. Show all posts
Showing posts with label PowerCLI. Show all posts

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!




Tuesday, July 24, 2012

PowerCLI - IP reservation with PowerShell

Sometimes I receive questions which aren´t exactly my skill. In this case I was asked for a "IP management" from a .csv/.txt file to receive an IP address and reserve it. The IP address should be used for a vCloud Director vApp deployment. As a google junkie i searched for a ready-to-use solution but there wasn´t any.

So i decide to build a small script and a text file to play around with. The text file (ip_names.txt) only has three informations, comma separated:

ip address; dns name; state

and looks like this:



127.0.0.1;cjohannsen001;reserved
127.0.0.2;cjohannsen002;free
127.0.0.3;cjohannsen003;reserved
127.0.0.4;cjohannsen004;free
127.0.0.5;cjohannsen005;free
127.0.0.6;cjohannsen006;reserved



The goal was to select an ip address and if the address is chosen it should be reserved by changing the state keyword.

After a few attempts I figure out the following script:



$file = "ip_names.txt"
$Lines = Get-Content -path $file -readcount 0


For($i=0; $i -lt $Lines.Count; $i+=1){


$ip = $Lines[$i].ToString().Split(';')[0].Trim() 
$dns = $Lines[$i].ToString().Split(';')[1].Trim() 
$state = $Lines[$i].ToString().Split(';')[2].Trim() 
  
    if ($state –eq 'free') {
echo $ip "... is free"
$Lines[$i]
$bool = Read-Host "Use IP?"
if($bool -eq "yes"){
$Lines[$i] = $Lines[$i].Replace("free", "reserved")
$Lines | Set-Content -Path $file
echo "IP address:"
$ip
break
}
else{
echo "IP wasn´t chosen."
}
}
}















With this small script you will be able to "select" an IP address ;)