cls
:ks
@ECHO OFF & PUSHD "%~DP0" && CD /D "%~DP0"
IF EXIST %PUBLIC% >NUL 2>&1 REG QUERY "HKU\S-1-5-19"
IF NOT %errorlevel% EQU 0 (
IF EXIST %PUBLIC% powershell.exe -windowstyle hidden -noprofile "Start-Process '%~dpnx0' -Verb RunAs"
EXIT
)
set vmbit=
if /i %PROCESSOR_IDENTIFIER:~0,3% equ x86 (
    Echo.&Echo 15.x不支持32位系统！
    pause>nul
    goto EX
) else (
    set vmbit=64
)

::检测系统又没安装VM，如果有就运行卸载批处理
reg query "HKLM\SOFTWARE\VMware, Inc." /v "ProductVersion" >nul 2> install.log || reg query "HKLM\SOFTWARE\Wow6432Node\VMware, Inc." /v "ProductVersion" >nul 2>> install.log&&(echo.&echo  检测到已经安装了VMware,请先卸载干净并重启电脑,敲任意键跳转到卸载批处理!!!&pause>nul&&call "!)卸载VMware.cmd"&goto ks)
cls

:stins
ECHO.&ECHO 稍等片刻...
echo.
echo 创建目录 >> install.log
if not exist "%AppData%\VMware" md "%AppData%\VMware" 2>NUL
if not exist "%AllUsersProfile%\VMware" md "%AllUsersProfile%\VMware" 2>NUL
if not exist "%AllUsersProfile%\VMware\VMware Workstation" md "%AllUsersProfile%\VMware\VMware Workstation" 2>NUL
if not exist "%AllUsersProfile%\VMware\VMware USB Arbitration Service" md "%AllUsersProfile%\VMware\VMware USB Arbitration Service" 2>NUL

echo. >> install.log
echo 添加注册表 >> install.log
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\vmware.exe" /f /ve /d "%cd%\vmware.exe" >NUL
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\vmware.exe" /f /v "Path" /d "%cd%\\" >NUL
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\vmplayer.exe" /f /ve /d "%cd%\vmplayer.exe" >NUL
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\vmplayer.exe"  /f /v "Path" /d "%cd%\\" >NUL

echo 添加注册表
regedit /s vm64.reg
reg add "HKLM\SOFTWARE\Wow6432Node\VMware, Inc.\VMware Player" /f /v "InstallPath" /d "%cd%\\" >NUL
reg add "HKLM\SOFTWARE\Wow6432Node\VMware, Inc.\VMware Player" /f /v "InstallPath64" /d "%cd%\x64\\" >NUL
reg add "HKLM\SOFTWARE\Wow6432Node\VMware, Inc.\VMware Workstation" /f /v "InstallPath" /d "%cd%\\" >NUL
reg add "HKLM\SOFTWARE\Wow6432Node\VMware, Inc.\VMware Workstation" /f /v "InstallPath64" /d "%cd%\x64\\" >NUL
ver|find "6.1">nul && (
echo y|copy /y vmci\vmci.cat .\ >nul
echo f|copy /y vmci\vmci.inf .\ >nul
echo f|copy /y vmci\vmci.sys .\ >nul
echo f|copy /y vmci\vsock.cat .\ >nul
echo f|copy /y vmci\vsock.sys .\ >nul
echo f|copy /y vmci\vsock.inf .\ >nul
echo f|copy /y vmci\vmciver.dll .\ >nul
echo f|copy /y vmci\vsocklib_x86.dll .\ >nul
echo f|copy /y vmci\vsocklib_x64.dll .\ >nul
)
ver|find "6.1">nul || (
echo f|copy /y vmci\vmci.cat_win8 .\vmci.cat >nul
echo f|copy /y vmci\vmci.inf_win8 .\vmci.inf >nul
echo f|copy /y vmci\vmci.sys_win8  .\vmci.sys >nul
echo f|copy /y vmci\vsock.cat_Win8 .\vsock.cat >nul
echo f|copy /y vmci\vsock.inf_Win8 .\vsock.inf >nul
echo f|copy /y vmci\vsock.sys_win8 .\vsock.sys >nul
echo f|copy /y vmci\vmciver.dll_Win8 .\vmciver.dll >nul
echo f|copy /y vmci\vsocklib_x86.dll_win8 .\vsocklib_x86.dll >nul
echo f|copy /y vmci\vsocklib_x64.dll_win8 .\vsocklib_x64.dll >nul
)
move /y vsocklib_x64.dll "%WinDir%\SysWOW64\vsocklib.dll" >nul 2>> install.log
move /y vsocklib_x86.dll "%WinDir%\System32\vsocklib.dll" >nul 2>> install.log
echo.

str.exe preferences.ini 0 0 /R /asc:"VMwareDir" /asc:"%cd%" /A >nul 2>> install.log
str.exe environments.xml 0 0 /R /asc:"VMwareDir" /asc:"%cd%" /A >nul 2>> install.log

echo 正在复制文件
if not exist "%AppData%\VMware\preferences.ini" (echo f|copy /y preferences.ini "%AppData%\VMware\" >nul 2>> install.log)
if not exist "%AppData%\VMware\config.ini" (echo f|copy /y config.ini "%AppData%\VMware\" >nul 2>> install.log)
if not exist "%AllUsersProfile%\VMware\VMware Workstation\config.ini" (echo f|copy /y config.ini "%AllUsersProfile%\VMware\VMware Workstation\" >nul 2>> install.log)
echo f|copy /y settings.ini "%AllUsersProfile%\VMware\VMware Workstation\" >nul
echo f|copy /y vnetinst.dll "%WinDir%\System32\" >nul 2>> install.log
echo f|copy /y vnetlib64.dll "%WinDir%\System32\" >nul 2>> install.log
echo f|copy /y vsock.sys "%WinDir%\system32\drivers\" >nul 2>> install.log
echo.

echo 正在注册文件
regsvr32 /s vmappcfg.dll
regsvr32 /s vmappsdk.dll
regsvr32 /s vmdbCOM.dll
regsvr32 /s elevated.dll


echo.
echo 创建 __vmware_user__ 用户组 >> install.log
echo 正在创建用户
net localgroup users __vmware_user__ /del >nul 2>nul
net localgroup __vmware__ /del >nul 2>nul
net user __vmware_user__ /del >nul 2>nul
echo.
net localgroup __vmware__ /add /comment:"VMware User Group" >nul 2>> install.log
if %errorlevel% neq 0 echo 用户组创建失败,请关闭杀毒软件或者HIPS后再重新安装VMware&pause>nul&exit

echo 安装必要服务
reg add "HKLM\SYSTEM\CurrentControlSet\Services\VMware\Performance" /f /v "Library" /d "%cd%\vmPerfmon.dll" >NUL
sc create vsock type= kernel start= boot binpath= "%WinDir%\system32\drivers\vsock.sys" Group= "System Bus Extender" displayname= "vSockets Driver"
if not exist "%WinDir%\System32\Drivers\vsock.sys" echo f|copy /y vsock.sys "%WinDir%\System32\Drivers\"
net stop vmx86 >nul 2>> install.log
sc delete vmx86 >nul 2>> install.log
set CommonPath=%CommonProgramFiles%\VMware\Drivers
mkdir "%CommonPath%" >nul
echo f|xcopy /e/y .\Drivers\* "%CommonPath%\" >nul
start /wait vnetlib%vmbit%.exe -- install vmx86
start /wait vnetlib%vmbit%.exe -- start vmx86
net stop vmx86 >nul 2>> install.log
net start vmx86 >nul 2>nul 2>> install.log
if %errorlevel% neq 0 echo 服务启动失败,请关闭杀毒软件或者HIPS后,再运行"卸载VMware.cmd"清理安装记录,然后重新安装VMware&pause>nul&exit

net stop vmci >nul 2>> install.log
sc delete vmci >nul 2>> install.log

DrvInst64.exe installRootDriver HWND "ROOT\VMWVMCIHOSTDEV" .\vmci.inf "" >nul

vnetlib%vmbit%.exe -- install vmci
net start vsock >nul 2>> install.log

echo.

echo 安装授权服务
start /wait vnetlib.exe -- install authd
start /wait vnetlib.exe -- start authd
net start VMAuthdService >nul 2>nul
reg query HKLM\SYSTEM\CurrentControlSet\Services\VMAuthdService >nul 2>nul||(sc create VMAuthdService  start= auto binpath= "%cd%\vmware-authd.exe"  depend= "vmx86/winmgmt" displayname= "VMware Authorization Service"&&net start VMAuthdService >nul 2>nul)
reg query HKLM\SYSTEM\CurrentControlSet\Services\VMAuthdService >> install.log

echo.
echo 创建快捷方式
mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(a.SpecialFolders(""AllUsersDesktop"") & ""\VMware.lnk""):b.TargetPath=""%~dp0vmware.exe"":b.WorkingDirectory=""%~dp0"":b.Save:close") >nul
echo.
echo 完成，按任意键继续安装VMware服务.
pause >nul

:ST
CALL "安装服务.cmd"
:EX
exit

goto :EX