Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

After deploying a Windows VM clone from a template, how can a script be written to change the network the vNIC is on ("Network label" in the VM Settings window) and enable the NIC?

I'm working with templates built in one environment that need to be deployed into many in an automated fashion, and the network needs to be reassigned automatically for the vSphere cluster the VM is deployed to.

share|improve this question
Not sure why there is a downvote or a VtC for "not constructive" - I know many others who have run into similar issues before and since asking. – warren Jun 19 '13 at 17:38
up vote 2 down vote accepted

I wrote this function for deployment scripts which takes the IP address and does a lookup in an external file. Here's the code...

Function Get-NetworkName

$ip      = $vmip
$ipArray = $ip.Split(".")
$subnet  = $ipArray[0] + "." + $ipArray[1] + "." + $ipArray[2]

$path     = "vlanmapping.txt"
$vlanhash = ConvertFrom-StringData -StringData ([io.file]::ReadAllText($path))

$networkname = $vlanhash.Get_Item($subnet)

return $networkname

$vmname = "my_vm"
$vmip = ""
Get-VM -name $vmname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName (Get-NetworkName($vmip)) -confirm:$false

#Contents of vlanmapping.txt

BE CAREFUL! This works well with a VM with 1 NIC only. The Get-NetworkAdapter cmdlet returns all NICs so all NICs will have their Network set. Hope ok.

share|improve this answer
Is this in PowerShell? – Dan Feb 22 '13 at 13:42
It certainly is with the PowerCLI plugin of course – jimmyjamesd Feb 23 '13 at 21:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.