使用Powershell实现虚拟机自动化的间歇性错误

时间:2013-06-13 14:24:28

标签: powershell automation virtual-machine

在虚拟机上自动执行某些测试时,我遇到了Powershell的间歇性问题。

方案和设置如下:

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

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

问题如下:

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

要恢复快照我正在使用“PowerShell Management Library for Hyper-V”:( http://pshyperv.codeplex.com/releases

以下是一些代码:

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

Function ApplySnapshot
{
  LogAction "Starting apply snapshot"
  LogAction $("Restoring snapshot {0}" -f $ss)
  #Stop a running VM, restore snapshot, start it up and connect to it
  $vmstate = get-vmstate $vmname
  $vmstate = $vmstate.EnabledState
  if ($vmstate -ne "Stopped")
  {
    stop-vm $vmname -force
    Start-Sleep -Second 15
  }
  get-vmsnapshot $vmname | where {$_.ElementName -eq $ss} | Restore-VMSnapshot -force
  start-vm $vmname -force
  Start-Sleep -Second 20
  LogAction $("Snapshot {0} restored" -f $ss)
  LogAction "End apply snapshot"
}

Function CopyFiles
{
  LogAction "Start copy installation files"
  $from = "\\server\folderx"
  $to = "\\" + $hostname + "\C$\test"
  Enter-PSSession -ComputerName $hostname -Credential $cred
  Copy-Item $from $to -Recurse
  LogAction "End copy installation files"
}

Function CreateSchedule ($hn, $tn, $tr, $sd, $st, $un, $pw)
{
  LogAction "Starting create schedule"
  Invoke-Command -ComputerName $hn -ScriptBlock {
    param($hn, $tn, $tr, $sd, $st, $un, $pw)
    Write-Host $("Host name: [{0}]" -f $hn); 
    $cmd = $("schtasks.exe /create /S ""{0}"" /tn ""{1}"" /tr ""{2}"" /sc once /sd {3} /st {4} /ru ""{5}"" /rp ""{6}"" /rl highest /V1" -f $hn, $tn, $tr, $sd, $st, $un, $pw);
  Invoke-Expression $cmd;
  } -ArgumentList @($hn, $tn, $tr, $sd, $st, $un, $pw)
  LogAction "End create schedule"
}

...setting variables etc...

ApplySnapshot
CopyFiles
CreateSchedule -hn $hostname -tn $taskname -tr $taskrun -sd $setdate -st $settime -un $username -pw $password

批处理文件:

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

示例输出:

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

因此,在此示例中,已成功应用快照(尽管有警告)。文件复制到虚拟机后出现“Enter-PSSession”错误。

作为测试,我在不同的服务器(也运行HyperV等等)上尝试了这个,我发现我仍然得到初始错误(在文件复制阶段之后)但是我没有得到创建计划任务的错误

我搜索“连接到远程服务器”上的信息的所有努力都失败了,并显示以下错误消息:WinRM客户端无法在指定的时间内完成操作。“错误似乎说”确保机器已设置为远程使用“;我知道这是因为有时它会起作用,如果我自己只运行一个“Enter-PSSession”命令,我就可以连接。

服务器和虚拟机位于同一个域中。

我知道这里有很多内容,但我真的很感激如何解决/解决这个问题。

谢谢

1 个答案:

答案 0 :(得分:0)

在进行连接尝试时,可能目标并不总是向上。