2K/XP batch scripting! Need an expert!!!!!

2K/XP batch scripting! Need an expert!!!!!

Author
Discussion

beanbag

Original Poster:

7,346 posts

242 months

Thursday 31st March 2005
quotequote all
Need some help!

I need to create a batch script that will do the following:

- Get the hostname of the machine the script is running on (using hostname command.....)

- Find out if the file "jview.exe" exists on the machine (Using DIR /b - usually in c:windowssystem32)

- Get the current date (using echo %date%)

- Output all the collected data to .wnt

- email output file to an email address with the subject: "Wintel MS Java Detect: "

Can this be done? I've almost got it but i'm finding the pipe command ( | ), doesn't seem to work in XP which is useless when I come to run this script on 2003 servers!!!!!

Any help would be greatly appreciated!!!

Thanks!

Podie

46,630 posts

276 months

Thursday 31st March 2005
quotequote all
beanbag said:
Can this be done? I've almost got it but i'm finding the pipe command ( | ), ...


Pipe command is ( > )

To append to it, use ( >> )

beanbag

Original Poster:

7,346 posts

242 months

Thursday 31st March 2005
quotequote all
Got that bit but what I need is to add the result of a command to a variable.

ie. Get the output of "hostname", and put it into the variable %current_hostname%.

You need to use | for that but it comes up as an unrecognised command in XP! Works fine in 2K. (Although I still don't know how to do the script!)

Plotloss

67,280 posts

271 months

Thursday 31st March 2005
quotequote all
vbScript is your friend...

beanbag

Original Poster:

7,346 posts

242 months

Thursday 31st March 2005
quotequote all
Actually.....i'd rather be UNIX scripting however I need to collect this data from about 500 servers and most of them have VB Scripting turned off for security reasons!

NT Batch scripting is apparantly my only option plus it can be deployed really easily too.....

Plotloss

67,280 posts

271 months

Thursday 31st March 2005
quotequote all
www.computerhope.com/sethlp.htm

Discusses setting variables...

beanbag

Original Poster:

7,346 posts

242 months

Thursday 31st March 2005
quotequote all
No good but thanks! I need something that's more detailed.

I've even gone down the route of trying to create an assembly program to perform my task!!!! Unfortunately, I haven't touched this stuff for about 6-7 years so i'm struggling to say the least.

I've managed to lock up the command prompt several times due to errors!!! d'oh!

Btw....if someone can work out this script, I'll send them a can of beer!

TheExcession

11,669 posts

251 months

Friday 1st April 2005
quotequote all
In Wink 2K the hostname is in the command spec as %COMPUTERNAME%

type 'set' at a DOS Prompt to see the full environment list

so

echo %COMPUTERNAME%>>Myfile.txt

c:
cd
dir *jview.exe* /s >>Myfile.txt

will find jview.exe for you on the C drive, painfully slow on a big disk though

type Myfile.txt

etc

best
Ex


>> Edited by TheExcession on Friday 1st April 12:18

TheExcession

11,669 posts

251 months

Friday 1st April 2005
quotequote all
Free Command Line emailer - not looked at it too closely but might do the trick for you.

www.beyondlogic.org/solutions/cmdlinemail/cmdlinemail.htm

best
Ex

_Dobbo_

14,423 posts

249 months

Friday 1st April 2005
quotequote all
Depends how married to the idea of using variables? The following writes all the information you need to a file - called "file.txt" so that you can then email this using a command line mailer - which are fairly common

Lets see how the forum butchers this - copy the three following lines into a file called whatever.bat:



echo %date% > file.txt
hostname >> file.txt
dir %systemroot%\system32\jview.exe >> file.txt


>> Edited by _Dobbo_ on Friday 1st April 12:30

beanbag

Original Poster:

7,346 posts

242 months

Friday 1st April 2005
quotequote all
TheExcession said:
In Wink 2K the hostname is in the command spec as %COMPUTERNAME%

type 'set' at a DOS Prompt to see the full environment list

so

echo %COMPUTERNAME%>>Myfile.txt

c:
cd
dir *jview.exe* /s >>Myfile.txt

will find jview.exe for you on the C drive, painfully slow on a big disk though

type Myfile.txt

etc

best
Ex


>> Edited by TheExcession on Friday 1st April 12:18


Excession, your a genius! Problem sorted! This is seriously going to save me a LOT of time!

Many Thanks!

beanbag

Original Poster:

7,346 posts

242 months

Friday 1st April 2005
quotequote all
Btw.....just in case anyone was interested or needed to know the same....here is the script. Very simple yet I didn't know how to get the hostname as the actual filename.


Find MS Java Batch Script said:

@echo off
cls
echo.
echo.FindJava Windows Batch Script v1.0 - By Nick
echo.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
goto find_old_output

--- Search for previous copies of output.txt and rename them to output.bak
--- If the file output.bak exists, it will be deleted

:find_old_output
cd
if exist c:\%computername%.txt goto :rename
goto run

:rename
del c:\%computername%.bak > nul
ren c:\%computername%.txt %computername%.bak
echo.Previous c:\%computername%.txt found! Renamed to c:\%computername%.bak.
echo.
goto run

--- Search for the hostname using command "hostname" and Windows version
--- using the command "ver" and outputting them to %computername%.txt.

:run
echo.Hostname: %computername%>> c:\%computername%.txt
ver >> c:\%computername%.txt
echo. >> c:\%computername%.txt
goto search_java

--- Search for the file "jview.exe" which is located in C:WINDOWSSYSTEM32.
--- If the file is found, it is executed to find the version number.

:search_java
if not exist %systemroot%system32jview.exe goto :nojava
echo.MS Java is installed on this system!>> c:\%computername%.txt
echo. >> c:\%computername%.txt
%systemroot%system32jview.exe | find "Version" >> c:\%computername%.txt
goto output

--- Write to output.txt if no MS Java is found.

:nojava
echo.MS Java is NOT installed on this system>> c:\%computername%.txt
:goto output

--- Display final screen information for user and write the date in output.txt

utput
echo.>> c:\%computername%.txt
echo.Data collected: %date%>> c:\%computername%.txt
echo.Contents of c:\%computername%.txt:
echo.
echo.###################################################
type c:\%computername%.txt
echo.###################################################
echo.
echo.Please email the file: cutput.txt to nick.rennie@hp.com
goto end
:end



P.S. If anyone can now work out a command line script that will attach the newly created batch file to an email and have the subject set as the hostname, that would be an even better bonus and even less work for me!

>> Edited by beanbag on Friday 1st April 15:07

pebbledash

795 posts

267 months

Friday 1st April 2005
quotequote all
Sounds like you may be better off using a VBS of JS (script) file and using all the scripting objects available such as the FSO (file system object) and the network one (computer name etc), youy can then use the CDO (or equiv) to send the e-mial.

hope that makes sense... and is not just alphabet spagetti...

beanbag said:

P.S. If anyone can now work out a command line script that will attach the newly created batch file to an email and have the subject set as the hostname, that would be an even better bonus and even less work for me!

>> Edited by beanbag on Friday 1st April 15:07

beanbag

Original Poster:

7,346 posts

242 months

Friday 1st April 2005
quotequote all
pebbledash said:
Sounds like you may be better off using a VBS of JS (script) file and using all the scripting objects available such as the FSO (file system object) and the network one (computer name etc), youy can then use the CDO (or equiv) to send the e-mial.

hope that makes sense... and is not just alphabet spagetti...


beanbag said:

P.S. If anyone can now work out a command line script that will attach the newly created batch file to an email and have the subject set as the hostname, that would be an even better bonus and even less work for me!

>> Edited by beanbag on Friday 1st April 15:07



Unfortunately I can't use VBS or JS. Reason being is the systems I will be testing are servers and it's on a client environment so no can do! (I did ask).

The limitations of the NT CLI i guess!

pebbledash

795 posts

267 months

Friday 1st April 2005
quotequote all
beanbag said:

Unfortunately I can't use VBS or JS. Reason being is the systems I will be testing are servers and it's on a client environment so no can do! (I did ask).

The limitations of the NT CLI i guess!



Hummmm. if its a 2k or 2003 server the scripting subsystem is installed as part of the O/S not sure you can even remove it...

edited to add. I have a scheduled jub that runs on each of my servers at a predefined time, to archive and cleardown all the log files, and send off an e-mail..

>> Edited by pebbledash on Friday 1st April 15:25

beanbag

Original Poster:

7,346 posts

242 months

Friday 1st April 2005
quotequote all
They're not my systems so I don't want to start playing around with them too much.

Plus I think this output from the batch script gives me all I need and it's very simple so the system owners won't mind running it on their systems.

(I've got about 440 servers to run it on!!!!)

pebbledash

795 posts

267 months

Friday 1st April 2005
quotequote all
beanbag said:
They're not my systems so I don't want to start playing around with them too much.

Plus I think this output from the batch script gives me all I need and it's very simple so the system owners won't mind running it on their systems.

(I've got about 440 servers to run it on!!!!)


I take the point, but I think its all there as standard, for the script side, and seems sillp not to use it. I have lots of scripts and stuff i use regularly and I dont even look at back files these days, logon scripts sheduled tasks etc are all done in JS or VBS. I am not sure how you will do the e-mail in a batch file, unless you resort to some trick code, or install something else, like sendmail. how about the winmgmts interface?

I have a script that can do a full N/W inventory from a single location cute, but hellish to code .

Could you query all the servers from a central location ? It would save a roll out..

pebbledash

795 posts

267 months

Friday 1st April 2005
quotequote all
I have had a good think and a play. And I can only think of three ways to send the e-mail.

1) Use the CDO (or equiv) scripting object. easy to use and all is installed by default.. but you say this can't be used.

2) Install a sendmail type util on each server, but I would imagine this is a no go, due to the install requirement.

3) If IIS is installed on the servers, and is configured for e-mail, you could create an e-mail file (using Echo) and then move it into the pickup directory and let the IIS mailer do the send.

.......

>> Edited by pebbledash on Friday 1st April 16:29

beanbag

Original Poster:

7,346 posts

242 months

Friday 1st April 2005
quotequote all
Had a chat with the client. Unfortunately not all servers have IIS running on Querying all the servers from a central location is also a no go due to firewall restrictions.

It sounds complicated but I've created a depot area on an HPUX box where the output files automatically copied to.

A script is running on the HPUX box that at set intervals, if it detects any new output files, emails them to me.

Sounds complicated but the company I'm working in is hellish when it comes to running anything on a Windows server due to security and I've got to know the HPUX chaps quite well so they were very obliging to create this email script for me!

>> Edited by beanbag on Friday 1st April 16:52

pebbledash

795 posts

267 months

Friday 1st April 2005
quotequote all
Nice solution.

The joys of UNIX... most unfreindly when its working against you, but sometimes its SOOOOO usefull to have one arround.



beanbag said:
Had a chat with the client. Unfortunately not all servers have IIS running on them or even installed so that's a definate no go.

It sounds complicated but he's created a depot area on an HPUX box where the output file automatically copy to.

A script is run on the HPUX box that at set intervals, if it detects any new outfiles, emails them to me.

Sounds complicated but the company I'm working in is hellish when it comes to running anything on a Windows server due to security and I've got to know the HPUX chaps quite well so they were very obliging to create this email script for me!