使用PowerShell自动化vmrun

问题描述:

我有一个PowerShell 2.0脚本,用于在Windows主机环境中使用vmrun克隆目标vmware工作站guest虚拟机。 克隆虚拟机的代码正确执行。使用PowerShell自动化vmrun

我现在试图扩展此脚本以自动执行更多的过程,例如,检查虚拟机是否正在运行,如果运行虚拟机vmrun list,并停止虚拟机vmrun stop [pathToVMXfile]

随着Windows命令提示,当我运行vmrun list,它返回下列:

总运行的VM:2
d:\ [路径] \ [名称] .VMX
E:\ [路径] \ [名称] .vmx

当我在PowerShell中尝试以下操作时,什么也不返回。这是我迄今为止所尝试的。我期待一个数组返回时,我从命令提示符运行时看到的值。

$vmwareRun = "C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" 

#Original test, also tried with single quotes and no quotes, 
#also tried quoting the -filePath 
Start-Process $vmwareRun -ArgumentList "list" 

#This gives a handle to the process but no return value 
$myProcess = Start-Process $vmwareRun -ArgumentList "list" -PassThru 

#$t is empty 
Start-Process $vmwareRun -ArgumentList "list" -OutVariable $t 

#$t is empty, clone command requires the -T ws parameters 
Start-Process $vmwareRun -ArgumentList "-T ws list" -OutVariable $t 

#RedirectStandardOutput creates a file with the expected output, $t is empty 
Start-Process -FilePath "$vmwareRun" -ArgumentList $argList -OutVariable $t -RedirectStandardError "C:\testError.log" -RedirectStandardOutput C:\testOut.log" 

无论我尝试什么,我都没有得到任何输出。我错过了什么? 注意:Vmrun命令行文档在这里找到:“https://www.vmware.com/support/developer/vix-api/vix112_vmrun_command.pdf

谢谢。

+0

如果使用(在PowerShell提示符下,在vmrun目录中):'。\ vmrun.exe /?'或'。\ vmrun.exe --help'是列出的参数选项? – Jelphy

+0

我可以从cmd提示。我无法从Powershell验证,因为cmd窗口很快显示并消失 –

我发现了一个类似的帖子在这里:Capturing standard out and error with Start-Process

无论出于何种原因PowerShell的解决方案不发送过程的结果向-OutVariable如我所料。

因此,我按照指示的页面上的步骤创建了一个新的$pinfo = New-Object System.Diagnostics.ProcessStartInfo。 我设置了相应的属性,然后使用System.Diagnostics.Process启动了该过程,它允许我将结果输出到字符串变量。