Skip to content

Instantly share code, notes, and snippets.

@alryaz
Last active October 13, 2020 15:11
Show Gist options
  • Save alryaz/e0fd198aa8467dfed3325d3eff31d102 to your computer and use it in GitHub Desktop.
Save alryaz/e0fd198aa8467dfed3325d3eff31d102 to your computer and use it in GitHub Desktop.
RD shadowing session selection command-line script (infused with admin requirement)
REM --add the following to the top of your bat file--
@echo off
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SystemRoot%\System32\cacls.exe" "%SystemRoot%\System32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
mode con:cols=100 lines=30
query session
set /p usersession= Enter the session ID:
%SystemRoot%\System32\choice.exe /C YN /M "Enable control over target session?"
if errorlevel 2 (
start %SystemRoot%\System32\mstsc.exe /shadow:%usersession% /noConsentPrompt
) else (
start %SystemRoot%\System32\mstsc.exe /shadow:%usersession% /noConsentPrompt /control
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment