您的位置:首页 > 其它

获取当前正在运行的虚拟机(Vmware Workstation),并对虚拟机进行控制

2016-06-30 16:08 567 查看
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的运行结果和对标准输出的解析

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: