查询当前vmware启动的vm

写在前面

我在我的电脑上安装了vmware,并且设置了其他的一些,vm开机自启,但是当我再去打开vmware的时候,发现他们并没有在运行(起码GUI显示的是这样)。通过任务管理器,我发现到有名叫VMware Workstation VMX的进程在后台运行,通过显示任务管理器的命令行可以知道他们在后台启动了我的vm。但是这样来查看我的vm是否在运行太麻烦了,因为他们的命令行实在是太长了。像下面这些:

"D:\VMware\VMware Workstation\x64\vmware-vmx.exe" -s "sched.group=host/user" -# "product=1;name=VMware Workstation;version=17.6.4;buildnumber=24832109;licensename=VMware Workstation;licenseversion=17.0;" -@ "pipe=\\.\pipe\vmx2b32af270a10be3e;msgs=no" "F:\vm\DERP\Debian12-mini.vmx"

所以我写了一个ps1脚本来查看这个,以用来判断我的哪些vm在运行。

查看当前后台运行的vm

vmware-list.ps1

if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb runAs; exit}

Get-CimInstance Win32_Process -Filter "Name = 'vmware-vmx.exe'" |
ForEach-Object {
    $vm_pid = $_.ProcessId
    $cmd = $_.CommandLine
    Add-Content -Encoding UTF8 -Path "$env:USERPROFILE\Desktop\vmware-vmx-processes.txt" -Value "PID: $vm_pid"
    Add-Content -Encoding UTF8 -Path "$env:USERPROFILE\Desktop\vmware-vmx-processes.txt" -Value "CMD: $cmd"
    Add-Content -Encoding UTF8 -Path "$env:USERPROFILE\Desktop\vmware-vmx-processes.txt" -Value ""
}

这个会把正在运行的vm的PID和运行的命令写到桌面的mware-vmx-processes.txt文件中。可能还有其他更便捷的方式,不过目前这样对我来说是有一点点帮助。

posted @ 2025-07-31 09:24  雨中遐想  阅读(20)  评论(0)    收藏  举报