Skip to content

Instantly share code, notes, and snippets.

@Techlogist
Last active October 3, 2021 12:36
Show Gist options
  • Save Techlogist/79db751318cf7a5bb19b849eb4537788 to your computer and use it in GitHub Desktop.
Save Techlogist/79db751318cf7a5bb19b849eb4537788 to your computer and use it in GitHub Desktop.
How to run a Batch remotely with WMIC?
REM License: GNU General Public License v2.0
REM Author: Miguel
REM Website: www.techlogist.net
REM Post: https://techlogist.net/batch/how-to-run-a-batch-remotely-with-wmic/
REM Description: This script copies the pinned shortcuts in your W10 task bar if ran as Administrator
REM OS/Language/Region: Windows/EN-US
@echo off
title Launch Batch Remotely
color f0
mode con:cols=70 lines=15
goto Start
:Start
cls
echo.
echo.
set /p RemotePC=With the remote computer name or IP:
:BatchFile
cls
echo.
echo.
set /p BatchFile=Batch file location:
if /i not "%BatchFile:.bat=%"=="%BatchFile%" (goto creds) else (cls & echo. & echo. & echo The file is not a Batch! & pause>nul & goto BatchFile)
:creds
cls
echo.
echo.
set /p Admin=Adminstrator username:
cls
echo.
echo.
set /p PassWD=Administrator Password:
cls
echo.
echo.
:mapdrive
set number=%random%
net use j: /delete /y
net use j: \\Server\share /user:%Domain%\%Admin% %PassWD%
mkdir "j:\folder%number%"
copy "%BatchFile%" "j:\folder%number%"
cd "j:\folder%number%"
for /f %%g in ('dir *.bat /b') do set FileInFolder=%%g
WMIC /node:"%RemotePC%" process call create "cmd.exe /c (net use j: /delete /y & net use j: \\Server\share /user:%Domain%\%Admin% %PassWD% & cd "j:\folder%number%\%FileInFolder%")
:exit
cls
set Admin=
set PassWD=
rmdir "j:\folder%number%" /q /s
net use j: /delete /y
goto EOF
:EOF
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment