Here's how I do it:
$ip = Read-Host "IP Address?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Use default network settings." $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Set your own network settings."
$netOptions = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$netTitle = "Networking Settings" $netMessage = "Use standard subnet mask(255.255.255.0), default gateway(192.168.100.1) and DNS servers(192.168.100.80,192.168.100.31,72.3.128.240,72.3.128.241)?" $netResult = $Host.ui.PromptForChoice($netTitle, $netMessage, $netOptions, 0)
switch ($netResult) { 0 { $netmask = "255.255.255.0" $gateway = "192.168.100.1" $DNS = "192.168.100.80,192.168.100.31,72.3.128.240,72.3.128.241" } 1 { $netmask = Read-Host "Network Mask:" $gateway = Read-Host "Default Gateway:" $DNS = Read-Host "DNS Server(s) (Note: Enter DNS server as a comma separated list, no spaces):" } }
$specName = "tempSpec" + (Get-Random)
$DNSA = $DNS -split ','
Get-OSCustomizationSpec -Name $spec | New-OSCustomizationSpec -Name $specName -Type NonPersistent
Get-OSCustomizationSpec -Name $specName | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping ` -IpMode UseStaticIP -IpAddress $ip -SubnetMask $netmask -DefaultGateway $gateway -Dns $DNSA
$tempSpec = Get-OSCustomizationSpec -Name $specName
$vmhost = (Get-Cluster | Get-VMHost | Sort-Object -Property MemoryUsageGB | Select-Object -First 1)
New-VM -Name $name -template $template -OsCustomizationSpec $tempSpec -VMHost $vmhost -Confirm:$true
Remove-OSCustomizationSpec -Confirm:$false -customizationSpec (Get-OSCustomizationSpec -name $specName)