Hyper-V脚本无法识别远程虚拟机

时间:2017-07-11 16:33:15

标签: powershell scripting hyper-v hypervisor

我正在创建一个PowerShell脚本作为计划任务运行。它的目的是每周一在我们的每台虚拟机上创建一个快照。我为每个VM创建了一个简短的脚本。建立到本地Hyper-V服务器的远程会话后,它应该只需找到我们的虚拟机并创建一个快照。这是测试时的样子:

"jest": {
  "preset": "react-native",
  "testEnvironment": "jsdom"
}

正如您可能知道的那样,当我手动运行" get-vm"时,远程计算机就好了。但是当作为脚本运行时,它仍然会搜索我的工作站。以下是脚本的内容:

PS C:\Users\crhoden\Documents\Scripts\HyperV Snapshot Job> .\win7

Name                               State CPUUsage(%) MemoryAssigned(M) Uptime   Status             Version
----                               ----- ----------- ----------------- ------   ------             -------
Windows 10 Professional            Off   0           0                 00:00:00 Operating normally 7.0
Windows 7 Professional Pre-Alpha   Saved 0           0                 00:00:00 Operating normally 7.0
Windows 8.1 Professional Pre-Alpha Off   0           0                 00:00:00 Operating normally 7.0
Windows Server 2012 R2             Off   0           0                 00:00:00 Operating normally 7.0
Get-VMSnapshot : Hyper-V was unable to find a virtual machine with name "Windows 7 Pro".
At C:\Users\crhoden\Documents\Scripts\HyperV Snapshot Job\Win7.ps1:6 char:1
+ Get-VMSnapshot -VMName $vmname | Where-Object {$_.CreationTime -lt (G ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Get-VMSnapshot], VirtualizationException
    + FullyQualifiedErrorId : ObjectNotFound,Microsoft.HyperV.PowerShell.Commands.GetVMSnapshot

Checkpoint-VM : Hyper-V was unable to find a virtual machine with name "Windows 7 Pro".
At C:\Users\crhoden\Documents\Scripts\HyperV Snapshot Job\Win7.ps1:7 char:1
+ Checkpoint-VM -Name $vmname -SnapshotName "Weekly Snapshot $((Get-Dat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Checkpoint-VM], VirtualizationException
    + FullyQualifiedErrorId : ObjectNotFound,Microsoft.HyperV.PowerShell.Commands.CheckpointVM



[hypervserver]: PS C:\Users\crhoden\Documents> get-vm

Name            State   CPUUsage(%) MemoryAssigned(M) Uptime      Status
----            -----   ----------- ----------------- ------      ------
Windows 10 Pro  Running 1           1172              1.00:49:27  Operating normally
Windows 7 Pro   Running 0           1024              10:54:07    Operating normally
Windows 8 Pro   Running 0           1159              16.22:02:43 Operating normally
Windows 8.1 Pro Running 1           1716              16.21:43:03 Operating normally


[hypervserver]: PS C:\Users\crhoden\Documents>

我添加了start-sleep命令作为尝试修复,认为命令在建立连接之前被触发。没有这样的运气。踢球者是如果我逐行完成这个脚本,它的工作正常。任何帮助表示赞赏。编辑:当完整地粘贴脚本的内容时,它也是完美的。

1 个答案:

答案 0 :(得分:1)

Enter-PSSession仅用于交互式会话,因此以交互方式工作或粘贴到会话中确实有效,但不是在本质上不是交互式的脚本中。

你可以

  • 远程执行脚本或
  • 使用-ComputerName
  • Get-VMSnapShot参数

正如您自己发现的,使用过的cmdlet必须与Hyper-Vserver版本匹配。