Skip to content

Instantly share code, notes, and snippets.

@realslacker
Last active January 31, 2024 21:24
Show Gist options
  • Save realslacker/29ce1818cf8bd423281362ddec57deb5 to your computer and use it in GitHub Desktop.
Save realslacker/29ce1818cf8bd423281362ddec57deb5 to your computer and use it in GitHub Desktop.
ScreenConnect Re-Install BATCH File
@ECHO OFF
REM If you ever need to re-install ScreenConnect on a client machine using
REM automation like Tanium or Automate and PowerShell is BROKEN on the
REM machine... never fear, this script will help you get it done.
REM
REM Note that this script expects to be able to be packaged with wget see:
REM https://eternallybored.org/misc/wget/
REM If wget is not present it will fallback to bitsadmin, however on Windows 7
REM bitsadmin is missing the /dynamic switch and downloads will fail.
SETLOCAL EnableDelayedExpansion
ECHO ScreenConnect Re-Deployment Script v24.1.31.1423
REM first argument into DownloadURI
SET "DownloadURI=%~1"
IF "!DownloadURI!"=="" (
ECHO ERROR: No download URI provided! 1>&2
EXIT /B 1
)
REM unescape DownloadURI, when Tanium provides
REM a parameter to a package it URL encodes the
REM arguments
SET "DownloadURI=!DownloadURI:%%3B=;!"
SET "DownloadURI=!DownloadURI:%%2E=.!"
SET "DownloadURI=!DownloadURI:%%2F=/!"
SET "DownloadURI=!DownloadURI:%%3F=?!"
SET "DownloadURI=!DownloadURI:%%3A=:!"
SET "DownloadURI=!DownloadURI:%%40=@!"
SET "DownloadURI=!DownloadURI:%%26=&!"
SET "DownloadURI=!DownloadURI:%%3D==!"
SET "DownloadURI=!DownloadURI:%%2B=+!"
SET "DownloadURI=!DownloadURI:%%24=$!"
SET "DownloadURI=!DownloadURI:%%2C=,!"
SET "DownloadURI=!DownloadURI:%%25=%%!"
ECHO Download URI: "!DownloadURI!"
REM parse the filename from the URI
SET "FileName=!DownloadURI!"
:filename-loop-start
FOR /F "tokens=1,* delims=/" %%I IN ("!FileName!") DO (
IF "%%~J"=="" (
FOR /F "tokens=1 delims=?" %%A IN ("!FileName!") DO (
SET FileName=%%~A
GOTO :filename-loop-end
)
)
SET FileName=%%~J
GOTO :filename-loop-start
)
:filename-loop-end
REM parse the extension from the filename
SET "Extension=!FileName!"
:extension-loop-start
FOR /F "tokens=1,* delims=." %%B IN ("!Extension!") DO (
IF "%%~C"=="" (
SET Extension=%%~B
GOTO :extension-loop-end
)
SET Extension=%%~C
GOTO :extension-loop-start
)
:extension-loop-end
SET "DownloadPath=%TEMP%\!FileName!"
ECHO Download Path: "!DownloadPath!"
IF EXIST "!DownloadPath!" (
ECHO Removing existing file...
DEL /F /Q "!DownloadPath!"
)
ECHO Starting download...
IF EXIST wget.exe (
wget.exe "!DownloadUri!" -O "!DownloadPath!" -q
) ELSE (
bitsadmin.exe /rawreturn /transfer ScreenConnectDownload /download /priority high /dynamic "!DownloadUri!" "!DownloadPath!"
)
IF EXIST "!DownloadPath!" (
ECHO Removing old ScreenConnect instances...
SET FindCmd=REG QUERY HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /s /f "ScreenConnect Client*"
FOR /f " usebackq delims={} tokens=2" %%i IN (`!FindCmd!`) DO (
ECHO Uninstall Key: {%%i}
START /WAIT msiexec.exe /x {%%i} /qn /norestart
SET MSIEXIT=!ERRORLEVEL!
ECHO Uninstall Exit: !MSIEXIT!
)
SET FindCmd=REG QUERY HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /s /f "ScreenConnect Client*"
FOR /F " usebackq delims={} tokens=2" %%i IN (`!FindCmd!`) DO (
ECHO Uninstall Key: {%%i}
START /WAIT msiexec.exe /x {%%i} /qn /norestart
SET MSIEXIT=!ERRORLEVEL!
ECHO Uninstall Exit: !MSIEXIT!
)
IF "!Extension!"=="exe" (
ECHO Running EXE installer...
"!DownloadPath!"
) ELSE (
ECHO Running MSI installer...
START /WAIT msiexec.exe /i "!DownloadPath!" /qn /norestart
)
ECHO Done
EXIT /B 0
) ELSE (
ECHO ERROR: Download Failed!
EXIT /B 1
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment