Automatic IP Assignment and VLAN change in VMware View

A while back I blogged about a scripting automation solution created by few VMware colleagues to automatic change the desktop VLAN in VMware View (here). The solution works great and I have received comments from organizations that have successfully implemented the scripts.

The previous solution created assume that the newly assigned VLAN will provide and allocate IP addresses to all virtual desktops via DHCP. A VMware colleague, Eric Monjoin (@emonloin), created a very simple and powerful PowerShell script that will change the virtual desktop IP address from DHCP to Fixed or Fixed to Fixed, using a pre-created .CSV file while also changing the VM portgroup assigned to the virtual desktop.

This script may help organizations that would rather have virtual desktops siting I a VLAN without DHCP and/or in a PVLAN where only RODC (Read-Only Domain Controller) are available. Adding additional security features to the virtual desktop infrastructure  is never a bad idea.

If you are running Windows 7 32bits you may need to duplicate GetVMGuestNetworkInterface_windows7_64Guest.bat as GetVMGuestNetworkInterface_windows7Guest.bat in C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\. Anyway, Eric explains how to do that in his PowerShell script.

You will need to run the script in one of the available View Connection Servers because the script loads the “VMware.VimAutomation.Core” SnapIn. The VMware View PS SnapIn does not accept remote execution at this point in time.

The easiest way to execute the script is to schedule a Windows task to run it every X minutes; however it’s also possible to configure a post-sync task in VMware View that uses PowerShell Remoting (invoke-command) to call the script when a new virtual desktop has been provisioned.


DISCLAIMER – The solution proposed below is a workaround and is offered without any support or warranties by me, VMware, or Eric Monjoin. It may not work in your environment for multiple different reasons. Only implement the solution if you feel conformable with the proposed setup and architecture, including scripting. Do not test the solution in production environment.

[css lang=”plain” autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ padlinenumbers=”false” gutter=”true” smarttabs=”true” tabsize=”10″ toolbar=”true”]
# Script to set static IP and move VM to other portgroup
# Eric Monjoin ([email protected]) 2012/03/26

# To make it works with Windows 7 32bits VM, in C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\
# copy GetVMGuestNetworkInterface_windows7_64Guest.bat to GetVMGuestNetworkInterface_windows7Guest.bat
# copy SetVMGuestNetworkInterface_windows7_64Guest.bat to SetVMGuestNetworkInterface_windows7Guest.bat

function LoadSnapin{
if (!(Get-PSSnapin | where {$_.Name -eq $PSSnapinName})){
Add-pssnapin -name $PSSnapinName
LoadSnapin -PSSnapinName "VMware.VimAutomation.Core"

# Populating static variables
$vcserver = ""
$vc_user = ""
$vc_userpwd = ""
$csvpath = ""
$prefix = ""
$SrcPG = ""

# Using variables
# Connect-VIServer $vcserver -User $vc_user -Password $vc_userpwd

# Using current credentail
Connect-VIServer $vcserver

# Get VMs present in the deployement portgroup and using a specific prefix
$vm = Get-VM -Name $prefix* | ? { $_ | Get-NetworkAdapter | Where { $_.NetworkName -eq $SrcPG } }

# Get ALL VMs present in the deployement portgroup
# ‘$vm = Get-VM | ? { $_ | Get-NetworkAdapter | Where { $_.NetworkName -eq $SrcPG } }’

# Ask for VM guest credentials
$GuestCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials", "", "")

# Import CSV file containing ALL VM
$vmlist = Import-CSV $csvpath

foreach ($virtualMachine in $vm) {
$item = $vmlist | Where-Object {$_.vmname -eq $}

# Setting variables
$vmname = $item.vmname
$ipaddr = $item.ipaddress
$subnet = $item.subnet
$gateway = $item.gateway
$pdns = $item.pdns
$sdns = $item.sdns
$pwins = $item.pwins
$swins = $item.swins
$portgrouptarget = $item.pgtgt

#Get the current interface info
$GuestInterface = Get-VMGuestNetworkInterface -VM $virtualMachine -GuestCredential $GuestCred

#Set the IP Address to VM
Set-VMGuestNetworkInterface -VMGuestNetworkInterface $GuestInterface -GuestCredential $GuestCred -IP $item.ipaddress -Netmask $item.subnet -Gateway $item.gateway -DNS $item.pdns,$item.sdns -Wins $item.pwins,$item.swins

# Moving deployed VM to production portgroup
Get-VM -Name $vmname | Get-NetworkAdapter | Where { $_.NetworkName -eq $SrcPG } | Set-NetworkAdapter -NetworkName $portgrouptarget -Confirm:$false

# Restarting VM
Restart-VMGuest -VM $vmname -Confirm:$false

listv.txt (Download here)


This article was first published by Andre Leibovici (@andreleibovici) at

Leave a Reply