虚拟机启动的一个Bat文件(启动虚拟机.bat)
// 从(http://www.xdowns.com/soft/6/56/2010/Soft_64236.html)下载的绿色虚拟机,不过貌似服务总是安装不全,所以自己做了个启动文件,倘若有未安装服务则立即安装上,同时实现服务在不用时自动停止功能.
@Rem *****************************
@Rem 1.首先实现的是窗口隐藏.
@Rem 2.接下来是服务的启动,同时倘若服务并没有安装则进行安装(因为发现好多的Install.bat中并不能真正把所需服务安装上,所以就把虚拟机启动.bat的功能稍微强化一下了).
@Rem 3.启动VMware.exe之后,cmd隐藏于后台等待,并且以10秒一次进行进程状态的检测,当检测到了进程结束之后,便执行服务关闭操作,同时将服务状态进行设置(这也是因为Install.bat中安装时不设置服务启动类型,所以把虚拟机启动.bat的功能稍微强化一下).
@Rem *.当虚拟机进入后台运行Vmware.exe关闭之后cmd仍然会继续等待,直到vmware.exe和vmware-vmx.exe同时处于结束状态.
@Rem 这里面有直接拷贝别人代码的,也有自己做的部分.(bat,js,vbs),为什么js不和vbs统一呢,:-)因为本人太懒了.
@Rem 为了隐藏窗口,所以难以实现在原始路径下执行,因此执行所有程序事件需使用绝对路径
@Rem 使用注册表确认服务是否安装(reg query HKLM\SYSTEM\CurrentControlSet\Services\[服务名])
@Rem 使用SC确认服务是否安装(sc query [服务名])
@Rem *****************************
@echo off
if {%1} == {1} (GOTO _Wait)
IF "%PROCESSOR_ARCHITECTURE%" neq "x86" (set vnetlib="%~dp0vnetlib64.exe") ELSE (set vnetlib="%~dp0vnetlib.exe")
@Rem mode con cols=20 lines=1
@Rem color 30
@Rem 用于启动安装服务代码部分
echo %vnetlib%
sc query vmci >nul 2>nul||(echo vmci服务需要安装!&%vnetlib% -- install vmci&echo vmci服务安装完成!)
%vnetlib% -- start vmci
Rem net start vmci
sc query vmx86 >nul 2>nul||(echo vmx86服务需要安装!&%vnetlib% -- install vmx86&echo vmx86服务安装完成!)
%vnetlib% -- start vmx86
Rem net start vmx86
sc query vmkbd >nul 2>nul||(echo vmkbd服务需要安装!&%vnetlib% -- install vmkbd&echo vmkbd服务安装完成!)
%vnetlib% -- start vmkbd
Rem net start vmkbd
sc query VMAuthdService >nul 2>nul||(echo 授权服务需要安装!&%vnetlib% -- install authd&echo 授权服务安装完成!)
%vnetlib% -- start authd
Rem net start VMAuthdService
sc query vmnetuserif >nul 2>nul||(echo userif服务需要安装!&%vnetlib% -- install userif&echo userif服务安装完成!)
%vnetlib% -- start userif
Rem net start VMnetuserif
reg query HKLM\SYSTEM\CurrentControlSet\Services\vmnetbridge >nul 2>nul&&(goto _BridgeInstalled)
echo 桥接服务需要安装!
rundll32 setupapi,InstallHinfSection _Install1.NT 128 %cd%\netadapter.inf >nul
ver|find "6.">nul && (copy /y BridgeWin7\*.* >nul)
ver|find "5.">nul && (copy /y BridgeWinxp\*.* >nul)
%vnetlib% -- install bridge
del vmnetbridge.dll
del vmnetbridge.sys
echo 桥接服务安装完成!
:_BridgeInstalled
%vnetlib% -- start bridge
@Rem net stop VMnetBridge
@Rem net start VMnetBridge
net start hcmon
net start "VMware NAT Service"
net start VMnetDHCP
net start VMUSBArbService
@Rem 用于启动虚拟机代码部分
start "" "%~dp0vmware.exe"
@Rem 用于隐藏窗口的代码部分
>"%temp%\vmware.vbs" echo set WshShell = WScript.CreateObject^(^"WScript.Shell^"^)
>>"%temp%\vmware.vbs" echo WshShell.Run chr^(34^) ^& %0 ^& chr^(34^) ^& ^" 1^",0
start /wait /d "%temp%" vmware.vbs
del %temp%\vmware.vbs
exit
:_Wait
@Rem 用于监测虚拟机退出代码部分
@Rem To PAUSE
@Rem if(WScript.Arguments.Length != 0)
@Rem {
@Rem var sQuery = "Select * from Win32_Process where Name = '" + WScript.Arguments(0) + "'";
@Rem for(var i = 1; i < WScript.Arguments.Length; i++)
@Rem {
@Rem sQuery = sQuery + " or Name = '" + WScript.Arguments(i) + "'"
@Rem }
@Rem var objProcesses;
@Rem var WMI = GetObject("WinMgmts:");
@Rem do
@Rem {
@Rem objProcesses = null;
@Rem WScript.Sleep(1000);
@Rem objProcesses = WMI.ExecQuery(sQuery);
@Rem }
@Rem while(objProcesses.count != 0)
@Rem }
@Rem 用于虚拟机退出后清理的代码部分
>"%temp%\vmware.js" ECHO if(WScript.Arguments.Length != 0)
>>"%temp%\vmware.js" ECHO {
>>"%temp%\vmware.js" ECHO var sQuery = "Select * from Win32_Process where Name = '" + WScript.Arguments(0) + "'";
>>"%temp%\vmware.js" ECHO for(var i = 1; i ^< WScript.Arguments.Length; i++)
>>"%temp%\vmware.js" ECHO {
>>"%temp%\vmware.js" ECHO sQuery = sQuery + " or Name = '" + WScript.Arguments(i) + "'"
>>"%temp%\vmware.js" ECHO }
>>"%temp%\vmware.js" ECHO var objProcesses;
>>"%temp%\vmware.js" ECHO var WMI = GetObject("WinMgmts:");
>>"%temp%\vmware.js" ECHO do
>>"%temp%\vmware.js" ECHO {
>>"%temp%\vmware.js" ECHO objProcesses = null;
>>"%temp%\vmware.js" ECHO WScript.Sleep(1000);
>>"%temp%\vmware.js" ECHO objProcesses = WMI.ExecQuery(sQuery);
>>"%temp%\vmware.js" ECHO }
>>"%temp%\vmware.js" ECHO while(objProcesses.count != 0)
>>"%temp%\vmware.js" ECHO }
echo 进入后台等待
cscript.exe //nologo "%temp%\vmware.js" vmware.exe vmware-vmx.exe
del %temp%\vmware.js
net stop VMUSBArbService
net stop hcmon
net stop VMnetDHCP
net stop "VMware NAT Service"
net stop VMnetBridge
net stop VMnetuserif
net stop VMAuthdService
net stop vmkbd
net stop vmx86
net stop vmci
reg query "HKLM\SOFTWARE\VMware, Inc." /v OnceStarted&&(GOTO _OnceStarted)
sc config VMUSBArbService start= demand
sc config hcmon start= demand
sc config VMnetDHCP start= demand
sc config "VMware NAT Service" start= demand
sc config VMnetBridge start= demand
sc config VMnetuserif start= demand
sc config VMAuthdService start= demand
sc config vmkbd start= demand
sc config vmx86 start= demand
sc config vmci start= demand
reg add "HKLM\SOFTWARE\VMware, Inc." /v OnceStarted
:_OnceStarted
taskkill /f /im vmware-tray.exe >nul 2>nul
exit