How to restore VM Snapshot remotely using PowerShell

This article shows how to restore VM Snapshot from a remote PC through PowerShell Remote Session. PS Credentials were saved to an XML so that password was securely encrypted and decrypted during the run.

Store PS Credential to XML

Powershell Credential will be stored in an XML which can be retrieved for PS session credentials.

$credxml = "mycredential.xml"

Get-Credential | Export-Clixml $credxml

Run:

After typing the password, the encrypted credential will be stored in XML.

Restore VM Remotely

PowerShell Remote Management is to be enabled before creating a PS session, and Invoke-Command runs the defined script block with the session.

$vmserver = "YOUR_Remote_TargetVM"

$snapshot = 'SnapshotName_YOUR_Remote_TargetVM'

$vmhost = "YOUR_VMHOST_for_TargetVM"

$credxml = "mycredential.xml" # saved before


# enable Remote PowerShell Management

Enable-PSRemoting -Force
 
$cred = Import-Clixml $credxml


# define the remote session
$session = New-PSSession -ComputerName $vmhost -credential $cred

$scriptBlock = {
 

                param( [String] $vmname, [String] $snapshotname)
            
                Restore-VMSnapshot –Name $snapshotname –VMName $vmname -confirm:$false
            
                Start-VM -Name $vmname
            }


# run the script block
Invoke-Command -Session $session -ScriptBlock $scriptBlock -ArgumentList $(,$vmserver,$snapshot)

More articles to read:

How to Install MSI Remotely Using PowerShell

How to download files from the Web using PowerShell

Useful List of PowerShell Commands

Leave a Comment