使用Powershell自动化虚拟机时出现间歇性错误

使用Powershell自动化虚拟机时出现间歇性错误,powershell,automation,virtual-machine,Powershell,Automation,Virtual Machine,在虚拟机上自动执行某些测试时,Powershell会出现间歇性问题 场景和设置如下所示: 运行HyperV的服务器 一个具有多个快照的虚拟机 Powershell脚本,用于恢复给定快照、复制文件、运行测试和检索日志文件 使用不同参数多次调用Powershell脚本的批处理文件 批处理文件参数指定要使用的快照、要运行的测试等 问题如下: 我可以运行批处理,一些测试将无法复制文件/无法创建计划任务/无法检索日志文件/等等。如果任何(或所有)部分失败,情况会有所不同。有些测试将完全有效。如果我重新运行

在虚拟机上自动执行某些测试时,Powershell会出现间歇性问题

场景和设置如下所示:

运行HyperV的服务器
一个具有多个快照的虚拟机
Powershell脚本,用于恢复给定快照、复制文件、运行测试和检索日志文件
使用不同参数多次调用Powershell脚本的批处理文件

批处理文件参数指定要使用的快照、要运行的测试等

问题如下:

我可以运行批处理,一些测试将无法复制文件/无法创建计划任务/无法检索日志文件/等等。如果任何(或所有)部分失败,情况会有所不同。有些测试将完全有效。如果我重新运行相同的批处理文件,那么一些测试可能会失败,而另一些测试会正常工作;在哪个失败和哪个运行方面没有一致性。有时我有两个相邻的测试使用同一个快照,一个正常,一个不正常(见下面的错误)

要从以下位置还原快照,我正在使用“Hyper-V的PowerShell管理库:()

下面是一些代码:

Powershell(减去一些函数/变量声明/读取xml配置文件/读取和验证命令行输入/以及其他不相关的部分):

批处理文件:

PowerShell -Command "& C:\Auto.ps1" <...params...>
PowerShell -Command "& C:\Auto.ps1" <...params...>
PowerShell -Command "& C:\Auto.ps1" <...params...>
PowerShell -Command "& C:\Auto.ps1" <...params...>
pause
PowerShell-Command“&C:\Auto.ps1”
PowerShell-命令“&C:\Auto.ps1”
PowerShell-命令“&C:\Auto.ps1”
PowerShell-命令“&C:\Auto.ps1”
暂停
示例输出:

C:\Auto>PowerShell -Command "& C:\Auto.ps1" <...params...>
WARNING: The job to Change state of VM TestVM to Stopped  is still
running in the background.
 You can check its progress with Test-wmiJob or Test-wmiJob -statusOnly using
the following job id:
\\Server\root\virtualization:Msvm_ConcreteJob.InstanceID="A207CEBA-F582-4A42-
BCDE-3312C7FB6DCC"
JobStarted
WARNING: The job to Change state of VM TestVM to Running  is still
running in the background.
 You can check its progress with Test-wmiJob or Test-wmiJob -statusOnly using
the following job id:
\\Server\root\virtualization:Msvm_ConcreteJob.InstanceID="42C31CEF-00E2-40A7-
AF70-578B0B91B05D"
JobStarted

Enter-PSSession : Connecting to remote server failed with the following error m
essage : The WinRM client cannot complete the operation within the time specifi
ed. Check if the machine name is valid and is reachable over the network and fi
rewall exception for Windows Remote Management service is enabled. For more inf
ormation, see the about_Remote_Troubleshooting Help topic.
At C:\Auto.ps1:192 char:18
+   Enter-PSSession <<<<  -ComputerName $hostname -Credential $cred
    + CategoryInfo          : InvalidArgument: (TestVM:String) [Enter-PSS
    ession], PSRemotingTransportException
    + FullyQualifiedErrorId : CreateRemoteRunspaceFailed

[TestVM] Connecting to remote server failed with the following error messa
ge : The WinRM client cannot complete the operation within the time specified.
Check if the machine name is valid and is reachable over the network and firewa
ll exception for Windows Remote Management service is enabled. For more informa
tion, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (:) [], PSRemotingTransportException
    + FullyQualifiedErrorId : PSSessionStateBroken
C:\Auto>PowerShell-Command“&C:\Auto.ps1”
警告:将VM TestVM的状态更改为“已停止”的作业仍在运行
在后台运行。
您可以使用Test-wmiJob或Test-wmiJob-statusOnly检查其进度
以下作业id:
\\Server\root\virtualization:Msvm_ConcreteJob.InstanceID=“A207CEBA-F582-4A42-
BCDE-3312C7FB6DCC“
工作开始
警告:将VM TestVM的状态更改为正在运行的作业仍在运行
在后台运行。
您可以使用Test-wmiJob或Test-wmiJob-statusOnly检查其进度
以下作业id:
\\Server\root\virtualization:Msvm_ConcreteJob.InstanceID=“42C31CEF-00E2-40A7-
AF70-578B0B91B05D“
工作开始
输入PSSession:连接到远程服务器失败,出现以下错误m
消息:WinRM客户端无法在指定的时间内完成操作
ed.检查机器名是否有效,是否可以通过网络和fi访问
已启用Windows远程管理服务的rewall异常。更多信息
有关配置,请参阅关于远程故障排除帮助主题。
在C:\Auto.ps1:192字符:18

+输入PSSession可能在尝试连接时目标并不总是向上。

好的,我现在觉得有点傻。今天早上,我注意到保存虚拟机快照的驱动器的磁盘空间非常少(大约15Mb可用空间)。我花了一些时间清理驱动器,并重新运行了几次测试,通常情况下,它们似乎没有出现错误(或者更确切地说,没有那么多错误)。嗯,在运行了几次成功的测试之后,问题又出现了。所以磁盘空间可能不是原因。好吧,我从头创建了一个全新的虚拟机,并重试了测试。到目前为止,经过100多次尝试,我还没有出现上述任何问题。我想我必须把这归因于一个损坏的虚拟机。
C:\Auto>PowerShell -Command "& C:\Auto.ps1" <...params...>
WARNING: The job to Change state of VM TestVM to Stopped  is still
running in the background.
 You can check its progress with Test-wmiJob or Test-wmiJob -statusOnly using
the following job id:
\\Server\root\virtualization:Msvm_ConcreteJob.InstanceID="A207CEBA-F582-4A42-
BCDE-3312C7FB6DCC"
JobStarted
WARNING: The job to Change state of VM TestVM to Running  is still
running in the background.
 You can check its progress with Test-wmiJob or Test-wmiJob -statusOnly using
the following job id:
\\Server\root\virtualization:Msvm_ConcreteJob.InstanceID="42C31CEF-00E2-40A7-
AF70-578B0B91B05D"
JobStarted

Enter-PSSession : Connecting to remote server failed with the following error m
essage : The WinRM client cannot complete the operation within the time specifi
ed. Check if the machine name is valid and is reachable over the network and fi
rewall exception for Windows Remote Management service is enabled. For more inf
ormation, see the about_Remote_Troubleshooting Help topic.
At C:\Auto.ps1:192 char:18
+   Enter-PSSession <<<<  -ComputerName $hostname -Credential $cred
    + CategoryInfo          : InvalidArgument: (TestVM:String) [Enter-PSS
    ession], PSRemotingTransportException
    + FullyQualifiedErrorId : CreateRemoteRunspaceFailed

[TestVM] Connecting to remote server failed with the following error messa
ge : The WinRM client cannot complete the operation within the time specified.
Check if the machine name is valid and is reachable over the network and firewa
ll exception for Windows Remote Management service is enabled. For more informa
tion, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (:) [], PSRemotingTransportException
    + FullyQualifiedErrorId : PSSessionStateBroken