Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 04-16-2009, 01:00 PM   #1 (permalink)
 
Banned

Join Date: Feb 2009

Location: Guantanomo Bay, Cuba

Posts: 546

SparkMonkeyHellion is on a distinguished road

Default Batch Script Help Please

Okay, I'm up to no good again...

I want to make a script which pings all of our machines on the network and if a machine doesn't respond it echos that machine name into a txt file.

Here's what I've got so far:

1) Exported CSV of all current machines in AD
2) Created an Excel document which generates the script to ping each machine by name (brilliant I might add)

=""&A2&""&B2&""&C2&"" - here's the formula I used where A contains "ping " and B is the list from AD and C is " -n 1". The end result is that I have a single column with a pretty list of "ping %machinename% -n 1" with all of our machines populating the %machinename% spot.

So it pings everything once, which is enough to determine if it's online or not but now I need a convenient method of finding the machines that don't respond (the output for this script is messy).

Any one know how to do IF>THENS in batch scripts?
SparkMonkeyHellion is offline  
Old 04-16-2009, 02:43 PM   #2 (permalink)
Osiris's Avatar
 

Join Date: Jan 2005

Location: Kentucky

Posts: 32,058

Osiris is a jewel in the roughOsiris is a jewel in the roughOsiris is a jewel in the rough

Send a message via ICQ to Osiris Send a message via AIM to Osiris Send a message via MSN to Osiris Send a message via Yahoo to Osiris Send a message via Skype™ to Osiris
Default Re: Batch Script Help Please

This Vbs script will read a list of machine names from a text file called MachineList.Txt which contains a list of machines names one per line as in the example below:

Machine01
Machine02
Machine08
Machine09

It will then write the ping state as either “On Line” or “Off Line” to a Microsoft Excel spreadsheet.


Vbs Script:


Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2

objExcel.Cells(1, 1).Value = "Machine Name"
objExcel.Cells(1, 2).Value = "Results"

Set Fso = CreateObject("Scripting.FileSystemObject")
Set InputFile = fso.OpenTextFile("MachineList.Txt")

Do While Not (InputFile.atEndOfStream)
HostName = InputFile.ReadLine

Set WshShell = WScript.CreateObject("WScript.Shell")
Ping = WshShell.Run("ping -n 1 " & HostName, 0, True)

objExcel.Cells(intRow, 1).Value = HostName

Select Case Ping
Case 0 objExcel.Cells(intRow, 2).Value = "On Line"
Case 1 objExcel.Cells(intRow, 2).Value = "Off Line"
End Select

intRow = intRow + 1
Loop

objExcel.Range("A1:B1").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit
__________________
Osiris is offline  
Old 04-16-2009, 02:44 PM   #3 (permalink)
Osiris's Avatar
 

Join Date: Jan 2005

Location: Kentucky

Posts: 32,058

Osiris is a jewel in the roughOsiris is a jewel in the roughOsiris is a jewel in the rough

Send a message via ICQ to Osiris Send a message via AIM to Osiris Send a message via MSN to Osiris Send a message via Yahoo to Osiris Send a message via Skype™ to Osiris
Default Re: Batch Script Help Please

and another

'************************************************
'**
'** Ping Script
'**
'** Michael Carpenter
'**
'** May 15,2006
'**
'**
'*************************************************

On Error Resume Next

set WshShell = CreateObject("WScript.Shell")
Result = WshShell.Popup("Searching for machine names or IP addresses from list.txt. " & Chr(13) & "This might take a while depending on number of machines you are trying to find" & Chr(13) & "Please Wait...", 6, "Ping Script")

'Gets the directory where script is running from
'and looks for list.txt for IP addresses

Set objShell = CreateObject("Wscript.Shell")

strPath = Wscript.ScriptFullName

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.GetFile(strPath)

strFolder = objFSO.GetParentFolderName(objFile)

strInputFile = strFolder & "\list.txt"

'************************************************* *****************************

Function ReadTextFile(strInputFile)
'Read contents of text file and return array with one element for each line.

On Error Resume Next

Const FOR_READING = 1

Set objFSO = CreateObject(Scripting.FileSystemObject)
If Not objFSO.FileExists(strInputFile) Then
Set objShell = CreateObject("WScript.Shell")
objShell.Run "taskkill /F /IM excel.exe"
WScript.Echo "Input text file " & strInputFile & " not found."
WScript.Quit

End If

Set objTextStream = objFSO.OpenTextFile(strInputFile, FOR_READING)
If objTextStream.AtEndOfStream Then
Set objShell = CreateObject("WScript.Shell")
objShell.Run "taskkill /F /IM excel.exe"
WScript.Echo "Input text file " & strInputFile & " is empty."
WScript.Quit
End If
arrLines = Split(objTextStream.ReadAll, vbCrLf)
objTextStream.Close

ReadTextFile = arrLines

End Function

'************************************************* *****************************

On Error Resume Next

x = 2

'Open Excel and populate header Information

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Activate
objWorksheet.Name = "Ping Output"

objExcel.Cells(1, 1).Value = "IP Address "
objExcel.Cells(1, 1).Font.Bold = TRUE
objExcel.Cells(1, 1).Font.Size = 12
objExcel.Cells(1, 1).Interior.colorIndex = 15

objExcel.Cells(1, 2).Value = "Reply? "
objExcel.Cells(1, 2).Font.Bold = TRUE
objExcel.Cells(1, 2).Font.Size = 12
objExcel.Cells(1, 2).Interior.colorIndex = 15

Set objRange = objExcel.Range("A1")
objRange.Activate
Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()

Set objRange = objExcel.Range("B1")
objRange.Activate
Set objRange = objExcel.ActiveCell.EntireColumn
objRange.Autofit()

'Ping machines from list and based on reply, enter information in excel

For Each strItem In ReadTextFile(strInputFile)
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping.exe -n 2 -w 1000 " & strItem)
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "ttl") Then

objExcel.Cells(x, 1).Value = strItem
objExcel.Cells(x, 2).Value = "Yes"
x = x + 1

Else

objExcel.Cells(x, 1).Value = strItem
objExcel.Cells(x, 2).Value = "No"
x = x + 1

End If
Next

'************************************************* *****************************

'Make sure that excel is selected

Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)

' Set the ranges so that we can sort.

Set objRange = objExcel.Range("A:B")
Set objRange2 = objExcel.Range("B3")

' Sort Yes / No

objRange.Sort objRange2,1,,,,,,1

'************************************************* *****************************

Answer = MsgBox ("Ping Script Complete!" & Chr(13) & Chr(13) & "Select OK to save Excel" & Chr(13) & "Spreadsheet to Script Directory" & Chr(13) & Chr(13) & "Select Cancel to Leave" & chr(13) & "Spreadsheet Open." & Chr(13), 65, "Ping Program")

If Answer = ("1") Then

set WshShell = CreateObject("WScript.Shell")
Result = WshShell.Popup("Saving Speadsheet " & Chr(13) & "Name: Ping_Information.xls" & Chr(13) & "Location: " & strFolder & Chr(13) & "Please Wait...", 8, "Saving to Script directory")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFolder & "\Ping_Information.old") Then objFSO.DeleteFile(strFolder & "\Ping_Information.old")
If objFSO.FileExists(strFolder & "\Ping_Information.xls") Then objFSO.MoveFile(strFolder & "\Ping_Information.xls") , (strFolder & "\Ping_Information.old")
Set objWorkbook = objExcel.ActiveWorkbook
objWorkbook.SaveAs(strFolder & "\Ping_Information.xls")
objExcel.Quit

Else

End If
__________________
Osiris is offline  
Old 04-16-2009, 04:28 PM   #4 (permalink)
 
Banned

Join Date: Feb 2009

Location: Guantanomo Bay, Cuba

Posts: 546

SparkMonkeyHellion is on a distinguished road

Default Re: Batch Script Help Please

sweet nads of thor! I'll have to try this out tomorrow!
SparkMonkeyHellion is offline  
Old 04-17-2009, 08:27 AM   #5 (permalink)
Osiris's Avatar
 

Join Date: Jan 2005

Location: Kentucky

Posts: 32,058

Osiris is a jewel in the roughOsiris is a jewel in the roughOsiris is a jewel in the rough

Send a message via ICQ to Osiris Send a message via AIM to Osiris Send a message via MSN to Osiris Send a message via Yahoo to Osiris Send a message via Skype™ to Osiris
Default Re: Batch Script Help Please

How did this work for ya?
__________________
Osiris is offline  
Old 04-17-2009, 08:57 AM   #6 (permalink)
 
Banned

Join Date: Feb 2009

Location: Guantanomo Bay, Cuba

Posts: 546

SparkMonkeyHellion is on a distinguished road

Default Re: Batch Script Help Please

I haven't tried it yet, just got to work.
SparkMonkeyHellion is offline  
Old 04-17-2009, 09:36 AM   #7 (permalink)
 
Banned

Join Date: Feb 2009

Location: Guantanomo Bay, Cuba

Posts: 546

SparkMonkeyHellion is on a distinguished road

Default Re: Batch Script Help Please

Okay, the first one is working beautifully!

I'm watching it sit here and autopopulate a nice little excel of all our machines!

Freaking sweet!

I'll try the next ones... next!
SparkMonkeyHellion is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
controlling windows batch files tmshort Programming Discussions 3 09-09-2008 01:52 PM
controlling "change owner to" drop box list in windows using script for a file sangeeta_shetty Windows Operating Systems and Software 0 06-17-2008 05:29 AM
Batch scripting: Startup applications and close batch file LincolnX Windows Operating Systems and Software 7 01-17-2008 08:50 PM
XP Batch script: Ping all PC's, if they all are offline: reboot router LincolnX Programming Discussions 2 12-19-2007 09:08 PM