@ECHO OFF&(PUSHD "%~DP0")&(REG QUERY "HKU\S-1-5-19">NUL 2>&1)||(
powershell -Command "Start-Process '%~sdpnx0' -Verb RunAs"&&EXIT)

taskkill /f /im TeamV* >NUL 2>NUL

rd/s/q "%TEMP%\TeamViewer"2>NUL
::这里保存的连接会话记录临时文件
::rd/s/q "%AppData%\TeamViewer" 2>NUL
rd/s/q "%LOCALAPPDATA%\TeamViewer"2>NUL

reg delete "HKCU\SOFTWARE\TeamViewer" /f >NUL 2>NUL
reg delete "HKLM\SOFTWARE\TeamViewer" /f >NUL 2>NUL
reg delete "HKLM\SOFTWARE\TeamViewer" /f /reg:32 >NUL 2>NUL

::官方安装API命令支持
start /wait TeamViewer.exe api --Installer

::定义软件安装路径键值标记
IF NOT EXIST "%ProgramW6432%" (
reg add "HKLM\SOFTWARE\TeamViewer" /f /v "InstallationDirectory" /d "%~dp0\" >NUL 2>NUL
) ELSE (
reg add "HKLM\SOFTWARE\TeamViewer" /f /v "InstallationDirectory" /d "%~dp0\" /reg:32 >NUL 2>NUL
)

::关联相关协议支持
reg add "HKLM\SOFTWARE\Classes\.tpsr" /f /ve /d "TeamViewerPilotSessionReporting" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\.tvc" /f /ve /d "TeamViewerConfiguration" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\.tvlink" /f /ve /d "InternetShortcut" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\.tvlink\DefaultIcon" /f /ve /d "\"%~dp0TeamViewer.exe\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\.tvlink\OpenWithProgIds" /f /v "InternetShortcut" /d "" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\.tvs" /f /ve /d "TeamViewerSession" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\blizzv1" /f /ve /d "URL:blizzv1 Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\blizzv1" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\blizzv1\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\teamviewer10" /f /ve /d "URL:teamviewer10 Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\teamviewer10" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\teamviewer10\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\teamviewer8" /f /ve /d "URL:teamviewer8 Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\teamviewer8" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\teamviewer8\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\teamviewerapi" /f /ve /d "URL:teamviewerapi Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\teamviewerapi" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\teamviewerapi\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerConfiguration" /f /v "DefaultIcon" /d "" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerConfiguration" /f /v "shell" /d "" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerConfiguration\DefaultIcon" /f /ve /d "\"%~dp0TeamViewer.exe\",0" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerConfiguration\shell" /f /v "open" /d "" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerConfiguration\shell\open" /f /v "command" /d "" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerConfiguration\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" --control \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerPilotSessionReporting" /f /v "DefaultIcon" /d "" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerPilotSessionReporting" /f /v "shell" /d "" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerPilotSessionReporting\DefaultIcon" /f /ve /d "\"%~dp0TeamViewer.exe\",0" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerPilotSessionReporting\shell" /f /v "open" /d "" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerPilotSessionReporting\shell\open" /f /v "command" /d "" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerPilotSessionReporting\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" --opentpsr \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerSession" /f /v "DefaultIcon" /d "" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerSession" /f /v "shell" /d "" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerSession\DefaultIcon" /f /ve /d "\"%~dp0TeamViewer.exe\",0" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerSession\shell" /f /v "open" /d "" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerSession\shell\open" /f /v "command" /d "" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\TeamViewerSession\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" --play \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvchat1" /f /ve /d "URL:tvchat1 Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvchat1" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvchat1\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvcontrol1" /f /ve /d "URL:tvcontrol1 Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvcontrol1" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvcontrol1\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvfiletransfer1" /f /ve /d "URL:tvfiletransfer1 Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvfiletransfer1" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvfiletransfer1\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvjoinv8" /f /ve /d "URL:tvjoinv8 Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvjoinv8" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvjoinv8\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvpresent1" /f /ve /d "URL:tvpresent1 Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvpresent1" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvpresent1\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvsendfile1" /f /ve /d "URL:tvsendfile1 Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvsendfile1" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvsendfile1\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvsqcustomer1" /f /ve /d "URL:tvsqcustomer1 Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvsqcustomer1" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvsqcustomer1\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvsqsupport1" /f /ve /d "URL:tvsqsupport1 Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvsqsupport1" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvsqsupport1\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvvideocall1" /f /ve /d "URL:tvvideocall1 Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvvideocall1" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvvideocall1\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvvpn1" /f /ve /d "URL:tvvpn1 Protocol" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvvpn1" /f /v "URL Protocol" /d "\"\"" >NUL 2>NUL
reg add "HKLM\SOFTWARE\Classes\tvvpn1\shell\open\command" /f /ve /d "\"%~dp0TeamViewer.exe\" \"%%1\"" >NUL 2>NUL

::创建桌面快捷方式
mshta VBScript:Execute("Set a=CreateObject(""WScript.Shell""):Set b=a.CreateShortcut(a.SpecialFolders(""Desktop"") & ""\TeamViewer.lnk""):b.TargetPath=""%~sdp0TeamViewer.exe"":b.WorkingDirectory=""%~sdp0"":b.Save:close")

:MENU
CLS&ECHO.&ECHO  完成，以下自选：
ECHO.&ECHO  1、安装无人值守访问授权服务 (TeamViewer_Service.exe)
ECHO.&ECHO  2、安装启用远程打印,VPN驱动,适用于 Outlook的会议插件
CHOICE /C 12 /N >NUL 2>NUL
IF "%ERRORLEVEL%"=="2" GOTO Installdrive
IF "%ERRORLEVEL%"=="1" GOTO AddService

:AddService
taskkill /f /im TeamV* >NUL 2>NUL
if exist TeamViewer_Service.bak ren TeamViewer_Service.bak TeamViewer_Service.exe
if exist TeamViewer_Service.exe TeamViewer_Service.exe -install
sc start TeamViewer >NUL 2>NUL
CLS&ECHO.&ECHO 完成! &TIMEOUT /t 2 >NUL&GOTO MENU
:Installdrive
regsvr32 /s outlook\TeamViewerMeetingAddinShim.dll
regsvr32 /s outlook\TeamViewerMeetingAddinShim64.dll
IF NOT EXIST "%ProgramW6432%" (
start /wait "" "%~dp0tv_w32.exe" --action install --inf "%~dp0x86\teamviewervpn.inf" --id TEAMVIEWERVPN
start /wait "" "%~dp0tv_w32.exe" --action installprinterdriverfrompackage --event Local\DriverInstallFinishEvent_Printer --inf "%~dp0Printer\TeamViewer_XPSDriverFilter.inf" --id "TeamViewer Printer"
) ELSE (
start /wait "" "%~dp0tv_x64.exe" --action install --inf "%~dp0x64\teamviewervpn.inf" --id TEAMVIEWERVPN
start /wait "" "%~dp0tv_x64.exe" --action installprinterdriverfrompackage --event Local\DriverInstallFinishEvent_Printer --inf "%~dp0Printer\TeamViewer_XPSDriverFilter.inf" --id "TeamViewer Printer"
)
CLS&ECHO.&ECHO 完成! &TIMEOUT /t 2 >NUL&GOTO MENU