Script Engine Example in Batch – v1

email me

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.

I do plan on adding a form to collect the necessary variables, and a GUI layout to add a little pizzazz to the overall look and functionality. I’ll post that when complete.

* be careful with this, especially if deploying packages to multiple computers

@echo off
Title Script Engine v1 – by Eddie Jackson
color 0a

::PACKAGE PATH, WITHOUT C:\ – will come from form
Set AppPath=Progra~2\CompanyFolder

::APP NAME – will come from form
set AppName=FooApp

::PACKAGE/SETUP FILE NAME – will come from form
Set FileName=FooSetup.exe

::USE, IF ONLY NEEDED FOR ONE COMPUTER
::otherwise, set OnePC=NO – will come from form
Set OnePC=NO

::——————————————————-

Set CurDir=C:\_ENGINE
Set PCList=computers.txt
setLocal EnableDelayedExpansion
set FilePath=c:\%AppPath%\%FileName%
If Not Exist%CurDir%\logs” (
md %CurDir%\logs
)

If Not Exist%CurDir%\paexec.exe” (
cls
echo Missing PAExec.exe file.
echo.
pause
exit
)

::GENERATES REMOTE SEQUENCE FILE
echo @echo off>remoteCommand.cmd
echo title %AppName%>>remoteCommand.cmd
echo cls>>remoteCommand.cmd
echo Echo Installing %AppName%…please wait>>remoteCommand.cmd
echo%FilePath%”>>remoteCommand.cmd

::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 ReportN=%mdate%_%mtime%.txt
if %OnePC% NEQ NO goto SINGLE

echo Multiple PC mode
echo.

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

::sets dynamic timestamp
FOR /F “TOKENS=*” %%B IN (‘DATE/T’) DO SET NowD=%%B
FOR /F “TOKENS=*” %%A IN (‘TIME/T’) DO SET NowT=%%A

cls
echo Accessing: %%a
c:\windows\System32\ping.exe %%a | find “Reply” > nul
if errorlevel 1 (echo !NowD! !NowT!, %%a, OFFLINE >> “logs\%ReportN%” & echo %%a OFFLINE & echo.
) else (
echo.
echo Copying files to %%aif not exist “\\%%a\c$\%AppPath%md “\\%%a\c$\%AppPath%”

copy /y /v “%CurDir%\remoteCommand.cmd” “\\%%a\c$\%AppPath%”
copy /y /v “%CurDir%\%FileName%” “\\%%a\c$\%AppPath%”
c:\windows\System32\ping.exe -n 4 127.0.0.1>nul
cls
echo Launching %AppName% on %%a…
“%CurDir%\PAExec.exe” \\%%a -s -i -w “c:\%AppPath%” “c:\%AppPath%\remoteCommand.cmd”
echo !NowD! !NowT!, %%a, ONLINE >> “logs\%ReportN%” & echo %%a ONLINE & echo.
)
)
goto END

:SINGLE
FOR /F “TOKENS=*” %%B IN (‘DATE/T’) DO SET NowD=%%B
FOR /F “TOKENS=*” %%A IN (‘TIME/T’) DO SET NowT=%%A

set ReportN=_SingleReport.txt
cls
echo Single PC mode
echo.
echo Accessing: %OnePC%
c:\windows\System32\ping.exe %OnePC% | find “Reply” > nul
if errorlevel 1 (echo %NowD% %NowT%, %OnePC%, OFFLINE >> “logs\%ReportN%” & echo %OnePC% OFFLINE & echo.
) else (
echo.
echo Copying files to %OnePC%if not exist “\\%OnePC%\c$\%AppPath%md “\\%OnePC%\c$\%AppPath%”

copy /y /v “%CurDir%\remoteCommand.cmd” “\\%OnePC%\c$\%AppPath%”
copy /y /v “%CurDir%\%FileName%” “\\%OnePC%\c$\%AppPath%”
c:\windows\System32\ping.exe -n 4 127.0.0.1>nul
echo Launching %AppName% on %OnePC%%CurDir%\PAExec.exe \\%OnePC% -s -i -w c:\%AppPath% c:\%AppPath%\remoteCommand.cmd
echo %NowD% %NowT%, %OnePC%, ONLINE >> “logs\%ReportN%” & echo %OnePC% ONLINE & echo.
)
goto END
:END
@echo on
pause
exit /b 0

 

 

Notes

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}