I am trying to rebuild several VM's each night and want to do this from a template . I have found when I am doing these builds I need to update the MAC, VLAN, and IP information in order to get them set up correctly. I could easily import a file to read in all the fields, but I am not sure how to put this together. I was able to get this working on terraform but I would like to have this on powershell so I can create a scheduled job for it with the rest of our stuff.
appreciate the help!
So what I do is take my already existing (created manually) customization spec, copy it to a temp one in my script, do the DHCP vs. static there, then delete the temp one, like this:
#Creates random 8 character variable
$Chars = [Char[]]"abcdefghijklmnopqrstuvwxyz"
$RandomName = ($Chars | Get-Random -Count 😎 -join ""
$osCust = Get-OSCustomizationSpec -Name $osCustName | New-OSCustomizationSpec -Name $RandomName -Type Persistent
You can leave the original cust spec as DHCP and set the IP as needed:
Switch ($IPOSType) {
"DHCP" {
Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust | Set-OSCustomizationNicMapping -IpMode UseDHCP -ErrorAction SilentlyContinue | Out-Null
}
"Windows" {
Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IP -SubnetMask $SubnetMask -DefaultGateway $Gateway -Dns $DNS -ErrorAction SilentlyContinue | Out-Null
}
"Linux" {
Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IP -SubnetMask $SubnetMask -DefaultGateway $Gateway -ErrorAction SilentlyContinue | Out-Null
}
}
Then after the script is done I delete the temp one:
Remove-OSCustomizationSpec $osCust -Confirm:$false
Hopefully that makes sense.
Why don't you use an OSCustomizationSpec?
Have a look at the New-OSCustomizationSpec and New-OSCustomizationSpecNicMapping cmdlets.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So that is what I was seeing but I am confused on how to put this together since most of what I see out there is for Windows.
My template I want to use is RHEL7. I have followed guidelines by RedHat on how to prepare the server so that I can convert it to a template - all which seems good. If I deploy the template manually - all I need to do is update the Mac for the NIC and everything works.
When I try and script this - I am running into issues where the hostname, IP, DNS, etc is not getting updates. I am confused on how to set up the OSCustomizationNicMapping.
so if I had very basic to just test if I can get what I want to run - how do I connect the specs with inputting them into my build?
$OSSpecs = Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping $spec -IpMode UseStaticIP -IPAddress 1.2.3.4 -SubnetMask 255.255.254.0 -DefaultGateway 1.2.3.1
New-VM -Name test -Template template_test -VMHost esxihost -OSCustomizationSpec $OSSpecs
Start-VM -VM test -Confirm:$false
thanks
You're doing almost exactly what I'm doing. I did find that for some reason the Linux specs don't accept the -DNS parameter. Looks like you have the $spec variable in the wrong place...
Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IP -SubnetMask $SubnetMask -DefaultGateway $Gateway
Ok so I have figured out a way to create a customization. I am still unsure of how to call this customization and then pass it the IP information.
Also - how can I get the option to 'Use and application configured on the vCenter Server to generate the IP Address'?
Thanks!
Try Example 4 here:
Set-OSCustomizationNicMapping - vSphere PowerCLI Cmdlets Reference
thank you! ok so when I make the OS customization I was choosing DHCP so that I could then pass it what to override. Or do I need to set up static IP then pass it what to over ride?
or I guess the better question is how do I set the IP settings for just the VM I am creating rather than my OSCustomization setting?
So what I do is take my already existing (created manually) customization spec, copy it to a temp one in my script, do the DHCP vs. static there, then delete the temp one, like this:
#Creates random 8 character variable
$Chars = [Char[]]"abcdefghijklmnopqrstuvwxyz"
$RandomName = ($Chars | Get-Random -Count 😎 -join ""
$osCust = Get-OSCustomizationSpec -Name $osCustName | New-OSCustomizationSpec -Name $RandomName -Type Persistent
You can leave the original cust spec as DHCP and set the IP as needed:
Switch ($IPOSType) {
"DHCP" {
Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust | Set-OSCustomizationNicMapping -IpMode UseDHCP -ErrorAction SilentlyContinue | Out-Null
}
"Windows" {
Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IP -SubnetMask $SubnetMask -DefaultGateway $Gateway -Dns $DNS -ErrorAction SilentlyContinue | Out-Null
}
"Linux" {
Get-OSCustomizationNicMapping -OSCustomizationSpec $osCust | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IP -SubnetMask $SubnetMask -DefaultGateway $Gateway -ErrorAction SilentlyContinue | Out-Null
}
}
Then after the script is done I delete the temp one:
Remove-OSCustomizationSpec $osCust -Confirm:$false
Hopefully that makes sense.
ah ok - that sounds like that could work - thank you