簡體   English   中英

使用PowerShell自動化vmrun

[英]Automating vmrun with PowerShell

我有一個PowerShell 2.0腳本,可用於在Windows主機環境中使用vmrun克隆目標vmware工作站來賓。 克隆虛擬機的代碼可以正確執行。

我現在正在嘗試擴展此腳本以自動執行更多過程,例如,檢查虛擬機是否正在運行, vmrun list以及停止虛擬機, vmrun stop [pathToVMXfile]如果正在運行,請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

謝謝。

我在這里找到了類似的帖子: 使用Start-Process捕獲標准輸出和錯誤

無論出於何種原因,powershell解決方案都不會像我期望的那樣將過程結果發送到-OutVariable。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM