1. 程式人生 > >獲取當前正在執行的虛擬機器(Vmware Workstation),並對虛擬機器進行控制

獲取當前正在執行的虛擬機器(Vmware Workstation),並對虛擬機器進行控制

vmware workstation 提供了一個命令列工具進行虛擬機器管理

C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe


用法如下:

POWER COMMANDS           PARAMETERS           DESCRIPTION
--------------           ----------           -----------
start                    Path to vmx file     Start a VM or Team
                         [gui|nogui]

stop                     Path to vmx file     Stop a VM or Team
                         [hard|soft]

reset                    Path to vmx file     Reset a VM or Team
                         [hard|soft]

suspend                  Path to vmx file     Suspend a VM or Team
                         [hard|soft]

pause                    Path to vmx file     Pause a VM

unpause                  Path to vmx file     Unpause a VM
GENERAL COMMANDS         PARAMETERS           DESCRIPTION
----------------         ----------           -----------
list                                          List all running VMs

upgradevm                Path to vmx file     Upgrade VM file format, virtual hw

installTools             Path to vmx file     Install Tools in Guest

checkToolsState          Path to vmx file     Check the current Tools state

register                 Path to vmx file     Register a VM

unregister               Path to vmx file     Unregister a VM

listRegisteredVM                              List registered VMs

deleteVM                 Path to vmx file     Delete a VM

clone                    Path to vmx file     Create a copy of the VM
                         Path to destination vmx file
                         full|linked
                         [-snapshot=Snapshot Name]
                         [-cloneName=Name]

更多的引數可以直接執行vmrun命令檢視(不帶引數)

.Net中,通過process.start來執行vmrun(帶list引數) ,獲取該命令的標準輸出重定向即可獲得當前正在執行的vmware虛擬機器列表,然後可根據不同需求(stop、suspend、pause等),帶上對應的引數進行虛擬機器操作。

        Dim strPathShutdown As String = ConfigurationManager.AppSettings("shutdown"),
            strPathVmrun As String = ConfigurationManager.AppSettings("vmrun"),
            proc As New Process(),
            intVM As Integer,
            listVM As New List(Of String)
        proc.StartInfo = New ProcessStartInfo(strPathVmrun, "list")
        proc.StartInfo.RedirectStandardOutput = True
        proc.StartInfo.UseShellExecute = False
        proc.Start()
        Dim out As String = proc.StandardOutput.ReadToEnd()
        MsgBox(out)
        listVM = Split(out, vbCrLf).ToList
        listVM.RemoveAt(0)
        intVM = New System.Text.RegularExpressions.Regex("\d+", RegexOptions.IgnoreCase).Match(out).Value
        For Each vm As String In listVM
            proc.StartInfo.Arguments = String.Format("suspend ""{0}""", vm)
            proc.Start()
        Next


vmrun的執行結果和對標準輸出的解析