qoheleth
qoheleth

Reputation: 121

Is there a way to create a Hyper-V checkpoint with a specified name through WMI?

I'm trying to find a way to create a Hyper-V checkpoint with a specified name through WMI. I am aware that it is possible to create a snapshot, and then rename it, as seen in this older question, but ideally I would like to create the checkpoint with the name I want to begin with.

This is for Hyper-V for Windows Server 2016. I have already tried setting ElementName to the name I want in the Msvm_VirtualSystemSnapshotSettingData object passed to the CreateSnapshot method, but this doesn't appear to work.

Upvotes: 2

Views: 408

Answers (2)

Victor
Victor

Reputation: 21

Got it working!! You can review my full PSscript: ScriptCode Take a look. You need to call ModifySystemSettings method to rename:

 $vm = Get-WmiObject -Namespace $Namespace -ComputerName $ComputerName -Query "select * from msvm_computersystem where elementname='$VMName'"
if($vm -eq $null){
    throw "Failed to get VM with display name '$VMName'"
}

$Msvm_VirtualSystemSnapshotService = Get-WmiObject -Namespace $Namespace -ComputerName $ComputerName -Class Msvm_VirtualSystemSnapshotService
if($Msvm_VirtualSystemSnapshotService -eq $null)
{
    throw "Failed to get Msvm_VirtualSystemSnapshotService instance"
}
[WMI]$Msvm_VirtualSystemSnapshotSettingData = ([WMIClass]"\\.\root\virtualization\v2:Msvm_VirtualSystemSnapshotSettingData").CreateInstance()
if($Msvm_VirtualSystemSnapshotSettingData -eq $null)
{
    throw "Failed to get Msvm_VirtualSystemSnapshotSettingData instance"
}
$Msvm_VirtualSystemSnapshotSettingData.ConsistencyLevel = 1
$Msvm_VirtualSystemSnapshotSettingData.IgnoreNonSnapshottableDisks = $true
$Msvm_VirtualSystemSnapshotSettingData.ElementName = $SnapshotName
$Msvm_VirtualSystemSnapshotSettingData.Description = $SnapshotDescription
$Msvm_VirtualSystemSnapshotSettingData

[System.Management.ManagementBaseObject]$result = $Msvm_VirtualSystemSnapshotService.CreateSnapshot(
    $vm,
    $Msvm_VirtualSystemSnapshotSettingData.GetText(2),
    32768)
[int]$res = Job-Observation -WMIJobResult $result -JobAction "Creating snapshot" -JobProgressCaption "Create snapshot"
[WMI]$snapshot = (([WMI]$result.Job).GetRelated("Msvm_VirtualSystemSettingData") | % {$_})
$Msvm_VirtualSystemManagementService = Get-WmiObject -Namespace $Namespace -ComputerName $ComputerName -Class Msvm_VirtualSystemManagementService
if($Msvm_VirtualSystemManagementService -eq $null)
{
    throw "Failed to get Msvm_VirtualSystemManagementService instance"
}

$snapshot.ElementName = $SnapshotName
$snapshot.Description = $SnapshotDescription

[System.Management.ManagementBaseObject]$result = $Msvm_VirtualSystemManagementService.ModifySystemSettings($snapshot.GetText(2))
[int]$res = Job-Observation -WMIJobResult $result -JobAction "Renaming snapshot" -JobProgressCaption "Rename snapshot"


$snapshot

Upvotes: 0

Victor
Victor

Reputation: 21

Same problem on my side. I used Msvm_VirtualSystemSnapshotSettingData to set ElementName but it do not set name. Maybe there is some method to rename.

I used this code of PowerShell to create snapshot with name:

$vm = Get-WmiObject -Namespace $Namespace -ComputerName $ComputerName -Query "select * from msvm_computersystem where elementname='$VMName'"
if($vm -eq $null){
    throw "Failed to get VM with display name '$VMName'"
}
$cms = Get-WmiObject -Namespace $Namespace -ComputerName $ComputerName -Class Msvm_CollectionManagementService
if($cms -eq $null){
    throw "Failed to get instance Msvm_CollectionManagementService"
}

[System.Management.ManagementBaseObject]$result = $cms.DefineCollection($snapshotname, $null, 0)
[int]$res = Job-Observation -WMIJobResult $result -JobAction "Define collection" -JobProgressCaption "Define collection"

if($res -eq 0)
{
    $coll = [wmi]$result.DefinedCollection
    $coll.Description = $SnapshotDescription
    $result = $cms.AddMember($vm, $coll)
    $res = Job-Observation -WMIJobResult $result -JobAction "Add collection as member" -JobProgressCaption "Add collection as member"
    if($res -eq 0)
    {
        $csss = Get-WmiObject -Namespace $Namespace -ComputerName $ComputerName -Class Msvm_CollectionSnapshotService                                         
        $result = $csss.CreateSnapshot([string]$coll, $null, 32768, $null)
        $res = Job-Observation -WMIJobResult $result -JobAction "Creating shapshot" -JobProgressCaption "Create snapshot"
        $result = $cms.RenameCollection($snapshotname, [string]$coll)
        $res = Job-Observation -WMIJobResult $result -JobAction "Set snaphost name" -JobProgressCaption "Set snapshot name"
    }
}

It works fine from PowerShell. But it doesn't work from C++ code.

Upvotes: 1

Related Questions