【问题标题】:Automating vmrun with PowerShell使用 PowerShell 自动化 vmrun
【发布时间】:2017-12-10 12:03:08
【问题描述】:

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

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

在 Windows 命令提示符下,当我运行 vmrun list 时,它会返回以下内容:

正在运行的虚拟机总数: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

谢谢。

【问题讨论】:

  • 如果您使用(在 PowerShell 提示符下,在 vmrun 目录中):.\vmrun.exe /?.\vmrun.exe --help 是否列出了参数选项?
  • 我可以在 cmd 提示符下。我无法从 Powershell 进行验证,因为 cmd 窗口会快速显示并消失

标签: powershell vmware vmrun


【解决方案1】:

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

无论出于何种原因,powershell 解决方案都不会像我预期的那样将进程结果发送到 -OutVariable。

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

【讨论】:

    猜你喜欢
    • 2011-02-07
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 2016-05-06
    • 2010-10-10
    相关资源
    最近更新 更多