获取当前正在运行的虚拟机(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的运行结果和对标准输出的解析

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值