A while ago I mentioned in a post about creating a bespoke patching task sequence for a client who wanted to snap shot every VM before patching. To do this I created a silent install wrapper for PowerCLI using PowerShell.
Anyway, as per the request… here is the script I used to snapshot a VM before patching it during the Task sequence. It must be run as a command line step and using an account that has access to VSphere.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
######################################################################################################################## #Author: SCCMOG - Richie Schuster - SCCMOG.COM # #Date: 05/01/2017 # #Name: Auto Snapshot VM before Patching # #RunAs CMD: Powershell.exe -Executionpolicy Bypass -File PrePatchSnapShot.ps1 # #Description: This script has been created to auto snapshot a VM before patching by a task sequence from SCCM. # # The script must be run as a Command Line in the task sequence by an account with access to VSphere. # ######################################################################################################################## #Variables $VMname = "$env:COMPUTERNAME.YOURDOMAIN.COM" $VIserver = "YOURVISERVER.YOURDOMAIN.COM" #$Username = "YourDomain\admin-SCCMOG" #$Password = "CrazyPass" #Create Credential to pass securely #$Cred = New-Object System.Management.Automation.PSCredential($Username, (ConvertTo-SecureString $Password -AsPlainText -Force)) #Add the VMware snapin if not added If ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin VMware.VimAutomation.Core } #Connect to VIC001 If ($global:DefaultVIServer -eq $null ) { Connect-VIServer -Server $VIserver #-Credential $Cred -Verbose } ElseIF ($global:DefaultVIServer -ne $VIserver ) { #Disconnect all server connections Disconnect-VIServer -Server $global:DefaultVIServers -Force -Confirm:$false -WarningAction SilentlyContinue -ErrorAction SilentlyContinue | Out-Null #Re-connect to specific server Connect-VIServer -Server $VIserver } #Reset Connected Variable $Connected = $global:DefaultVIServer.Name -eq $VIserver If ($Connected -eq $true) { #Snapshot Time #Time Date for Snapshot name $Date = Get-Date $DateShort = $Date.ToShortDateString() -replace '[/]' $TimeShort = $Date.ToShortTimeString() $Snapshotname = "Before Patching $TimeShort $DateShort" #Take Snapshot New-Snapshot -VM $VMname -Name $Snapshotname -Description "This Snapshot was taken before patching the VM from the SCCM TaskSequence. $Date" -Verbose #Check Snapshot complete $SnapShots = Get-Snapshot -VM $VMname $Taken = $Snapshots.Name.Contains($Snapshotname) #If found Disconnect and Exit Script Success 0 If ($Taken -eq $true) { Disconnect-VIServer -Server $VIserver -Force -Confirm:$false -WarningAction SilentlyContinue -ErrorAction SilentlyContinue | Out-Null Exit 0 } #If Failed Exit Script 999 Failing the Tasksequence Else { Exit 999 } } Elseif ($Connected -eq $false) { Exit 911 } Else { Exit 912 } ###################################################################################################################### |