Who doesn’t love a bit of automation these days, I certainly like to use code snippets and shortcuts whenever I can. Recently I’ve been deploying some VMs in my homelab and thought I would share some of the POWERCLI I use.
I have a Windows 2016 template stored in vCenter that I have recently patched and tweaked to my satisfaction and deploy that with a temporary customization spec to deploy and do the initial config (IP settings and domain join).
The key commands to use here to get the IP settings are
Get-OScustomizationNicMapping
Set-OSCustomizationNicMapping
Only started playing with these recently and they are very useful.
## Deploy New VM ## CT April 2019 ## Define Variables $VCCred = Get-Credential # vCenterCred $DomCred = Get-Credential # Cred for adding VM to domain $VC = "vc1.yourdomain.com" # vCenter $VCTemplate = "Windows Server 2016 Template" # name of existing VM Template $VMname = "NewVM" # New VM name $Domain = "yourdomain.com" # Domain $LocalAdmin = "Administrator" # Local Admin name of VM $LocalAdminPW = "D3pl0yMyVMBaby!" # Local Admin pw $OrgName = "OrgName" # Organisation $VMIP = "192.168.0.101" #Desired IP of server $VMSubnetMask = "255.255.255.0" #VM subnetmask $VMDefaultGW = "192.168.0.1" #VM Default GW $DNSServer1 = "192.168.0.110" #DNS Server1 $DNSServer2 = "192.168.0.120" #DNS Server2 $VMHost = "esx1.yourdomain.com" #Target Host $Datastore = "vsandatastore" #Target Datastore ## Connect to vCenter Connect-VIServer $VC -Credential $VCCred ## Create Customisation $CustSpec = New-OSCustomizationSpec -OSType "Windows"` -Name "TempSpec" -Type NonPersistent ` -NamingScheme vm ` -Domain $Domain -DomainCredentials $DomCred ` -FullName $LocalAdmin -AdminPassword $LocalAdminPW ` -AutoLogonCount 1 ` -OrgName $OrgName -Description "Temp Custom Spec for VM deployment" ` -TimeZone 085 -ChangeSid ` -ErrorAction Stop ## Set Network Properties $IP = @{OScustomizationNicMapping = Get-OSCustomizationNicMapping -OSCustomizationSpec $CustSpec} $IP.IPMode = "UseStaticIP" $IP.IPAddress = $VMIP $IP.SubnetMask = $VMSubnetMask $IP.DefaultGateway = $VMDefaultGW $IP.dns = $DNSServer1,$DNSServer2 Set-OSCustomizationNicMapping @IP ## Deploy VM $NewVM = New-VM -Name $VMname -Template $VCTemplate ` -VMHost $VMHost ` -Datastore $Datastore ` -OSCustomizationSpec $CustSpec ` -ErrorAction Stop ## Power UP VM to complete customisation Start-VM $VMname
1 Response
[…] discussed previously how I like to handle deploying appliances and VMs using PowerCLI and an existing VM template but I want to take that a stage further and create a template using […]