PowerCLI Set-OSCustomizationSpec for IP address

Here's how I do it:

Set networking settings

$ip = Read-Host "IP Address?"

Define networking settings prompt options

$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."

Define the array of networking settings options

$netOptions = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

Create networking settings prompt

$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):" } }

Generate a random name for the tempSpec to allow parallel runs

$specName = "tempSpec" + (Get-Random)

Split DNS string into array

$DNSA = $DNS -split ','

Modify the selected Custom Spec to configure desired networking settings

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

Use the host with the current lowest memory usage

$vmhost = (Get-Cluster | Get-VMHost | Sort-Object -Property MemoryUsageGB | Select-Object -First 1)

Create the VM

New-VM -Name $name -template $template -OsCustomizationSpec $tempSpec -VMHost $vmhost -Confirm:$true

Cleanup the temporary Spec. System will do this outside of the session, but this will allow the scripts to be reused within a session.

Remove-OSCustomizationSpec -Confirm:$false -customizationSpec (Get-OSCustomizationSpec -name $specName)

/r/vmware Thread