2017-07-06 65 views
1

我有一個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窗口很快顯示並消失 –

回答

0

我發現了一個類似的帖子在這裏:Capturing standard out and error with Start-Process

無論出於何種原因PowerShell的解決方案不發送過程的結果向-OutVariable如我所料。

因此,我按照指示的頁面上的步驟創建了一個新的$pinfo = New-Object System.Diagnostics.ProcessStartInfo。 我設置了相應的屬性,然後使用System.Diagnostics.Process啓動了該過程,它允許我將結果輸出到字符串變量。