【问题标题】:Remote PowerShell script doesn't apply Hyper-V Snapshot远程 PowerShell 脚本不应用 Hyper-V 快照
【发布时间】:2013-08-09 01:58:19
【问题描述】:

我正在尝试通过 PowerShell 远程应用 Hyper-V 快照。为此,我正在关注Ben Armstrong's guide

顺便说一句,我使用的是 Server 2008 R2。

简而言之:

连接到远程服务器:

$password = ConvertTo-SecureString "password" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("domain\user", $password)
Enter-PSSession -Computer computerName -Credential $cred

这很好用。我已经通过在机器上创建一个文件夹来测试这个远程连接。

应用快照:

$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 apply
$SnapshotName = Read-Host "Specify the name of the snapshot to apply"

# 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 apply
$Snapshot = gwmi -Namespace root\virtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData" | where {$_.ElementName -eq $SnapshotName} | select -first 1

# Apply the snapshot
$VMMS.ApplyVirtualSystemSnapshot($VM, $Snapshot)

我一个接一个地执行这些,找到虚拟机和快照就好了,但是当我执行最后一个命令时,我的快照没有应用。它也不会失败,没有错误消息。

但是,在执行最终命令后,我确实得到了以下信息:

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     : 
__DYNASTY        : __PARAMETERS
__RELPATH        : 
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         : 
__NAMESPACE      : 
__PATH           : 
ReturnValue      : 32775

这个返回值根据this MSDN page表示Invalid state for this operation (32775)。那是什么意思?如何解决此问题以实际应用快照?

VM 当前处于运行状态,因此我在关闭 VM 并正确应用快照的情况下再次尝试。

如何强制快照在虚拟机处于运行状态时应用?

【问题讨论】:

  • 您是否尝试过通过 Hyper-V UI“手动”拍摄快照? “此操作的无效状态”似乎意味着它所说的 - 例如如果正在进行合并,我已经看到它会发生(但在你的情况下不可能如此)。
  • @Al-Muhandis 拍摄快照没有问题,这篇文章不涉及拍摄快照。问题在于 VM 处于运行状态时通过 PowerShell 应用快照。
  • 对,对不起。您是否尝试过通过 UI 应用它?
  • @Al-Muhandis 是的,我可以在虚拟机处于运行状态时手动应用快照,没问题。当我尝试通过 WMI/PowerShell 做同样的事情时,我只会得到 Invalid state for this operation

标签: powershell windows-server-2008 hyper-v


【解决方案1】:

文章明确指出:

需要注意的一点是,如果在您尝试应用快照时虚拟机正在运行,则此操作将失败 - 需要先关闭虚拟机或进入保存状态。

ApplyVirtualSystemSnapshot 不能与正在运行的虚拟机远程工作

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多