VMware Workstation优化控制批处理 (Control Batch)

This is a simple batch script work with Microsoft devcon utility to disable and enable VMware services and virtual devices.

Download link is provided at the end of this post, batch script and devcon included.

众所周知, VMware Workstation(下文均简称为VM)功能上十分强大, 性能优异, 可谓是Windows平台虚拟机软件中的老大哥. 只是, 同样霸气的是它的诸多后台服务和虚拟设备, 在最新版中, VM的后台服务就达6项之多, 另外还有好几项的虚拟设备(主要是虚拟网卡). 对于不是经常使用VM或是像我这样有”洁癖”的人来说, 看着就觉得不爽. 于是, 就用批处理与Microsoft官方的devcon联合调教它了一番.

运行环境: >= Windows XP

调教目标:

1. 在不用VM时, 所有与之相关的服务与虚拟设备均处于停止或禁用状态.

2. 需要使用VM时, 自动启动所有相关服务与虚拟设备.

调教过程: (不感兴趣的可直接拉至本文末尾直接下载)

使用批处理+devcon解决. 其中批处理用于流程控制及后台服务控制, devcon用于在控制设备状态.

a. 启用VM, 运行后会自动启动VM: (vm_enable.cmd)

:: written by Delta on 4/7/2012
:: update on 08/09/2015 with admin permission check

@ECHO OFF&PUSHD %~DP0 &TITLE Enable VMware
Rd "%WinDir%\system32\test_permissions" >NUL 2>NUL
Md "%WinDir%\System32\test_permissions" 2>NUL||(Echo Please run as administrator.&&Pause >nul&&Exit)
Rd "%WinDir%\System32\test_permissions" 2>NUL
SetLocal EnableDelayedExpansion

if %PROCESSOR_ARCHITECTURE%==AMD64 set arch=x64
if %PROCESSOR_ARCHITECTURE%==IA64 set arch=ia64
if %PROCESSOR_ARCHITECTURE%==x86 set arch=x86

echo.
echo ...Enabling VMware Virtual Devices...
echo.
%~sdp0\devcon\%arch%\devcon.exe enable *vm*
echo.
echo ...Complete!

echo.
echo ...Starting VMware Workstation Services...
echo.
net start "VMware DHCP Service"
net start "VMware NAT Service"
net start "VMware USB Arbitration Service"
net start "VMware Authorization Service"
:: net start "VMware Workstation Server"
echo ...Complete!

echo.
echo ...Starting VMware Workstation...
echo.
start %SystemDrive%\"Program Files (x86)"\VMware\"VMware Workstation"\vmware.exe

ping -n 5 127.1>nul

b. 禁用VM, 在使用完VM后运行: (vm_disable.cmd)

:: written by Delta on 4/7/2012
:: update on 08/09/2015 with admin permission check

@ECHO OFF&PUSHD %~DP0 &TITLE Disable VMware
Rd "%WinDir%\system32\test_permissions" >NUL 2>NUL
Md "%WinDir%\System32\test_permissions" 2>NUL||(Echo Please run as administrator.&&Pause >nul&&Exit)
Rd "%WinDir%\System32\test_permissions" 2>NUL
SetLocal EnableDelayedExpansion

if %PROCESSOR_ARCHITECTURE%==AMD64 set arch=x64
if %PROCESSOR_ARCHITECTURE%==IA64 set arch=ia64
if %PROCESSOR_ARCHITECTURE%==x86 set arch=x86

echo ...Setting VMware Workstation Services...
echo.
sc config VMAuthdService start= demand
sc config VMnetDHCP start= demand
sc config "VMware NAT Service" start= demand
sc config VMUSBArbService start= demand
sc config VMwareHostd start= demand
echo.
echo ...Complete!

echo ...Disabling VMware Virtual Devices...
echo.
%~sdp0\devcon\%arch%\devcon.exe disable *vm*
echo.
echo ...Complete!

echo.
echo ...Stopping VMware Workstation Services...
echo.
net stop "VMware Authorization Service"
net stop "VMware DHCP Service"
net stop "VMware NAT Service"
net stop "VMware USB Arbitration Service"
:: net stop "VMware Workstation Server"
echo ...Complete!

使用说明:

1. 脚本中所执行的操作需要管理员权限, xp一般不存在此问题, vista/win7则一般需要在批处理上右键并选择以管理员方式运行! 否则操作无效.

2. 在vm_enable.cmd中最后启动VM的路径如非默认则需要根据自己安装VM的位置进行改动.

3. 第一个的启动脚本中默认只启动了四个基本的VM服务, 剩下的一个”VMware Workstation Server”使用较少, 作用是提供远程连接及管理虚拟机服务. 如有需要, 把注释去掉即可.

4. devcon文件夹可任意移动, 但注意修改批处理中的引用位置.

最后附上devcon+上面写好的批处理打包下载:  vmware_control.7z

08/09/2015 Update 1: Update batch scripts and devcon tool, the new devcon tool is extracted from Windows 10 WDK.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.