Script Engine Example in Batch – v2

email me

This is version two of this: I wrote this to install a ‘prepared’ package to a single computer, or to a list of computers. It’s low tech to get around the numerous problems associated with remote installation using PowerShell, VBScript, or desktop management applications.

cls
@Echo off
Title Remote Engine by Eddie Jackson
color 0a
Setlocal EnableDelayedExpansion


:: SET COMMAND
set fileName=Sequence.cmd
set fileOpt=
set workDir=c:\windows\system32
set hidden=No

Set PCList=computers.txt
:: or
Set PC=



:: INTERNAL 
:BEGIN
c:
cd "%~dp0"
set Script=False
for %%i in (%fileName%) do set cmdExt=%%~xi
set Interact=-i
if [%cmdExt%]==[.cmd] set Script=True
if [%cmdExt%]==[.CMD] set Script=True
if [%cmdExt%]==[.bat] set Script=True
if [%cmdExt%]==[.BAT] set Script=True
if [%cmdExt%]==[.vbs] goto VBS
if [%cmdExt%]==[.VBS] goto VBS
if [%hidden%]==[Yes] set Interact=

set cmdPath=%workDir%\%fileName%

:: CREATES DATE AND TIME TIMESTAMP
:: sets a static timestamp
for /F "tokens=2-4 delims=/- " %%p in ('date/T') do set mdate=%%r%%p%%q
for /F "tokens=1-2 delims=:- " %%p in ('time/T') do set mtime=%%p%%q
Set Report=logs\%mdate%_%mtime%_%report%.txt
md logs>nul
cls
if not [%PC%]==[] goto SINGLE
goto MULTI


:: PROGRAM ROUTINE
:MULTI
Echo.
Echo [Entering multi computer mode]
Echo.

if not exist "%PCList%" (
Echo Computers text file not found^^!
echo.
pause
exit
)

Set cmdRem=PAExec.exe \\%%a -s %Interact% -w %workDir% "%cmdPath%" %fileOpt%

for /f "tokens=* delims= " %%a in (%PCList%) do (

echo Contacting %%a...
ping %%a -w 250 | find "Reply">nul
if errorlevel 1 (echo %%a,Offline >>"%Report%"
) else ( 
echo %%a,Online >>"%Report%"

if %Script%==True copy /y %fileName% \\%%a\c$\windows\SysWOW64\%fileName%>nul

:: LAUNCH REMOTE COMMAND
echo Launching %cmdPath% on %%a...
%cmdRem%>nul && Echo %%a,Success >>"%Report%" || Echo %%a,Failed >>"%Report%"
echo.
)
)
goto END


:SINGLE
Echo.
Echo [Entering single computer mode]
Echo.

Set cmdRem=PAExec.exe \\%PC% -s %Interact% -w %workDir% "%cmdPath%" %fileOpt%

echo Contacting %PC%...
echo.
ping %PC% -w 250 | find "Reply">nul
if errorlevel 1 (echo %PC% is Offline^^!
) else ( 
echo %PC% is Online^^!
echo.

if %Script%==True copy /y %fileName% \\%PC%\c$\windows\SysWOW64\%fileName%>nul

:: LAUNCH REMOTE COMMAND
echo Launching %cmdPath% on %PC%...
echo.
%cmdRem%>nul && Echo Success^^! || Echo Failed^^!
echo.
goto END
)

:: EXIT
:END
Echo.
Echo Done^^!
pause
endlocal
exit /b 0


:VBS
Echo VBScripts are NOT SUPPORTED^^!
echo.
pause
exit

 

 

Notes

Run GPUpdate on List of Servers

FOR /F “tokens=*” %%A IN (C:\script\servers.txt) DO WMIC /Node:%%A Process call create “cmd.exe /c gpupdate”

$List = ‘server1′,’server2′,’server3′,’server4’
foreach ($server in $List) {psexec \\$server gpupdate}


Move to current directory

c:
cd “%~dp0”


Execute EXEs dynamically

@ECHO OFF
FOR /f “tokens=*” %%A IN (‘dir /b *.exe’) DO (
ECHO Filename: “%%A”
:: “%%A” /SILENTINSTALLOPTIONS
:: or
:: start “” “%%A”
)
pause

 

tags: Batch current directory, dir /b output, script engine, MrNetTek