Deleting a snapshot–via PowerShell [Hyper-V]
Here is a script that will allow you to delete a single snapshot from a Hyper-V virtual machine:
# Function for handling WMI jobs / return values
Function ProcessResult($result, $successString, $failureString)
{
#Return success if the return value is "0"
if ($result.ReturnValue -eq 0)
{write-host $successString}
#If the return value is not "0" or "4096" then the operation failed
ElseIf ($result.ReturnValue -ne 4096)
{write-host $failureString " Error value:" $result.ReturnValue}
Else
{#Get the job object
$job=[WMI]$result.job
#Provide updates if the jobstate is "3" (starting) or "4" (running)
while ($job.JobState -eq 3 -or $job.JobState -eq 4)
{write-host $job.PercentComplete "% complete"
start-sleep 1
#Refresh the job object
$job=[WMI]$result.job}
#A jobstate of "7" means success
if ($job.JobState -eq 7)
{write-host $successString
return $true}
Else
{write-host $failureString
write-host "ErrorCode:" $job.ErrorCode
write-host "ErrorDescription" $job.ErrorDescription
return $false}
}
}
# Prompt for the Hyper-V Server to use
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
# Prompt for the virtual machine to use
$VMName = Read-Host "Specify the name of the virtual machine"
# Prompt for the name of the snapshot to delete
$SnapshotName = Read-Host "Specify the name of the snapshot to delete"
# Get the management service
$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer
# Get the virtual machine object
$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $HyperVServer
# Find the snapshot that we want to delete
$Snapshot = gwmi -Namespace root\virtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData" | where {$_.ElementName -eq $SnapshotName} | select -first 1
# Delete the snapshot
$result = $VMMS.RemoveVirtualSystemSnapshot($Snapshot)
# Check to make sure we succeeded
$deleteSucceeded = ProcessResult $result "Deleted snapshot." "Failed to delete snapshot."
Cheers,
Ben
Comments
- Anonymous
March 17, 2013
The comment has been removed - Anonymous
April 01, 2013
Your error in the WMI script is in this portion of the pipeline: gwmi -Namespace rootvirtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData" The assumption of this command is where you are running the script. It assumes that you are running the script locally on the Hyper-V Server itself, in spite of previously asking you and the previous lines targeting a remote system. Try this: $Snapshot = gwmi -Namespace rootvirtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData" -computername $HyperVServer | where {$_.ElementName -eq $SnapshotName} | select -first 1 Notice that I added the -computername