This is what I use to get an IP address, create the customization spec, and clone from the template.
#vlan125
if ($vlan -eq 'vlan125'){
$valid = $true
foreach($i in 254..3)
{
$ip = "111.111.1.$i"
If (Test-Connection -comp $ip -count 1 -quiet) {
write-host -ForegroundColor Green "Computer online $ip"
}
Else {
"Computer offline $ip"
try{
$HostName = [System.Net.Dns]::GetHostByAddress($ip).HostName
$hostname
}
catch{
write-host -ForegroundColor Red "This is the $ip that will be used.";
$valid=$false;
break
}
}
}
if ($valid -eq $true){
write-host -ForegroundColor Red "Alert there are no available IP address for $vlan"
}
$SUB = '255.255.255.0'
$Gateway = '111.111.11.1'
}
$custSpec = New-OSCustomizationSpec -Type NonPersistent -OSType Windows `
-OrgName “WORK” -FullName "$LocalAdminAccount" -Domain “WORK.HOME” `
-DomainUsername $usr -DomainPassword $pss -LicenseMode PerSeat `
-AdminPassword "This is the password" -NamingScheme Fixed -NamingPrefix $newVMName -TimeZone 035 -ChangeSid
#Modify the default network configuration settings
$custSpec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping `
-IpMode UseStaticIP -IpAddress $IP -SubnetMask $SUB `
-Dns $DNS -DefaultGateway $Gateway -Wins $WINS
#Deploy a VM from a template
$clonetask = New-VM -Name "$newVMName" -Template "$template" -VMHost "$tgtEsxName" -Datastore "$tgtDatastoreName" `
-OSCustomizationSpec $custSpec