我有一个PowerShell 2.0脚本,用于在windows主机环境中使用vmrun克隆目标vmware工作站客户。克隆虚拟机的代码正确执行。
我现在试图对这个脚本进行扩展,以自动化更多的进程,例如,检查虚拟机是否正在运行,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命令行文档可以在这里找到:"command.pdf“
谢谢。
发布于 2017-07-06 14:32:27
我在这里发现了一个类似的帖子:Capturing standard out and error with Start-Process
出于任何原因,powershell解决方案没有像我预期的那样将进程的结果发送到-OutVariable。
因此,我按照页面上的步骤创建了一个新的$pinfo = New-Object System.Diagnostics.ProcessStartInfo。我设置了相应的属性,然后使用System.Diagnostics.Process启动进程,它允许我将结果输出到字符串变量。
https://stackoverflow.com/questions/44949604
复制相似问题