Fun thing of today: Powershell and VMware – Part 1

19 May 2010

Today’s fun thing: utilizing powershell to test stuff in VMware. There are a few cool things I have been able to piece together with Powershell and VMware -hopefully some of those will make it into a brief series.

I wrote two scripts to help with testing the various VLANs that were necessary for our VMs. (Really less scripts and more basic functions)

Function 1: PowerCLI

$vm = get-vm testVM
$VLANs = "3","5","08","09","11","18","22","35","59","106","112","201","202","222"
foreach ($vlan in $VLANs){
     $vm |Get-NetworkAdapter|Set-NetworkAdapter -networkname "VLAN $vlan" -Confirm:$false
     write-host "Press any key to continue..."
     $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

Function 2: Testing VM

$VLANs = "3","5","08","09","11","18","22","35","59","106","112","201","202","222"
foreach ($vlan in $VLANS) {
     .\set-StaticIP.ps1 -ip 10.10.$vlan.254 -gtw 10.1.$vlan.254
     start-sleep 5
     $ping = new-object System.Net.NetworkInformation.Ping
     $rslt = $ping.send("10.10.3.3")
     if ($rslt.status.tostring() -eq "Success"){
          write-host "Vlan $vlan tested ok"
     }
     else {​
          write-host "Vlan $vlan ping test failed"
     }
     write-host "Press any key to continue..."
     $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

Script 1: set-StaticIP.ps1

param(  [string]$ip,
     [string]$mask = "255.255.255.0",
     [string]$gtw = "192.168.1.254",
     [string]$dns1 = "192.168.1.1",
     [st​ring]$dns2
     )
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration | where{$_.IPEnabled -eq "TRUE"}
Foreach($NIC in $NICs) {
     if ($ip){
          $no = $NIC.EnableStatic($ip, $mask)
     }
     $no = $NIC.SetGateways($gtw)
     if($dns2){
          $dns ="$dns1,$dns2"
     }
     $no = $NIC.SetDNSServerSearchOrder($dns)
     $no = $NIC.SetDynamicDNSRegistration("FALSE")
}

Explanation

Function1 is started first using PowerCLI (after connecting to the appropriate ESX host). It configures the specified VM’s network adapter to be on the first VLAN, then pauses. After the vmnic is configured properly, Function2 is run on the testVM in powershell. This utilizes the set-StaticIP.ps1 script and configures the IP address to match the specified VLAN, then attempts a ping against MSIDI001 (This machine should be pingable from all the testing VLANS. If the ping is successful, it is indicated that the VLAN tested ok, if not, the failure is stated. After the result is noted, any key is pressed in PowerCLI to set the vmnic to the next VLAN. After it has been configured, any key is pressed in Fuction2 to set the new IP for testVM (the ping test then takes place on this new VLAN). This is repeated until all VLANs have been tested.
This is much quicker than manually assigning IPs or setting VLAN membership in VMware’s Infrastructure Client.
Comments (0) Trackback Leave a comment
  1. No comments yet.
Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
  1. No trackbacks yet.