Checking if a machine is turned off, and dumping to a log

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
CodyOebel
Posts: 4
Joined: 27 Jul 2012 09:45

Checking if a machine is turned off, and dumping to a log

#1 Post by CodyOebel » 27 Jul 2012 09:52

Im trying to check a list of remote machines to see if any of them are offline.



OK here what what I am trying to achieve via dost\batch.

I have a list of remote machines.

I want to check firs to see if all the list of machines are online. Those that are not online I would like
to output their computer names to a .txt such as using a find with >>.

There are two possibilities going on here to keep in mind!
1. Remote computer is turned off
2. I dont have domain access to the remote computer, and it should return an access error


Those two stipulations above. How can I make a bat file detect them? Any nifty idea's one can think up?
My task is to release the IP address of all the computers in the list. Some of these machines I'm doing test on tell me I dont have access to release the IP's, but others do. By IT policy all computers should be turned on, and kept on, but you know how users turn off machines at the end of their shift.

This said.. I'm trying to OUTPUT to a .txt file those machines that are online, or I dont have access too!

I guess I'm asking is there a way I could script the dos window to "MARK" all of the contents within itself, and paste those contents into a .txt file? Then I could simply run a find command against "Access denied" string, and quickly locate computers that will need to have a tech manually visit.



Code: Select all

psexec \\RemoteComputerName ipconfig /release | clip>>c:\temp\test2.txt
psexec \\RemoteComputerName ipconfig /release | clip>>c:\temp\test2.txt

the clip and pipe method didnt work either lol. Im trying to get the results from the above command
saved into test2.txt. Then at the end of my batch file. I will use a find command to search for my string, the output what lines are found on a InvestigateLog.txt file in which are the machines that are offline\have no access to!

Any help is appreciated thank you!

-Cody Oebel

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Checking if a machine is turned off, and dumping to a lo

#2 Post by foxidrive » 27 Jul 2012 10:46

Maybe this snippet will help you.

With luck the log file can be parsed to extract the info you need then.

Code: Select all

@echo off
for /f %%a in (computerlist.txt) do (
echo ========== %%a ============ >>file.log
psexec \\%%a ipconfig /release >> file.log 2>&1
echo ========================== >>file.log
)

CodyOebel
Posts: 4
Joined: 27 Jul 2012 09:45

Re: Checking if a machine is turned off, and dumping to a lo

#3 Post by CodyOebel » 27 Jul 2012 12:18

Fox I'm going to play around a bit with your script and I will let you know how things work out. I greatly appreciate the time you spent to assist me with this!

Now .. the German in me has to break this down and understand it. Could you please explain to me the %%a, and 2>&1

I'm guessing without googling %%a is a dynamic variable? now the 2>&1 I'm not sure of please if you could to help me understand


Code: Select all

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

    or, if usebackq option present:

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ('string') DO command [command-parameters]
FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Checking if a machine is turned off, and dumping to a lo

#4 Post by foxidrive » 27 Jul 2012 13:03

STDOUT is given the number 1 for its stream. STDERR is given the number 2 for its stream.

In this command

echo abc>file.txt 2>&1

then STDOUT is implied as stream 1 going to the file.txt and the trailing syntax 2>&1 means 'send stream 2 (STDERR) to the same place that stream 1 is going to'.
In fact if you use ECHO ON then you will see a 1>file.txt in the echo command.

In practice this is usually used to redirect standard error text into a file or to nul to hide error messages from appearing on the console.

You can use it to capture error messages into the log file.

And yes %%a is a metavariable used in a for-in-do loop. When tokens are used you can split text into more parts and use %%b %%c %%d etc. The letters are case sensitive too.

CodyOebel
Posts: 4
Joined: 27 Jul 2012 09:45

Re: Checking if a machine is turned off, and dumping to a lo

#5 Post by CodyOebel » 27 Jul 2012 13:33

Foxidrive I wanted to reply to in great thanks!
I appreciate the time of your day that youll never get back in order to pass your knowledge to me and help out. That is priceless , and I can only say Thank you with equal priceless appreciation in replying back with a thank you!

CodyOebel
Posts: 4
Joined: 27 Jul 2012 09:45

Re: Checking if a machine is turned off, and dumping to a lo

#6 Post by CodyOebel » 27 Jul 2012 14:50

Foxidrive so the &1 is kind of like a c++ pointer reference\dereference to a variables address, but in this case it's a .txt file?
So 2(All Error data) output to address\object of 1 that object being a .txt file?

Ok, so since STDERR is given the number 2 for it's stream, can I check a return error of 2 ?

Code: Select all

like  IF ERRORLEVEL 2  
echo %%a >> file.txt


I really dont want anything but that which errors to be placed to a log file.
So dual streaming both output, and error output is not really necessary for my needs.

So basically if any error is encountered in my loop
outputting that machines name to the file.txt is what I need.
Then I send a tech to go visit the station. :)

Ed Dyreen
Expert
Posts: 1441
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Checking if a machine is turned off, and dumping to a lo

#7 Post by Ed Dyreen » 27 Jul 2012 14:59

'
You mean like

Code: Select all

@echo off

2> file.log (

       for /f %%a in (computerlist.txt) do (
              echo ========== %%a ============
              psexec \\%%a ipconfig /release
              echo ==========================
       )
)

Post Reply