Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Would like to an if statement to a batch script that will check to see if the script can connect to a computer if not skip the rest of the commands.

Posted on 2007-12-05
7
Medium Priority
?
1,237 Views
Last Modified: 2008-03-07
Would like to an if statement to a batch script that will check to see if the script can connect to a computer if not skip the rest of the commands.

My script
pause
psexec @K9.txt net stop K9 >> C:\Utils\PSTools\PSExeclog.txt
 
@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul

psexec @K9.txt K9nt remove -s >> C:\Utils\PSTools\PSExeclog.txt

@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul


psexec @K9.txt sc config w32time start= auto >> C:\Utils\PSTools\PSExeclog.txt

@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul


psexec @K9.txt net start w32time >> C:\Utils\PSTools\PSExeclog.txt

@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul

psexec @K9.txt sc config Messenger start= auto >> C:\Utils\PSTools\PSExeclog.txt

@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul

psexec @K9.txt net start Messenger >> C:\Utils\PSTools\PSExeclog.txt

pause

I would like to make something like this work but don't know how:

---------------------------
pause
psexec @K9.txt net stop K9 >> C:\Utils\PSTools\PSExeclog.txt
If the script can connect to the computer do:  
               @ping 127.0.0.1 -n 2 -w 1000 > nul
                @ping 127.0.0.1 -n %1% -w 1000> nul

                 psexec @K9.txt K9nt remove -s >> C:\Utils\PSTools\PSExeclog.txt

                @ping 127.0.0.1 -n 2 -w 1000 > nul
                @ping 127.0.0.1 -n %1% -w 1000> nul


                psexec @K9.txt sc config w32time start= auto >> C:\Utils\PSTools\PSExeclog.txt

                 @ping 127.0.0.1 -n 2 -w 1000 > nul
                 @ping 127.0.0.1 -n %1% -w 1000> nul


                 psexec @K9.txt net start w32time >> C:\Utils\PSTools\PSExeclog.txt

                 @ping 127.0.0.1 -n 2 -w 1000 > nul
                @ping 127.0.0.1 -n %1% -w 1000> nul

                 psexec @K9.txt sc config Messenger start= auto >> C:\Utils\PSTools\PSExeclog.txt

                 @ping 127.0.0.1 -n 2 -w 1000 > nul
                 @ping 127.0.0.1 -n %1% -w 1000> nul

                  psexec @K9.txt net start Messenger >> C:\Utils\PSTools\PSExeclog.txt
ELSE
                Exit
pause
--------------------------------------------------------



0
Comment
Question by:rorybrady
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 14

Expert Comment

by:canali
ID: 20416515
you don't need psexec for configuring and starting service...
Bye Gastone Canali
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: K9.txt is the file with the list of your computer
:: cfg_and_start_svc.cmd
:::: byBye Gastone Canali 
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Batch Script Start
@Echo Off
SETLOCAL
pushd %~d0%~p0
set filename=%~n0
set logfile="C:\Utils\PSTools\_%filename%.txt"
:: create empty log files
type nul>%logfile%
 
IF NOT EXIST C:\K9.txt  Goto :_ERR
FOR %%R IN (C:\K9.txt ) Do IF %%~zR EQU 0 Goto :_ERR
 
FOR /F %%c IN ('Type K9.txt') Do (
    Echo Processing: %%c
      PING -n 2 -w 500 %%c|Find /I "TTL" >NUL
      IF NOT ErrorLevel 1 (
        rem if pc is alive do ....
        rem ??? psexec \\%c  remove -s        >> %logfile%
        sc  \\%%c config  w32time start= auto >> %logfile%
        sc  \\%%c start w32time               >> %logfile%
        sc  \\%%c config Messenger start= auto>>%logfile%
        sc  \\%c start Messenger              >> %logfile%
      )ELSE (Echo %%c: Not able to connect )
)      
 
Goto :_END
:_ERR
Echo " C:\K9.txt" file does not exist or file is empty!
:_END
:: Batch Script End

Open in new window

0
 

Author Comment

by:rorybrady
ID: 20416550
There are 2 reasons I am using psexec.

1. I can use it to read the names of computers from a text file.
2. It is easy to run commands on these computers remotly.
0
 
LVL 14

Expert Comment

by:canali
ID: 20417968
For checking if a pc  alive you need to read the computer list from try to ping it and then run psexec (you can't use in this case psexec and @listofcomputers but psexec \\computer ...):
substitute the lines 23-27 wirh your psexec command
 EX. psexec \\%%c net start w32time >> %logfile%

Bye Gas
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:rorybrady
ID: 20421115
This is a great script canali, but I was hoping you could help me modify it. I am tring to put in some error checking but I get an incorrect syntax when I run the script. I am sure it is because I don't have my if/else statments applied correctly.

@Echo Off
SETLOCAL
set filename=%~n0
set logfile="C:\Utils\PSTools\_%PSExeclog%.txt"
:: create empty log files
type nul>%logfile%
 
IF NOT EXIST C:\Utils\PSTools\K9.txt  Goto :_ERR
      FOR %%R IN (C:\Utils\PSTools\K9.txt ) Do IF %%~zR EQU 0 Goto :_ERR
 
            FOR /F %%c IN ('Type K9.txt') Do (
                      Echo Processing: %%c
                        PING -n 2 -w 500 %%c|Find /I "TTL" >NUL
                        
                  IF NOT ErrorLevel 1 (
                        rem if pc is alive do .... >> C:\Utils\PSTools\PSExeclog.txt

                          psexec @K9.txt net stop K9 >> C:\Utils\PSTools\PSExeclog.txt
                        pause
                          IF ErrorLevel 2 (
                              pause
                              rem Service is not running, does not exist or firewall issue. >> C:\Utils\PSTools\PSExeclog.txt
                        )ELSE
                              
                              @ping 127.0.0.1 -n 2 -w 1000 > nul
                              @ping 127.0.0.1 -n %1% -w 1000> nul

                              psexec @K9.txt K9nt remove -s >> C:\Utils\PSTools\PSExeclog.txt            
                                IF ErrorLevel 2 (
                                    pause
                                    rem Problem removing K9 Service >> C:\Utils\PSTools\PSExeclog.txt
                              )ELSE(
                              
                                    @ping 127.0.0.1 -n 2 -w 1000 > nul
                                    @ping 127.0.0.1 -n %1% -w 1000> nul
            
                              
                                    psexec @K9.txt sc config w32time start= auto >> C:\Utils\PSTools\PSExeclog.txt
                                      IF ErrorLevel 2 (
                                          pause
                                          rem Problem changing w32time start option>> C:\Utils\PSTools\PSExeclog.txt
                                    )ELSE

                                    @ping 127.0.0.1 -n 2 -w 1000 > nul
                                    @ping 127.0.0.1 -n %1% -w 1000> nul


                                    psexec @K9.txt net start w32time >> C:\Utils\PSTools\PSExeclog.txt
                                      IF ErrorLevel 2 (
                                          pause
                                          rem Problem starting w32time Service >> C:\Utils\PSTools\PSExeclog.txt
                                    )ELSE

                                    @ping 127.0.0.1 -n 2 -w 1000 > nul
                                    @ping 127.0.0.1 -n %1% -w 1000> nul

                                    psexec @K9.txt sc config Messenger start= auto >> C:\Utils\PSTools\PSExeclog.txt
                                      IF ErrorLevel 2 (
                                          pause
                                          rem Problem changing Messenger start option >> C:\Utils\PSTools\PSExeclog.txt
                                    )ELSE

                                    @ping 127.0.0.1 -n 2 -w 1000 > nul
                                    @ping 127.0.0.1 -n %1% -w 1000> nul

                                    psexec @K9.txt net start Messenger >> C:\Utils\PSTools\PSExeclog.txt
                                      IF ErrorLevel 2 (
                                          pause
                                          rem Problem starting Messenger Service >> C:\Utils\PSTools\PSExeclog.txt
                                    )ELSE

                                    pause
                        )ELSE (Echo %%c: Can ping machine but not modify services >> C:\Utils\PSTools\PSExeclog.txt)
                        )ELSE (Echo %%c: Not able to connect >> C:\Utils\PSTools\PSExeclog.txt )
)      
 
Goto :_END
:_ERR
Echo " C:\Utils\PSTools\K9.txt" file does not exist or file is empty!
:_END
:: Batch Script End
0
 
LVL 14

Accepted Solution

by:
canali earned 2000 total points
ID: 20424686

:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: K9.txt is the file with the list of your computer
:: cfg_and_start_svc.cmd
:::: byBye Gastone Canali 
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Batch Script Start
@Echo Off
SETLOCAL
pushd %~d0%~p0
set filename=%~n0
set logfile="C:\_%filename%.txt" 2^>^&1
:: create empty log files
type nul>%logfile%
set sleep=@ping -n 2 127.0.0.1^>nul
IF NOT EXIST C:\K9.txt  Goto :_ERR
FOR %%R IN (C:\K9.txt ) Do IF %%~zR EQU 0 Goto :_ERR
 
FOR /F %%c IN ('Type c:\K9.txt') Do (
      Echo Processing: %%c
      PING -n 2 -w 500 %%c|Find /I "TTL" >NUL
      IF NOT ErrorLevel 1 (
        Echo Processing: %%c  >> %logfile%
        rem if pc is alive do ....
        rem remove k9 time service
        c:\pro\psexec \\%%c  -i -s k9nt.exe remove -s ||echo ERR removing K9 Service >> %logfile%
        %sleep%
        sc  \\%%c config  w32time start= auto|find /I "FAILED" && echo ERR changing w32time start option >>%logfile%
        %sleep%
        sc  \\%%c start w32time|find /I "FAILED" && echo ERR starting w32time >>%logfile%
        %sleep%
        sc  \\%%c config Messenger start= auto |find /I "FAILED" && echo ERR changing Messenger start option >>%logfile%
        %sleep%
        sc  \\%%c start Messenger|find /I "FAILED" && echo ERR starting messenger >>%logfile%   
        %sleep%
      ) ELSE ( 
               Echo %%c: Not able to connect
               Echo %%c: Not able to connect>> %logfile%
              )
)      
Goto :_END
:_ERR
Echo " C:\K9.txt" file does not exist or file is empty!
:_END
:: Batch Script End

Open in new window

0
 
LVL 14

Expert Comment

by:canali
ID: 20613468
I think the answer was closed with the last batch...
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question