Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

1 Answer 1

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
{
param($vmip)

$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 = "10.10.1.100"
Get-VM -name $vmname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName (Get-NetworkName($vmip)) -confirm:$false


#Contents of vlanmapping.txt
#10.10.10=VLAN1
#10.10.20=VLAN2
#10.10.30=VLAN3

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

 
discard

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.