是否可以通过WMI创建具有指定名称的Hyper-V检查点?

时间:2019-05-17 20:17:54

标签: wmi hyper-v

我正在尝试找到一种通过WMI创建具有指定名称的Hyper-V检查点的方法。我知道可以创建快照,然后重命名快照,如this older question所示,但是理想情况下,我想使用要开始的名称创建检查点。

这是用于Windows Server 2016的Hyper-V。我已经尝试在传递给CreateSnapshot方法的Msvm_VirtualSystemSnapshotSettingData对象中将ElementName设置为想要的名称,但这似乎不起作用。

2 个答案:

答案 0 :(得分:1)

我这边有同样的问题。我使用Msvm_VirtualSystemSnapshotSettingData设置ElementName,但未设置名称。也许有一些重命名方法。

我使用PowerShell的以下代码创建了具有以下名称的快照:

$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"
    }
}

在PowerShell中运行正常。但这不适用于C ++代码。

答案 1 :(得分:0)

让它正常工作!!您可以查看我的完整PSscript:ScriptCode。您需要调用ModifySystemSettings方法来重命名:

 $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