# Solved: Write Log of Output of Batch File



## Phanikiran (Jul 28, 2014)

I have a batch file,I need to write logging for ------------------------------------------------------------------------------------------------------------------
echo xcopy supdate.exe \\%%a\c$\localbin\ /Z /Y>>C:\Temp\%%a.cmd
-------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------
echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
-----------------------------------------------------------------------------------------------------------------------------------
I have attached the Updatefile.zip for reference 

if anyone could offer some help I would really appreciate it.


----------



## foxidrive (Oct 20, 2012)

This may log the psexec session into %%a-file.log if that is what you want to do. I haven't tested it.


```
echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1"^>"%%a-file.log" >>C:\Temp\%%a.cmd
```


----------



## Phanikiran (Jul 28, 2014)

HI
----------------------------------------------------------------------------------------------------------------------------------

@echo off

if ("%1") == ("") goto noargs
if ("%2") == ("") goto noargs

if ("%1") neq ("reboot") if ("%1") neq == ("install") if ("%1") neq == ("preview") goto CHECKARGS2
GOTO CONTINUE

:CHECKARGS2

if ("%1") neq ("REBOOT") if ("%1") neq == ("INSTALL") if ("%1") neq == ("PREVIEW") goto noargs

:CONTINUE

if exist %2 echo Using file list - %2
if exist %2 set updlist=%2
if exist %2 goto multi

if not exist %2 echo Servername specified - %2
if not exist %2 goto single

:noargs

echo Usage: Simpleupdate [Option] [servername] OR [serverlist.txt] &echo.&echo Options: Preview (check for updates only)&echo Install (install updates but do not reboot)&echo Reboot (install updates and reboot if required) &echo.&echo Example: Simpleupdate install MyIISbox(This will install updates on the server named MyIISbox. &echo.&echo Example: Simpleupdate preview servers.txt (This will check for updates against all servers in the txt file listed.) &Echo.&echo A log output file is created in format ServerName_patch(XYZ) where X=Number of updates found, Y specifies S-uccess/F-ail/E-rror, and Z = Reboot required Y/N. &goto end
goto end

:multi

echo Option specified: %1

FOR /F "eol=; tokens=1* delims=, " %%a in (%updlist%) do (

echo ***********************************************************
Echo Running Simple Update on %%a...
echo ***********************************************************

if NOT exist logs\ md logs\
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\

echo @echo off>>C:\Temp\%%a.cmd

echo echo Checking server up status with ping %%a...>>c:\temp\%%a.cmd

echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo if ERRORLEVEL == 1 goto noreach>>c:\temp\%%a.cmd
echo.

echo echo Checking for Access on %%a...>>C:\temp\%%a.cmd
echo if not exist \\%%a\c$ goto noaccess>>c:\temp\%%a.cmd

echo.

echo.

echo cscript //NOLOGO CheckPSexeSvc.vbs %%a>>C:\Temp\%%a.cmd
echo %%a >>d:\sailogfile.txt
*echo xcopy supdate.exe \\%%a\c$\localbin\ /Z /Y>>C:\Temp\%%a.cmd*
if %ERRORLEVEL% NEQ 0 echo.Error >>d:\sailogfile.txt
if %ERRORLEVEL% EQU 0 echo.Success >>d:\sailogfile.txt
echo if exist logs\*_patch*.log del logs\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist c:\localbin\*_patch*.log del c:\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist \\%%a\c$\localbin\*_patch*.log del \\%%a\c$\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo title %%a>>c:\temp\%%a.cmd
*echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd*
if %ERRORLEVEL% NEQ 0 echo.Error >>d:\sailogfile.txt
if %ERRORLEVEL% EQU 0 echo.Success >>d:\sailogfile.txt

echo goto cleanup>>c:\temp\%%a.cmd

echo :noreach>>c:\temp\%%a.cmd
ECHO.
ECHO.
echo echo CANNOT CONNECT to %%a! Skipping patch execution!>>c:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NOT-REACHABLE.log>>c:\temp\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :noaccess>>c:\temp\%%a.cmd
ECHO.
ECHO.
ECHO echo ACCESS DENIED! Skipping patch execution!>>C:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NO-ACCESS.log>>C:\TEMP\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :cleanup>>c:\temp\%%a.cmd

ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd

ECHO if exist \\%%a\c$\localbin\*_patch*.log COPY /Y \\%%a\C$\localbin\*_patch*.log .\LOGS>>C:\Temp\%%a.cmd

echo :nopatch>>c:\temp\%%a.cmd
echo exit>>c:\temp\%%a.cmd

REM echo if exist c:\temp\%%a.cmd del C:\Temp\%%a.cmd>>c:\temp\%%a.cmd

echo. 
start "%%a" cmd /k "C:\Temp\%%a.cmd&del c:\temp\%%a.cmd&title %%a - DONE"

echo COLOR E>>C:\Temp\%%a.cmd

)

goto end

:single
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\
IF EXIST C:\TEMP\PATCHTHIS1.TXT DEL C:\TEMP\PATCHTHIS1.TXT
ECHO %2>C:\TEMP\PATCHTHIS1.TXT
set updlist=c:\temp\patchthis1.txt

goto multi

:end
if exist c:\temp\patchthis1.txt del c:\temp\patchthis1.txt

----------------------------------------------------------------------------------------

This is the file i am having and i need to write logging steps for the script which is highlighted in bold .Appreciate your help in this regard.

Phanikiran Pisipati


----------



## foxidrive (Oct 20, 2012)

If you want the style of logging that you appear to have tried to use, then this may help.


```
echo if %%ERRORLEVEL%% NEQ 0 echo.Error ^>^>d:\sailogfile.txt >>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Success ^>^>d:\sailogfile.txt >>C:\Temp\%%a.cmd
```


----------



## Phanikiran (Jul 28, 2014)

foxidrive said:


> If you want the style of logging that you appear to have tried to use, then this may help.
> 
> 
> ```
> ...


Hi ,
I have tested 
echo if %%ERRORLEVEL%% NEQ 0 echo.Error ^>^>d:\sailogfile.txt >>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Success ^>^>d:\sailogfile.txt >>C:\Temp\%%a.cmd

above changes in .bat file to log the errors when the xcopy and psexec commands failed.could you help me out.


----------



## foxidrive (Oct 20, 2012)

What was the error that you see? How did it fail?


----------



## Phanikiran (Jul 28, 2014)

@echo off
echo Checking server up status with ping tk3orchwb01...
ping -n 1 tk3orchwb01
if ERRORLEVEL == 1 goto noreach
echo Checking for Access on tk3orchwb01...
if not exist \\tk3orchwb01\c$ goto noaccess
cscript //NOLOGO CheckPSexeSvc.vbs tk3orchwb01
xcopy supdate.exe \\tk3orchwb01\c$\localbin\ /Z /Y
Success 
if exist logs\*_patch*.log del logs\*_patch*.log
if exist c:\localbin\*_patch*.log del c:\localbin\*_patch*.log
if exist \\tk3orchwb01\c$\localbin\*_patch*.log del \\tk3orchwb01\c$\localbin\*_patch*.log
title tk3orchwb01
psexec -n 300 -h -s \\tk3orchwb01 cmd /c "title tk3orchwb01&hostname&echo Running Simple Update...&cd \localbin\&supdate -preview">"tk3orchwb01-file.log" 
if %ERRORLEVEL% NEQ 0 echo.Error >>d:\sailogfile.txt 
if %ERRORLEVEL% EQU 0 echo.Success >>d:\sailogfile.txt 
goto cleanup
:noreach
echo CANNOT CONNECT to tk3orchwb01! Skipping patch execution!
IF EXIST .\LOGS\tk3orchwb01-no-access* del .\LOGS\tk3orchwb01-no-access-*
IF EXIST .\LOGS\tk3orchwb01_patch* del .\LOGS\tk3orchwb01_patch*
COPY /Y NUL .\logs\tk3orchwb01_patch-NOT-REACHABLE.log
goto nopatch
:noaccess
echo ACCESS DENIED! Skipping patch execution!
IF EXIST .\LOGS\tk3orchwb01-not-reachable.log del .\LOGS\tk3orchwb01-not-reachable.log*
IF EXIST .\LOGS\tk3orchwb01_patch* del .\LOGS\tk3orchwb01_patch*
COPY /Y NUL .\logs\tk3orchwb01_patch-NO-ACCESS.log
goto nopatch
:cleanup
IF EXIST .\LOGS\tk3orchwb01-not-reachable.log del .\LOGS\tk3orchwb01-not-reachable.log*
IF EXIST .\LOGS\tk3orchwb01-no-access* del .\LOGS\tk3orchwb01-no-access-*
IF EXIST .\LOGS\tk3orchwb01_patch* del .\LOGS\tk3orchwb01_patch*
if exist \\tk3orchwb01\c$\localbin\*_patch*.log COPY /Y \\tk3orchwb01\C$\localbin\*_patch*.log .\LOGS
:nopatch
exit
COLOR E
*This is the error i am seeing * foxidrive ,let me know what can we do now.


----------



## Phanikiran (Jul 28, 2014)

Sailogfile

tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Success 
Success 
tk3orchwb01 
Success 
Success 
tk3orchwb01 
Success 
Success 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error 
tk3orchwb01 
Error


----------



## Phanikiran (Jul 28, 2014)

Also want to capture date time for the sailogfile.txt .could you help in this regard


----------



## foxidrive (Oct 20, 2012)

That batch snippet you posted shows a problem here


> xcopy supdate.exe \\tk3orchwb01\c$\localbin\ /Z /Y
> Success


where Success shows that you have an error in your original batch file because it should not be printing Success there.

Try this after the xcopy and psexec lines.


```
echo if %%ERRORLEVEL%% NEQ 0 echo.Error - %%date%% %%time%% ^>^>d:\sailogfile.txt >>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Success - %%date%% %%time%% ^>^>d:\sailogfile.txt >>C:\Temp\%%a.cmd
```


----------



## Phanikiran (Jul 28, 2014)

Thanks a lot I am getting the sailogfile.txt for 
tk3orchwb01 
Error - Fri 08/01/2014 10:53:57.11 
Error - Fri 08/01/2014 10:53:57.12 

which is as expected thanks a lot .. 

Cheers,
Phani.


----------



## Phanikiran (Jul 28, 2014)

Hi i want to have sailogfile.txt displaying with respect to date wise ,so that for particular day i can have the server logs.

could you help in this ..

Regards,
Phani,


----------



## Phanikiran (Jul 28, 2014)

also there should be new file created with tomorrow date if we run the bat file tomorrow and the today log file will remain and not be deleted.


----------



## foxidrive (Oct 20, 2012)

I think you want the log file to have the days date and time in it too, so change the occurrences of this


```
d:\sailogfile.txt
```
to this, and include the double quotes.


```
"d:\sailogfile - %%date%% %%time%.txt"
```


----------



## Phanikiran (Jul 28, 2014)

hi I am getting 
Error in appending a timestamp to file name ie Simpleupdatelogfile - %date% %time%.txt


ServerName:tk3orchwb01 

how can we do this?


----------



## Phanikiran (Jul 28, 2014)

Hi this is the way i have modified the file 

echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the xcopy on supdate.exe on %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date%% %%time%%.txt">>C:\Temp\%%a.cmd

echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the xcopy on supdate.exe on %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date%% %%time%%.txt">>C:\Temp\%%a.cmd 

echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date%% %%time%%.txt" >>C:\Temp\%%a.cmd

echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the psexec on %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date%% %%time%%.txt" >>C:\Temp\%%a.cmd

but it seems getting the above thead error .

Can you hel me out.


----------



## foxidrive (Oct 20, 2012)

What error do you see?


----------



## Phanikiran (Jul 28, 2014)

Simpleupdatelogfile - %date% %time%.txt is been displayed as error where as the file name should be Simpleupdatelogfile -08/04/2014.txt

I have modified the file as :
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the xcopy on supdate.exe on %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date%% %%time%%.txt">>C:\Temp\%%a.cmd

echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the xcopy on supdate.exe on %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date%% %%time%%.txt">>C:\Temp\%%a.cmd 

echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date%% %%time%%.txt" >>C:\Temp\%%a.cmd

echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the psexec on %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date%% %%time%%.txt" >>C:\Temp\%%a.cmd


----------



## foxidrive (Oct 20, 2012)

*EDITED*

You've given me the clue - date and time contain illegal filename characters - try this modification


```
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the xcopy on  supdate.exe on %%date%% %%time%% ^>^>"Simpleupdatelogfile -  %%date:/=-%% %%time::=.%%.txt">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the xcopy on  supdate.exe on %%date%% %%time%% ^>^>"Simpleupdatelogfile -  %%date:/=-%% %%time::=.%%.txt">>C:\Temp\%%a.cmd 

echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on  %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date:/=-%% %%time::=.%%.txt" >>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the psexec on  %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date:/=-%% %%time::=.%%.txt" >>C:\Temp\%%a.cmd
```


----------



## Phanikiran (Jul 28, 2014)

It is working foxidrive and thanks a lot.


----------



## Phanikiran (Jul 28, 2014)

Hi 
Now i am able to execute the bat file by calling the list of servers in a file and logs are been fetched in the Simpleupdatelogfile.txt ,now i want to have a dotted line separator after these list of servers have been executed in the Simpleupdatelogfile.txt so that again if i run another list of servers the logs relating can be easily identified.

Like the below mentioned scenario i.e(dotted line).

Error in performing the xcopy on tk3orchwb01 on Tue 08/05/2014 12:46:47.47 
Error in performing the psexec on tk3orchwb01 on Tue 08/05/2014 12:46:47.48 
Sucess in performing the xcopy on tk3orchwb02 on Tue 08/05/2014 12:47:16.43 
Error in performing the psexec on tk3orchwb02 on Tue 08/05/2014 12:47:18.02 
Sucess in performing the xcopy on tk5xemswb22 on Tue 08/05/2014 12:47:41.08 
Error in performing the psexec on tk5xemswb22 on Tue 08/05/2014 12:47:42.89 
Sucess in performing the xcopy on tk5xemswb23 on Tue 08/05/2014 12:48:11.17 
Error in performing the psexec on tk5xemswb23 on Tue 08/05/2014 12:48:12.23 
CANNOT CONNECT to tk3ecdditsql402 on Tue 08/05/2014 16:52:17.72 ! Skipping patch execution!
***********************************************************************************************************
Error in performing the xcopy on tk3orchwb01 on Tue 08/05/2014 18:46:47.47 
Error in performing the psexec on tk3orchwb01 on Tue 08/05/2014 18:46:47.48 
Sucess in performing the xcopy on tk3orchwb02 on Tue 08/05/2014 18:47:16.43 

What need to be added for the dotted line foxidrive?

Thanks for your help
Kiran.


----------



## Phanikiran (Jul 28, 2014)

Is it something like this ....

@echo off

if ("%1") == ("") goto noargs
if ("%2") == ("") goto noargs

if ("%1") neq ("reboot") if ("%1") neq == ("install") if ("%1") neq == ("preview") goto CHECKARGS2
GOTO CONTINUE

:CHECKARGS2

if ("%1") neq ("REBOOT") if ("%1") neq == ("INSTALL") if ("%1") neq == ("PREVIEW") goto noargs

:CONTINUE

if exist %2 echo Using file list - %2
if exist %2 set updlist=%2
if exist %2 goto multi

if not exist %2 echo Servername specified - %2
if not exist %2 goto single

:noargs

echo Usage: Simpleupdate [Option] [servername] OR [serverlist.txt] &echo.&echo Options: Preview (check for updates only)&echo Install (install updates but do not reboot)&echo Reboot (install updates and reboot if required) &echo.&echo Example: Simpleupdate install MyIISbox(This will install updates on the server named MyIISbox. &echo.&echo Example: Simpleupdate preview servers.txt (This will check for updates against all servers in the txt file listed.) &Echo.&echo A log output file is created in format ServerName_patch(XYZ) where X=Number of updates found, Y specifies S-uccess/F-ail/E-rror, and Z = Reboot required Y/N. &goto end
goto end

:multi

echo Option specified: %1

FOR /F "eol=; tokens=1* delims=, " %%a in (%updlist%) do (

echo ***********************************************************
Echo Running Simple Update on %%a...
echo ***********************************************************

if NOT exist logs\ md logs\
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\

echo @echo off>>C:\Temp\%%a.cmd

echo echo Checking server up status with ping %%a...>>c:\temp\%%a.cmd

echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo if ERRORLEVEL == 1 goto noreach>>c:\temp\%%a.cmd
echo.

echo echo Checking for Access on %%a...>>C:\temp\%%a.cmd
echo if not exist \\%%a\c$ goto noaccess>>c:\temp\%%a.cmd

echo.

echo.

echo cscript //NOLOGO CheckPSexeSvc.vbs %%a>>C:\Temp\%%a.cmd
echo %%a >>d:\sailogfile.txt
echo xcopy supdate.exe \\%%a\c$\localbin\ /Z /Y>>C:\Temp\%%a.cmd
if %ERRORLEVEL% NEQ 0 echo.Error >>d:\sailogfile.txt
if %ERRORLEVEL% EQU 0 echo.Success >>d:\sailogfile.txt
echo if exist logs\*_patch*.log del logs\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist c:\localbin\*_patch*.log del c:\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist \\%%a\c$\localbin\*_patch*.log del \\%%a\c$\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo title %%a>>c:\temp\%%a.cmd
echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
if %ERRORLEVEL% NEQ 0 echo.Error >>d:\sailogfile.txt
if %ERRORLEVEL% EQU 0 echo.Success >>d:\sailogfile.txt

echo goto cleanup>>c:\temp\%%a.cmd

echo :noreach>>c:\temp\%%a.cmd
ECHO.
ECHO.
echo echo CANNOT CONNECT to %%a! Skipping patch execution!>>c:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NOT-REACHABLE.log>>c:\temp\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :noaccess>>c:\temp\%%a.cmd
ECHO.
ECHO.
ECHO echo ACCESS DENIED! Skipping patch execution!>>C:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NO-ACCESS.log>>C:\TEMP\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :cleanup>>c:\temp\%%a.cmd

ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd

ECHO if exist \\%%a\c$\localbin\*_patch*.log COPY /Y \\%%a\C$\localbin\*_patch*.log .\LOGS>>C:\Temp\%%a.cmd

echo :nopatch>>c:\temp\%%a.cmd
echo exit>>c:\temp\%%a.cmd

REM echo if exist c:\temp\%%a.cmd del C:\Temp\%%a.cmd>>c:\temp\%%a.cmd

echo. 
start "%%a" cmd /k "C:\Temp\%%a.cmd&del c:\temp\%%a.cmd&title %%a - DONE"

echo COLOR E>>C:\Temp\%%a.cmd

)

goto end

:single
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\
IF EXIST C:\TEMP\PATCHTHIS1.TXT DEL C:\TEMP\PATCHTHIS1.TXT
ECHO %2>C:\TEMP\PATCHTHIS1.TXT
set updlist=c:\temp\patchthis1.txt

goto multi

:end
if exist c:\temp\patchthis1.txt del c:\temp\patchthis1.txt
echo ******************************************************************** >>Simpleupdatelogfile.txt


----------



## foxidrive (Oct 20, 2012)

At the top of your batch file include these lines to get a static date and time:


```
set d=%date:/=-%
set t=%time::=.%
```
Then in the lines writing to the log change them to this to use the static date and time.


```
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the xcopy on  supdate.exe on %%date%% %%time%% ^>^>"Simpleupdatelogfile -  %d% %t%.txt">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the xcopy on  supdate.exe on %%date%% %%time%% ^>^>"Simpleupdatelogfile -  %d% %t%.txt">>C:\Temp\%%a.cmd 

echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on  %%date%% %%time%% ^>^>"Simpleupdatelogfile - %d% %t%.txt" >>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the psexec on  %%date%% %%time%% ^>^>"Simpleupdatelogfile - %d% %t%.txt" >>C:\Temp\%%a.cmd
```
and if you don't want date-time stamped log files then ignore the above and place the divider line above that lower line that already exists in the batch file.


```
echo echo ********************************************** ^>^>"Simpleupdatelogfile.txt" >>C:\Temp\%%a.cmd
echo exit>>c:\temp\%%a.cmd
```


----------



## Phanikiran (Jul 28, 2014)

Hi You mean to say (place the divider line above that lower line that already exists in the batch file.)Is this way as i mentioned below down line of code ?

:single
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\
IF EXIST C:\TEMP\PATCHTHIS1.TXT DEL C:\TEMP\PATCHTHIS1.TXT
ECHO %2>C:\TEMP\PATCHTHIS1.TXT
set updlist=c:\temp\patchthis1.txt

goto multi

:end
if exist c:\temp\patchthis1.txt del c:\temp\patchthis1.txt
echo echo ********************************************** ^>^>"Simpleupdatelogfile.txt" >>C:\Temp\%%a.cmd
echo exit>>c:\temp\%%a.cmd


----------



## foxidrive (Oct 20, 2012)

No, it should be between these lines:



> echo :nopatch>>c:\temp\%%a.cmd
> echo exit>>c:\temp\%%a.cmd


----------



## Phanikiran (Jul 28, 2014)

ok foxidrive,
it is working fine now


----------



## Phanikiran (Jul 28, 2014)

Hi in the batfile code pasted below ,i had a requirement for 
*pinging 5 times on server in the batch file-*--so how can we accomplish this ,what needs to be modified in the code form the batfile,,here is the code below

@echo off

if ("%1") == ("") goto noargs
if ("%2") == ("") goto noargs

if ("%1") neq ("reboot") if ("%1") neq == ("install") if ("%1") neq == ("preview") goto CHECKARGS2
GOTO CONTINUE

:CHECKARGS2

if ("%1") neq ("REBOOT") if ("%1") neq == ("INSTALL") if ("%1") neq == ("PREVIEW") goto noargs

:CONTINUE

if exist %2 echo Using file list - %2
if exist %2 set updlist=%2
if exist %2 goto multi

if not exist %2 echo Servername specified - %2
if not exist %2 goto single

:noargs

echo Usage: Simpleupdate [Option] [servername] OR [serverlist.txt] &echo.&echo Options: Preview (check for updates only)&echo Install (install updates but do not reboot)&echo Reboot (install updates and reboot if required) &echo.&echo Example: Simpleupdate install MyIISbox(This will install updates on the server named MyIISbox. &echo.&echo Example: Simpleupdate preview servers.txt (This will check for updates against all servers in the txt file listed.) &Echo.&echo A log output file is created in format ServerName_patch(XYZ) where X=Number of updates found, Y specifies S-uccess/F-ail/E-rror, and Z = Reboot required Y/N. &goto end
goto end

:multi

echo Option specified: %1

FOR /F "eol=; tokens=1* delims=, " %%a in (%updlist%) do (

echo ***********************************************************
Echo Running Simple Update on %%a...
echo ***********************************************************

if NOT exist logs\ md logs\
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\

echo @echo off>>C:\Temp\%%a.cmd

echo echo Checking server up status with ping %%a...>>c:\temp\%%a.cmd

echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo if ERRORLEVEL == 1 goto noreach>>c:\temp\%%a.cmd
echo.

echo echo Checking for Access on %%a...>>C:\temp\%%a.cmd
echo if not exist \\%%a\c$ goto noaccess>>c:\temp\%%a.cmd

echo.

echo.

echo cscript //NOLOGO CheckPSexeSvc.vbs %%a>>C:\Temp\%%a.cmd
echo %%a >>"Simpleupdatelogfile - %%date:/=-%%.txt"

echo xcopy supdate.exe \\%%a\c$\localbin\ /Z /Y>>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the xcopy on %%a on %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the xcopy on %%a on %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd

echo if exist logs\*_patch*.log del logs\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist c:\localbin\*_patch*.log del c:\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist \\%%a\c$\localbin\*_patch*.log del \\%%a\c$\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo title %%a>>c:\temp\%%a.cmd

echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on %%a on %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the psexec on %%a on %%date%% %%time%% ^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd

echo goto cleanup>>c:\temp\%%a.cmd

echo :noreach>>c:\temp\%%a.cmd
ECHO.
ECHO.
echo echo CANNOT CONNECT to %%a on %%date%% %%time%% ! Skipping patch execution!^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NOT-REACHABLE.log>>c:\temp\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :noaccess>>c:\temp\%%a.cmd
ECHO.
ECHO.
ECHO echo ACCESS DENIED to %%a on %%date%% %%time%% ! Skipping patch execution!^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NO-ACCESS.log>>C:\TEMP\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :cleanup>>c:\temp\%%a.cmd

ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd

ECHO if exist \\%%a\c$\localbin\*_patch*.log COPY /Y \\%%a\C$\localbin\*_patch*.log .\LOGS>>C:\Temp\%%a.cmd

echo :nopatch>>c:\temp\%%a.cmd
echo echo ****************************************************************************** ^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
echo exit>>c:\temp\%%a.cmd

REM echo if exist c:\temp\%%a.cmd del C:\Temp\%%a.cmd>>c:\temp\%%a.cmd

echo. 
start "%%a" cmd /k "C:\Temp\%%a.cmd&del c:\temp\%%a.cmd&title %%a - DONE"

echo COLOR E>>C:\Temp\%%a.cmd

)

goto end

:single
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\
IF EXIST C:\TEMP\PATCHTHIS1.TXT DEL C:\TEMP\PATCHTHIS1.TXT
ECHO %2>C:\TEMP\PATCHTHIS1.TXT
set updlist=c:\temp\patchthis1.txt

goto multi

:end
if exist c:\temp\patchthis1.txt del c:\temp\patchthis1.txt


----------



## foxidrive (Oct 20, 2012)

Change the 1 to a 5 to get more pings and change null to nul


```
echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
```


----------



## Phanikiran (Jul 28, 2014)

hi i want this line in the beggining of the logfile 

echo echo ********************************************** ^>^>"Simpleupdatelogfile.txt" >>C:\Temp\%%a.cmd

what needs to be modified in the code form the batfile code


----------



## foxidrive (Oct 20, 2012)

Place it under this line:


```
echo @echo off>>C:\Temp\%%a.cmd
```


----------



## Phanikiran (Jul 28, 2014)

@echo off

if ("%1") == ("") goto noargs
if ("%2") == ("") goto noargs

if ("%1") neq ("reboot") if ("%1") neq == ("install") if ("%1") neq == ("preview") goto CHECKARGS2
GOTO CONTINUE

:CHECKARGS2

if ("%1") neq ("REBOOT") if ("%1") neq == ("INSTALL") if ("%1") neq == ("PREVIEW") goto noargs

:CONTINUE

if exist %2 echo Using file list - %2
if exist %2 set updlist=%2
if exist %2 goto multi

if not exist %2 echo Servername specified - %2
if not exist %2 goto single

:noargs

echo Usage: Simpleupdate [Option] [servername] OR [serverlist.txt] &echo.&echo Options: Preview (check for updates only)&echo Install (install updates but do not reboot)&echo Reboot (install updates and reboot if required) &echo.&echo Example: Simpleupdate install MyIISbox(This will install updates on the server named MyIISbox. &echo.&echo Example: Simpleupdate preview servers.txt (This will check for updates against all servers in the txt file listed.) &Echo.&echo A log output file is created in format ServerName_patch(XYZ) where X=Number of updates found, Y specifies S-uccess/F-ail/E-rror, and Z = Reboot required Y/N. &goto end
goto end

:multi

echo Option specified: %1

FOR /F "eol=; tokens=1* delims=, " %%a in (%updlist%) do (

echo ***********************************************************
Echo Running Simple Update on %%a...
echo ***********************************************************

if NOT exist logs\ md logs\
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\

echo @echo off>>C:\Temp\%%a.cmd
echo echo ****************************************************************************** ^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd

echo echo Checking server up status with ping %%a...>>c:\temp\%%a.cmd

echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo if ERRORLEVEL == 1 goto noreach>>c:\temp\%%a.cmd
echo.

echo echo Checking for Access on %%a...>>C:\temp\%%a.cmd
echo if not exist \\%%a\c$ goto noaccess>>c:\temp\%%a.cmd

echo.

echo.

echo cscript //NOLOGO CheckPSexeSvc.vbs %%a>>C:\Temp\%%a.cmd
echo %%a >>"Simpleupdatelogfile - %%date:/=-%%.txt"

echo xcopy supdate.exe \\%%a\c$\localbin\ /Z /Y>>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the xcopy on %%a on %%date%% %%time%% ^>^>"SimpleupdateErrorlogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Success in performing the xcopy on %%a on %%date%% %%time%% ^>^>"SimpleupdateSuccesslogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd

echo if exist logs\*_patch*.log del logs\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist c:\localbin\*_patch*.log del c:\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist \\%%a\c$\localbin\*_patch*.log del \\%%a\c$\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo title %%a>>c:\temp\%%a.cmd

echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateErrorlogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateSuccesslogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd

echo goto cleanup>>c:\temp\%%a.cmd

echo :noreach>>c:\temp\%%a.cmd
ECHO.
ECHO.
echo echo CANNOT CONNECT to %%a on %%date%% %%time%% ! Skipping patch execution!^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NOT-REACHABLE.log>>c:\temp\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :noaccess>>c:\temp\%%a.cmd
ECHO.
ECHO.
ECHO echo ACCESS DENIED to %%a on %%date%% %%time%% ! Skipping patch execution!^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NO-ACCESS.log>>C:\TEMP\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :cleanup>>c:\temp\%%a.cmd

ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd

ECHO if exist \\%%a\c$\localbin\*_patch*.log COPY /Y \\%%a\C$\localbin\*_patch*.log .\LOGS>>C:\Temp\%%a.cmd

echo :nopatch>>c:\temp\%%a.cmd
echo exit>>c:\temp\%%a.cmd

REM echo if exist c:\temp\%%a.cmd del C:\Temp\%%a.cmd>>c:\temp\%%a.cmd

echo. 
start "%%a" cmd /k "C:\Temp\%%a.cmd&del c:\temp\%%a.cmd&title %%a - DONE"

echo COLOR E>>C:\Temp\%%a.cmd

)

goto end

:single
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\
IF EXIST C:\TEMP\PATCHTHIS1.TXT DEL C:\TEMP\PATCHTHIS1.TXT
ECHO %2>C:\TEMP\PATCHTHIS1.TXT
set updlist=c:\temp\patchthis1.txt

goto multi

:end
if exist c:\temp\patchthis1.txt del c:\temp\patchthis1.txt


----------



## Phanikiran (Jul 28, 2014)

hi above is the code ,
and another a requirement for implementing by logic for "pinging 5 times on server in the batch file" 
I have the logic as ...
for (int i = 0; i <= 5; i++)
{
string ping = "ping" + i;
if (ping = "success")
{
//XCOPY
//PSEXEC
}
else
{
ping = "ping" + i +1;
}
}
.............................................................................................
please let me know how can above logic can be implemented in Dos in the batfile code.


----------



## foxidrive (Oct 20, 2012)

Do you want a delay between the ping attempts? How long will the delay be?

The change I have already suggested will ping 5 times and if any one of them gets a reply then it will branch to do your tasks.


----------



## Phanikiran (Jul 28, 2014)

HI foxidrive I am looking to for the server to ping 5 times based on Sucess and ERROR iteration .Consider if ping 1 is sucess then only the XCOPY and PSEXEC commands will execute and create logs in respective files.If Ping 1 is ERROR then it will check for Ping2 ,now If Ping 2 is sucess then only the XCOPY and PSEXEC commands will execute and create logs in respective files.If Ping 2 is ERROR then it will check for Ping3 ,now if Ping 3 is sucess then only the XCOPY and PSEXEC commands will execute and create logs in respective files. like wise it will check uptill ping 5 sucess conecutively. below i mentioned the logic ,Can we implement this logic in bat file using DOS ?.


string ping = "ping1";
if(ping="success")
{
//XCOPY
//PSEXEC
}
else
{
ping = "ping2";
}

if (ping = "success")
{
//XCOPY
//PSEXEC
}
else
{
ping = "ping3";
}
if (ping = "success")
{
//XCOPY
//PSEXEC
}
else
{
ping = "ping4";
}
if (ping = "success")
{
//XCOPY
//PSEXEC
}
else
{
ping = "ping5";
}
if (ping = "success")
{
//XCOPY
//PSEXEC
}
else
{
"stop";
}

string ping = "ping1";
if(ping="success")
{
//XCOPY
//PSEXEC
}
else
{
ping = "ping2";
}

if (ping = "success")
{
//XCOPY
//PSEXEC
}
else
{
ping = "ping3";
}
if (ping = "success")
{
//XCOPY
//PSEXEC
}
else
{
ping = "ping4";
}
if (ping = "success")
{
//XCOPY
//PSEXEC
}
else
{
ping = "ping5";
}
if (ping = "success")
{
//XCOPY
//PSEXEC
}
else
{
"stop";
}


----------



## foxidrive (Oct 20, 2012)

Phanikiran said:


> HI foxidrive I am looking to for the server to ping 5 times based on Sucess and ERROR iteration .Consider if ping 1 is sucess then only the XCOPY and PSEXEC commands will execute and create logs in respective files.If Ping 1 is ERROR then it will check for Ping2 ,now If Ping 2 is sucess then only the XCOPY and PSEXEC commands will execute and create logs in respective files.If Ping 2 is ERROR then it will check for Ping3 ,now if Ping 3 is sucess then only the XCOPY and PSEXEC commands will execute and create logs in respective files. like wise it will check uptill ping 5 sucess conecutively. below i mentioned the logic ,Can we implement this logic in bat file using DOS ?.


What delay do you want between the pings? is the server likely to be down and come up in 5 minutes or what?

Otherwise the ping command will try 5 times sequentially and if all 5 fail it will "goto noreach"

The only reason you would run a loop to 5 different ping commands to the same server is if you want a long delay between each attempt.


----------



## Phanikiran (Jul 28, 2014)

There should be no delay betwwen the pings and it should be continous foxidrive.


----------



## foxidrive (Oct 20, 2012)

Then see this post: http://forums.techguy.org/8950123-post28.html


----------



## Phanikiran (Jul 28, 2014)

Understood foxidrive thanks..


----------



## Phanikiran (Jul 28, 2014)

hi from the below code :
*echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd*
*echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateErrorlogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd*
*echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateSuccesslogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd*
while performing the psexec ,if error comes in performing the psexec on server then i need to do again the psexec command operation .how can the code get modified?


----------



## Phanikiran (Jul 28, 2014)

*i am doing in this way, is this correct one?*

echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateErrorlogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Success in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateRevisedlogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd

echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Success in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateSuccesslogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd


----------



## foxidrive (Oct 20, 2012)

These two blue additions should work to repeat the PSEXEC line on an error

*echo :again**>>C:\Temp\%%a.cmd 
echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd*
*echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateErrorlogfile - %%date:/=-%%.txt" ^& goto :again>>C:\Temp\%%a.cmd*
*echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateSuccesslogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd*


----------



## Phanikiran (Jul 28, 2014)

Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.52 
Error in performing the psexec on tk5xemswb23 on Mon 08/11/2014 12:22:18.52 
Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.52 
Error in performing the psexec on tk5xemswb22 on Mon 08/11/2014 12:22:18.53 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.53 
Error in performing the psexec on tk5xemswb23 on Mon 08/11/2014 12:22:18.53 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.53 
Error in performing the psexec on tk5xemswb23 on Mon 08/11/2014 12:22:18.53 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.54 
Error in performing the psexec on tk5xemswb22 on Mon 08/11/2014 12:22:18.54 
Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.54 
Error in performing the psexec on tk5xemswb23 on Mon 08/11/2014 12:22:18.54 
Error in performing the psexec on tk5xemswb22 on Mon 08/11/2014 12:22:18.55 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.55 
Error in performing the psexec on tk5xemswb23 on Mon 08/11/2014 12:22:18.55 
Error in performing the psexec on tk5xemswb22 on Mon 08/11/2014 12:22:18.55 
Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.56 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.56 
Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.56 
Error in performing the psexec on tk5xemswb22 on Mon 08/11/2014 12:22:18.56 
Error in performing the psexec on tk5xemswb23 on Mon 08/11/2014 12:22:18.56 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.57 
Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.57 
Error in performing the psexec on tk5xemswb22 on Mon 08/11/2014 12:22:18.57 
Error in performing the psexec on tk5xemswb23 on Mon 08/11/2014 12:22:18.57 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.58 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.58 
Error in performing the psexec on tk5xemswb22 on Mon 08/11/2014 12:22:18.58 
Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.58 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.59 
Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.59 
Error in performing the psexec on tk5xemswb23 on Mon 08/11/2014 12:22:18.59 
Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.59 
Error in performing the psexec on tk5xemswb23 on Mon 08/11/2014 12:22:18.60 
Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.60 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.60 
Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.60 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.60 
Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.60 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.61 
Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.61 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.61 
Error in performing the psexec on tk5xemswb22 on Mon 08/11/2014 12:22:18.61 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.61 
Error in performing the psexec on tk5xemswb22 on Mon 08/11/2014 12:22:18.62 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.62 
Error in performing the psexec on tk5xemswb22 on Mon 08/11/2014 12:22:18.62 
Error in performing the psexec on tk3orchwb01 on Mon 08/11/2014 12:22:18.63 
Error in performing the psexec on tk5xemswb22 on Mon 08/11/2014 12:22:18.63 
Error in performing the psexec on tk5xemswb23 on Mon 08/11/2014 12:22:18.63 
Error in performing the psexec on tk5xemswb22 on Mon 08/11/2014 12:22:18.63 
Error in performing the psexec on tk5xemswb23 on Mon 08/11/2014 12:22:18.64 
Error in performing the psexec on tk5xemswb23 on Mon 08/11/2014 12:22:18.64 
Error in performing the psexec on tk5xemswb22 on Mon 08/11/2014 12:22:18.65 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.65 
Error in performing the psexec on tk3orchwb02 on Mon 08/11/2014 12:22:18.66


----------



## Phanikiran (Jul 28, 2014)

and the cmd prompt is running indefinitely times in psexec command,how to modify the code?please see the attachemnt


----------



## foxidrive (Oct 20, 2012)

psexec.exe is missing.


----------



## Phanikiran (Jul 28, 2014)

while performing the psexec ,if error comes in performing the psexec on server then i need to do 2 times the psexec command operation .how to do foxidrive?


----------



## Phanikiran (Jul 28, 2014)

Hi foxidrive,
I had a requirement where i need to implement the *logic for pining the server for 5 times* ,below is the modified code which is giving error in script.could you help me out in rectifying the errors ,bold ones in the code is the logic i have written and does IF and ELSE support in DOS,pls let me know?

@echo off

if ("%1") == ("") goto noargs
if ("%2") == ("") goto noargs

if ("%1") neq ("reboot") if ("%1") neq == ("install") if ("%1") neq == ("preview") goto CHECKARGS2
GOTO CONTINUE

:CHECKARGS2

if ("%1") neq ("REBOOT") if ("%1") neq == ("INSTALL") if ("%1") neq == ("PREVIEW") goto noargs

:CONTINUE

if exist %2 echo Using file list - %2
if exist %2 set updlist=%2
if exist %2 goto multi

if not exist %2 echo Servername specified - %2
if not exist %2 goto single

:noargs

echo Usage: Simpleupdate [Option] [servername] OR [serverlist.txt] &echo.&echo Options: Preview (check for updates only)&echo Install (install updates but do not reboot)&echo Reboot (install updates and reboot if required) &echo.&echo Example: Simpleupdate install MyIISbox(This will install updates on the server named MyIISbox. &echo.&echo Example: Simpleupdate preview servers.txt (This will check for updates against all servers in the txt file listed.) &Echo.&echo A log output file is created in format ServerName_patch(XYZ) where X=Number of updates found, Y specifies S-uccess/F-ail/E-rror, and Z = Reboot required Y/N. &goto end
goto end

:multi

echo Option specified: %1

FOR /F "eol=; tokens=1* delims=, " %%a in (%updlist%) do (

echo ***********************************************************
Echo Running Simple Update on %%a...
echo ***********************************************************

if NOT exist logs\ md logs\
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\

echo @echo off>>C:\Temp\%%a.cmd

echo echo Checking server up status with ping %%a...>>c:\temp\%%a.cmd

*echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo IF %ERRORLEVEL% == 0 ( 
^& goto :xcopy
^& goto sexec
echo if %%ERRORLEVEL%% EQU 0 echo.Success for first ping^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
)
echo ELSE (
echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo IF %ERRORLEVEL% == 0 ( 
^& goto :xcopy
^& goto sexec
echo if %%ERRORLEVEL%% EQU 0 echo.Success for Second ping^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
)
echo ELSE (
echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo IF %ERRORLEVEL% == 0 ( 
^& goto :xcopy
^& goto sexec
echo if %%ERRORLEVEL%% EQU 0 echo.Success for Third ping^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
)
echo ELSE (
echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo IF %ERRORLEVEL% == 0 ( 
^& goto :xcopy
^& goto sexec
echo if %%ERRORLEVEL%% EQU 0 echo.Success for Fourth ping^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
)
echo ELSE (
echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo IF %ERRORLEVEL% == 0 ( 
^& goto :xcopy
^& goto sexec
echo if %%ERRORLEVEL%% EQU 0 echo.Success for Fifth ping^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
)
echo ELSE (
echo if %%ERRORLEVEL%% EQU 0 echo.Failed in pinging the server^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
echo if ERRORLEVEL == 1 goto noreach>>c:\temp\%%a.cmd
echo.
)
)
)
)
)

*

echo echo Checking for Access on %%a...>>C:\temp\%%a.cmd
echo if not exist \\%%a\c$ goto noaccess>>c:\temp\%%a.cmd

echo.

echo.

echo cscript //NOLOGO CheckPSexeSvc.vbs %%a>>C:\Temp\%%a.cmd
echo %%a >>"Simpleupdatelogfile - %%date:/=-%%.txt"

*echo :xcopy>>c:\temp\%%a.cmd*
echo xcopy supdate.exe \\%%a\c$\localbin\ /Z /Y>>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the xcopy on %%a on %%date%% %%time%% ^>^>"SimpleupdateErrorlogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Success in performing the xcopy on %%a on %%date%% %%time%% ^>^>"SimpleupdateSuccesslogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd

echo if exist logs\*_patch*.log del logs\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist c:\localbin\*_patch*.log del c:\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist \\%%a\c$\localbin\*_patch*.log del \\%%a\c$\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo title %%a>>c:\temp\%%a.cmd

*echo sexec>>c:\temp\%%a.cmd
*echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateErrorlogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateSuccesslogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateErrorlogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd

echo goto cleanup>>c:\temp\%%a.cmd

echo :noreach>>c:\temp\%%a.cmd
ECHO.
ECHO.
echo echo CANNOT CONNECT to %%a on %%date%% %%time%% ! Skipping patch execution!^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NOT-REACHABLE.log>>c:\temp\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :noaccess>>c:\temp\%%a.cmd
ECHO.
ECHO.
ECHO echo ACCESS DENIED to %%a on %%date%% %%time%% ! Skipping patch execution!^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NO-ACCESS.log>>C:\TEMP\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :cleanup>>c:\temp\%%a.cmd

ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd

ECHO if exist \\%%a\c$\localbin\*_patch*.log COPY /Y \\%%a\C$\localbin\*_patch*.log .\LOGS>>C:\Temp\%%a.cmd

echo :nopatch>>c:\temp\%%a.cmd
echo exit>>c:\temp\%%a.cmd

REM echo if exist c:\temp\%%a.cmd del C:\Temp\%%a.cmd>>c:\temp\%%a.cmd

echo. 
start "%%a" cmd /k "C:\Temp\%%a.cmd&del c:\temp\%%a.cmd&title %%a - DONE"

echo COLOR E>>C:\Temp\%%a.cmd

)

goto end

:single
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\
IF EXIST C:\TEMP\PATCHTHIS1.TXT DEL C:\TEMP\PATCHTHIS1.TXT
ECHO %2>C:\TEMP\PATCHTHIS1.TXT
set updlist=c:\temp\patchthis1.txt

goto multi

:end
if exist c:\temp\patchthis1.txt del c:\temp\patchthis1.txt


----------



## Phanikiran (Jul 28, 2014)

Hi foxidrive,
I had a requirement where i need to implement the logic for pining the server for 5 times ,below is the modified code which is giving error in script.could you help me out in rectifying the errors ,bold ones in the code is the logic i have written and does IF and ELSE support in DOS,pls let me know?

@echo off

if ("%1") == ("") goto noargs
if ("%2") == ("") goto noargs

if ("%1") neq ("reboot") if ("%1") neq == ("install") if ("%1") neq == ("preview") goto CHECKARGS2
GOTO CONTINUE

:CHECKARGS2

if ("%1") neq ("REBOOT") if ("%1") neq == ("INSTALL") if ("%1") neq == ("PREVIEW") goto noargs

:CONTINUE

if exist %2 echo Using file list - %2
if exist %2 set updlist=%2
if exist %2 goto multi

if not exist %2 echo Servername specified - %2
if not exist %2 goto single

:noargs

echo Usage: Simpleupdate [Option] [servername] OR [serverlist.txt] &echo.&echo Options: Preview (check for updates only)&echo Install (install updates but do not reboot)&echo Reboot (install updates and reboot if required) &echo.&echo Example: Simpleupdate install MyIISbox(This will install updates on the server named MyIISbox. &echo.&echo Example: Simpleupdate preview servers.txt (This will check for updates against all servers in the txt file listed.) &Echo.&echo A log output file is created in format ServerName_patch(XYZ) where X=Number of updates found, Y specifies S-uccess/F-ail/E-rror, and Z = Reboot required Y/N. &goto end
goto end

:multi

echo Option specified: %1

FOR /F "eol=; tokens=1* delims=, " %%a in (%updlist%) do (

echo ***********************************************************
Echo Running Simple Update on %%a...
echo ***********************************************************

if NOT exist logs\ md logs\
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\

echo @echo off>>C:\Temp\%%a.cmd

echo echo Checking server up status with ping %%a...>>c:\temp\%%a.cmd

echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo IF %ERRORLEVEL% == 0 ( 
^& goto :xcopy
^& goto sexec
echo if %%ERRORLEVEL%% EQU 0 echo.Success for first ping^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
)
echo ELSE (
echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo IF %ERRORLEVEL% == 0 ( 
^& goto :xcopy
^& goto sexec
echo if %%ERRORLEVEL%% EQU 0 echo.Success for Second ping^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
)
echo ELSE (
echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo IF %ERRORLEVEL% == 0 ( 
^& goto :xcopy
^& goto sexec
echo if %%ERRORLEVEL%% EQU 0 echo.Success for Third ping^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
)
echo ELSE (
echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo IF %ERRORLEVEL% == 0 ( 
^& goto :xcopy
^& goto sexec
echo if %%ERRORLEVEL%% EQU 0 echo.Success for Fourth ping^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
)
echo ELSE (
echo ping -n 1 %%a>null>>c:\temp\%%a.cmd
echo IF %ERRORLEVEL% == 0 ( 
^& goto :xcopy
^& goto sexec
echo if %%ERRORLEVEL%% EQU 0 echo.Success for Fifth ping^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
)
echo ELSE (
echo if %%ERRORLEVEL%% EQU 0 echo.Failed in pinging the server^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
echo if ERRORLEVEL == 1 goto noreach>>c:\temp\%%a.cmd
echo.
)
)
)
)
)

echo echo Checking for Access on %%a...>>C:\temp\%%a.cmd
echo if not exist \\%%a\c$ goto noaccess>>c:\temp\%%a.cmd

echo.

echo.

echo cscript //NOLOGO CheckPSexeSvc.vbs %%a>>C:\Temp\%%a.cmd
echo %%a >>"Simpleupdatelogfile - %%date:/=-%%.txt"

echo :xcopy>>c:\temp\%%a.cmd
echo xcopy supdate.exe \\%%a\c$\localbin\ /Z /Y>>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the xcopy on %%a on %%date%% %%time%% ^>^>"SimpleupdateErrorlogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Success in performing the xcopy on %%a on %%date%% %%time%% ^>^>"SimpleupdateSuccesslogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd

echo if exist logs\*_patch*.log del logs\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist c:\localbin\*_patch*.log del c:\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist \\%%a\c$\localbin\*_patch*.log del \\%%a\c$\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo title %%a>>c:\temp\%%a.cmd

echo sexec>>c:\temp\%%a.cmd
echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateErrorlogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Sucess in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateSuccesslogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleupdateErrorlogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd

echo goto cleanup>>c:\temp\%%a.cmd

echo :noreach>>c:\temp\%%a.cmd
ECHO.
ECHO.
echo echo CANNOT CONNECT to %%a on %%date%% %%time%% ! Skipping patch execution!^>^>"Simpleupdatelogfile - %%date:/=-%%.txt">>C:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NOT-REACHABLE.log>>c:\temp\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :noaccess>>c:\temp\%%a.cmd
ECHO.
ECHO.
ECHO echo ACCESS DENIED to %%a on %%date%% %%time%% ! Skipping patch execution!^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NO-ACCESS.log>>C:\TEMP\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :cleanup>>c:\temp\%%a.cmd

ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*^>^>"Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*>>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd

ECHO if exist \\%%a\c$\localbin\*_patch*.log COPY /Y \\%%a\C$\localbin\*_patch*.log .\LOGS>>C:\Temp\%%a.cmd

echo :nopatch>>c:\temp\%%a.cmd
echo exit>>c:\temp\%%a.cmd

REM echo if exist c:\temp\%%a.cmd del C:\Temp\%%a.cmd>>c:\temp\%%a.cmd

echo. 
start "%%a" cmd /k "C:\Temp\%%a.cmd&del c:\temp\%%a.cmd&title %%a - DONE"

echo COLOR E>>C:\Temp\%%a.cmd

)

goto end

:single
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\
IF EXIST C:\TEMP\PATCHTHIS1.TXT DEL C:\TEMP\PATCHTHIS1.TXT
ECHO %2>C:\TEMP\PATCHTHIS1.TXT
set updlist=c:\temp\patchthis1.txt

goto multi

:end
if exist c:\temp\patchthis1.txt del c:\temp\patchthis1.txt


----------



## Phanikiran (Jul 28, 2014)

Hi can i have the correct logic for above thread code which was giving error in script.could you help me out in rectifying the errors ,bold ones in the code is the logic i have written and does IF and ELSE support in DOS,pls let me know?


----------



## Phanikiran (Jul 28, 2014)

Hi i need to implement these three in below code could you help me out.

1) Ping 5 times in case of failure
2)	Perform PSExec 3 times in case of failure
3)	Perform XCopy in case of failure.

@echo off

if ("%1") == ("") goto noargs
if ("%2") == ("") goto noargs

if ("%1") neq ("reboot") if ("%1") neq == ("install") if ("%1") neq == ("preview") goto CHECKARGS2
GOTO CONTINUE

:CHECKARGS2

if ("%1") neq ("REBOOT") if ("%1") neq == ("INSTALL") if ("%1") neq == ("PREVIEW") goto noargs

:CONTINUE

if exist %2 echo Using file list - %2
if exist %2 set updlist=%2
if exist %2 goto multi

if not exist %2 echo Servername specified - %2
if not exist %2 goto single

:noargs

echo Usage: Simpleupdate [Option] [servername] OR [serverlist.txt] &echo.&echo Options: Preview (check for updates only)&echo Install (install updates but do not reboot)&echo Reboot (install updates and reboot if required) &echo.&echo Example: Simpleupdate install MyIISbox(This will install updates on the server named MyIISbox. &echo.&echo Example: Simpleupdate preview servers.txt (This will check for updates against all servers in the txt file listed.) &Echo.&echo A log output file is created in format ServerName_patch(XYZ) where X=Number of updates found, Y specifies S-uccess/F-ail/E-rror, and Z = Reboot required Y/N. &goto end
goto end

:multi

echo Option specified: %1

FOR /F "eol=; tokens=1* delims=, " %%a in (%updlist%) do (

echo ***********************************************************
Echo Running Simple Update on %%a...
echo ***********************************************************

if NOT exist logs\ md logs\
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\

echo @echo off>>C:\Temp\%%a.cmd

echo echo Checking server up status with ping %%a...>>c:\temp\%%a.cmd

echo ping -n 5 %%a>null>>c:\temp\%%a.cmd
echo if ERRORLEVEL == 1 goto noreach>>c:\temp\%%a.cmd
echo.

echo echo Checking for Access on %%a...>>C:\temp\%%a.cmd
echo if not exist \\%%a\c$ goto noaccess>>c:\temp\%%a.cmd

echo.

echo.

echo cscript //NOLOGO CheckPSexeSvc.vbs %%a>>C:\Temp\%%a.cmd
echo %%a >>"SimpleUpdateLogs/Simpleupdatelogfile - %%date:/=-%%.txt"

echo xcopy supdate.exe \\%%a\c$\localbin\ /Z /Y>>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the xcopy on %%a on %%date%% %%time%% ^>^>"SimpleUpdateLogs/SimpleupdateErrorlogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Success in performing the xcopy on %%a on %%date%% %%time%% ^>^>"SimpleUpdateLogs/SimpleupdateSuccesslogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd

echo if exist logs\*_patch*.log del logs\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist c:\localbin\*_patch*.log del c:\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo if exist \\%%a\c$\localbin\*_patch*.log del \\%%a\c$\localbin\*_patch*.log>>C:\Temp\%%a.cmd
echo title %%a>>c:\temp\%%a.cmd

echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleUpdateLogs/SimpleupdateErrorlogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Sucesss in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleUpdateLogs/SimpleupdateSuccesslogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd

echo goto cleanup>>c:\temp\%%a.cmd

echo :noreach>>c:\temp\%%a.cmd
ECHO.
ECHO.
echo echo CANNOT CONNECT to %%a on %%date%% %%time%% ! Skipping patch execution!^>^>"SimpleUpdateLogs/Simpleupdatelogfile - %%date:/=-%%.txt">>C:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*^>^>"SimpleUpdateLogs/Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NOT-REACHABLE.log>>c:\temp\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :noaccess>>c:\temp\%%a.cmd
ECHO.
ECHO.
ECHO echo ACCESS DENIED to %%a on %%date%% %%time%% ! Skipping patch execution!^>^>"SimpleUpdateLogs/Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*^>^>"SimpleUpdateLogs/Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd
echo COPY /Y NUL .\logs\%%a_patch-NO-ACCESS.log>>C:\TEMP\%%a.cmd
echo goto nopatch>>c:\temp\%%a.cmd

echo :cleanup>>c:\temp\%%a.cmd

ECHO IF EXIST .\LOGS\%%a-not-reachable.log del .\LOGS\%%a-not-reachable.log*^>^>"SimpleUpdateLogs/Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a-no-access* del .\LOGS\%%a-no-access-*^>^>"SimpleUpdateLogs/Simpleupdatelogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd
ECHO IF EXIST .\LOGS\%%a_patch* del .\LOGS\%%a_patch*>>C:\Temp\%%a.cmd

ECHO if exist \\%%a\c$\localbin\*_patch*.log COPY /Y \\%%a\C$\localbin\*_patch*.log .\LOGS>>C:\Temp\%%a.cmd

echo :nopatch>>c:\temp\%%a.cmd
echo exit>>c:\temp\%%a.cmd

REM echo if exist c:\temp\%%a.cmd del C:\Temp\%%a.cmd>>c:\temp\%%a.cmd

echo. 
start "%%a" cmd /k "C:\Temp\%%a.cmd&del c:\temp\%%a.cmd&title %%a - DONE"

echo COLOR E>>C:\Temp\%%a.cmd

)

goto end

:single
if exist C:\Temp\%%a.cmd del C:\Temp\%%a.cmd
if NOT exist C:\Temp\ MD C:\Temp\
IF EXIST C:\TEMP\PATCHTHIS1.TXT DEL C:\TEMP\PATCHTHIS1.TXT
ECHO %2>C:\TEMP\PATCHTHIS1.TXT
set updlist=c:\temp\patchthis1.txt

goto multi

:end
if exist c:\temp\patchthis1.txt del c:\temp\patchthis1.txt


----------



## Phanikiran (Jul 28, 2014)

HI foxidrive how can we implement the below requiement in the code below

2)	Perform PSExec 3 times in case of failure using *for loop*

echo psexec -n 300 -h -s \\%%a cmd /c "title %%a&hostname&echo Running Simple Update...&cd \localbin\&supdate -%1">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% NEQ 0 echo.Error in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleUpdateLogs/SimpleupdateErrorlogfile - %%date:/=-%%.txt">>C:\Temp\%%a.cmd
echo if %%ERRORLEVEL%% EQU 0 echo.Sucesss in performing the psexec on %%a on %%date%% %%time%% ^>^>"SimpleUpdateLogs/SimpleupdateSuccesslogfile - %%date:/=-%%.txt" >>C:\Temp\%%a.cmd


----------

