MKUMAR2
Contributor
Contributor

template patching automation in vCenter 7

Hello Experts 

All these days (when vCenter 6.x), i was using the below script to auto update windows templates in my VC and manually pushing to content library. But now I upgraded vCenter to vSphere 7 today. Here the template update is different like check in and check out for modification. Can anyone help me with updating scripts that work in vCenter 7 

Script that I was using 

-------------------------------------------------------------------------------------------------------------------------------

# Connect to vCenter

Connect-VIServer "VcENTER"

# Convert template to VM

Set-Template -Template WIN2019 -ToVM -Confirm:$false -RunAsync

Start-sleep -s 20

#Start VM - I've seen some converted templates that prompt with the VMQuestion, so adding the command to answer with the default option was my response to it.
#Get-VM -name WIN2019 | Start-VM -VM WIN2019 -Confirm -RunAsync
Start-VM -VM WIN2019 | Get-VMQuestion | Set-VMQuestion -DefaultOption -Confirm:$false

Start-sleep -s 60

#Create variables for Guest OS credentials - This is needed for the Invoke-VMScript cmdlet to be able to execute actions inside the Guest.

#If you don't want to enter the Guest OS local administrator password as clear text in the script, follow the steps on following link to create a file and store it as an encrypted string: Using PowerShell credentials without being prompted for a password - Stack Overflow

$Username = "administrator"

$OSPwd = cat C:\Pwd.txt | convertto-securestring

$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $OSPwd

#The following is the cmdlet that will invoke the Get-WUInstall inside the GuestVM to install all available Windows updates; optionally results can be exported to a log file to see the patches installed and related results.

Invoke-VMScript -ScriptType PowerShell -ScriptText "Install-WindowsUpdate -AcceptAll -AutoReboot" -VM WIN2019 -GuestCredential $Cred | Out-File -FilePath "C:\Templatelogs\$(get-date -f yyyy-MM-dd)-WIN2019.log" -Width 4000 -Append

Start-sleep -s 200

#Optionally restart VMGuest one more time in case Windows Update requires it and for whatever reason the –AutoReboot switch didn’t complete it.

Restart-VMGuest -VM WIN2019 -Confirm:$false
Start-sleep -s 200
#On a separate scheduled script or after a desired wait period, Shutdown the server and convert it back to Template.

#Shutdown-VMGuest –VM WIN2019 -Confirm:$false –RunAsync
Get-VM WIN2019 | Stop-VMGuest -Confirm:$false
Start-sleep -s 150

#Set-VM –VM WIN2019 -ToTemplate -Confirm:$false

 

Labels (1)
Tags (2)
0 Kudos
0 Replies