Building VMs using OSCustomizationSpec

With the release of Windows Server 2016, we did a complete overhaul of all of our VM templates.  The current way that I was building VMs (link to article) and configuring the NICs (link to article) stopped working because we increased the security footprint in the templates.  So I was stuck figuring out how to revamp my build script.  After doing some research I found a way to do everything with the OSCustomizationSpec and OSCustomizationNicMapping that I had additional scripts to do.  Not only does this reduce the time it takes to build the VM, but its less complicated.

#Generate a new OSCustomizationSpec so this will add the servers to the domain, and configure the NIC
      $OSCustomizationSpecExists = Get-OSCustomizationSpec -name PowerCliOnly
      IF ($OSCustomizationSpecExists -eq $True){
          Remove-OSCustomizationSpec -OSCustomizationSpec PowerCliOnly -Confirm:$false
          }
      Write-Host "Generating OSCustomizationSpec file" -ForegroundColor Yellow
      New-OSCustomizationSpec -OrgName "Organization_Name" -OSType Windows -ChangeSid -Server 'vCenter_Server' -Name PowerCliOnly -FullName "Company_Name" -Type Persistent -AdminPassword "Password_Here" -TimeZone 'Eastern (U.S. and Canada)' -AutoLogonCount 3 -Domain "Domain_Name" -DomainCredentials $credOSC -NamingScheme Vm -Description "PowerCli Use only" -LicenseMode PerServer -LicenseMaxConnections 5 -Confirm:$false
      Write-Host "Generating OSCustomizationNicMapping file" -ForegroundColor Yellow
      Get-OSCustomizationNicMapping -OSCustomizationSpec PowerCliOnly | Set-OSCustomizationNicMapping -Position 1 -IpMode UseStaticIP -IpAddress $IP -SubnetMask $SNM -DefaultGateway $GW -Dns $DNS1,$DNS2 -Wins $WINS1,$WINS2 -Confirm:$false

This is the simplest way to build servers as it is a complete server build and configuration in one step.  Before this I had 3 scripts that would need to be ran to get a server to the same point as using OS customization spec and OS customization NIC mapping.

The way this works is it generates a new OS customization spec file for each server that is going to be built, along with a new OS customization NIC mapping file.  In these files you will place the domain, IP, subnet mask, gateway, DNS servers, and WINS servers.  Then once you build the server you will call this newly created OS customization spec file to be used.  Once the server has been built it will need to be rebooted a few times which happens automatically due to the spec file and once it has completed it will be IP’ed and joined to the domain.

-Stuart

Get the complete script at Github – https://github.com/NotesofaScripter/PowerCLI

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.