Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ping / win32_pingstatus and using a text file Query 1

Status
Not open for further replies.

wotsit

MIS
Oct 18, 2002
47
0
0
GB
Hi,

Please forgive me, I'm new to scripting so I haven't got much experience.

I have a script which works okay. It queries some ip addresses, then it reports their Asset details in a spreadsheet, like it's make, model, os etc. I have pasted the code at the bottom of this post.

I have two queries. One is, how can i get it to check if the ip address is online or not before carrying on with the rest of the script. At the moment, if the address is not responding, the script just hangs. I would like it to check it's available, then carry on with the script, if it isn't, i want it to move on to the next address etc. I've done a bit of digging and found example scripts using ping and win32_pingstatus, but I can't seem to get their code to work with mine.

My next query is that at the moment whilst i'm creating/testing, the script uses a handful of ip addresses which are stated in the code. How can i get it to use the contents of a text file instead? I tried entering InputFile="c:\scripts\Servers.txt", Set ifile = iFSO.OpenTextFile(inputfile) but it doesn't seem to work.

Does anybody have any ideas as to where I am going wrong please?

Thank you,
H

This is the working code I use:

'=-=============================
On Error Resume Next

Set WshNetwork = WScript.CreateObject("WScript.Network")

'unc location of the spreadsheet.
strWorkBook = "\\E6400-6HGZ1L1.dudley.local\c$\scripts\audit.xls"

' Create Excel Spreadsheet
Set app = CreateObject("Excel.Application")
app.Workbooks.Open strWorkBook
Set wb = app.ActiveWorkbook
app.Visible = False
wb.Activate
Set ws = wb.Worksheets(1)
ws.Cells(1,1).Value = "Computername"
ws.Columns(1).ColumnWidth = 20
ws.Cells(1,11).Value = "Username"
ws.Columns(11).ColumnWidth = 30
ws.Cells(1,2).Value = "Manufacturer"
ws.Columns(2).ColumnWidth = 20
ws.Cells(1,3).Value = "Model"
ws.Columns(3).ColumnWidth = 20
ws.Cells(1,4).Value = "Serial Number"
ws.Columns(4).ColumnWidth = 30
ws.Cells(1,5).Value = "System Type"
ws.Columns(5).ColumnWidth = 40
ws.Cells(1,10).Value = "Service Pack"
ws.Columns(10).ColumnWidth = 20
ws.Cells(1,12).Value = "Audit Date"
ws.Columns(12).ColumnWidth = 20
ws.Cells(1,9).Value = "Operating System"
ws.Columns(9).ColumnWidth = 40
ws.Cells(1,7).Value = "Domain"
ws.Columns(7).ColumnWidth = 40
ws.Cells(1,8).Value = "Primary Owner"
ws.Columns(8).ColumnWidth = 40
ws.Cells(1,6).Value = "Name Details"
ws.Columns(6).ColumnWidth = 40
ws.Rows(1).Font.Bold = True

arrComputers = Array("192.168.70.24","192.168.70.19","192.168.70.18","192.168.70.17","192.168.70.14","192.168.70.12")
For Each strComputer In arrComputers

' Get the serial number first to see if it already exists in the spreadsheet
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
For Each objItem In colItems
strSerialNumber = "" & objItem.SerialNumber
Next
Set objWMIService = Nothing
Set colItems = Nothing

intRowToUse = -1
For intRowCount = 2 To ws.UsedRange.Rows.Count
If Trim(strSerialNumber) = Trim(ws.Cells(intRowCount,5).Value) Then
intRowToUse = intRowCount
End If
Next
If intRowToUse = -1 Then
intRowToUse = ws.UsedRange.Rows.Count + 1
End If

' Get Computer System Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,1).Value = "" & objItem.Caption
ws.Cells(intRowToUse,11).Value = "" & objItem.UserName
ws.Cells(intRowToUse,2).Value = "" & objItem.Manufacturer
ws.Cells(intRowToUse,3).Value = "" & objItem.Model
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
'Output the Serial Number
ws.Cells(intRowToUse,4).Value = strSerialNumber

'
' Get OS Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,9).Value = "" & objItem.Caption
ws.Cells(intRowToUse,8).Value = "" & objItem.CSDVersion
ws.Cells(intRowToUse,10).Value = "" & FormatNumber(objItem.TotalVisibleMemorySize/1024,0)
Next

' Get System type details Details

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,5).Value = "" & objItem.SystemType
Next

' Get Domain details Details

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,7).Value = "" & objItem.Domain
Next

' Get primary owner name Details

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,8).Value = "" & objItem.PrimaryOwnerName
Next

' Get name Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,6).Value = "" & objItem.Name
Next

Set objWMIService = Nothing
Set colItems = Nothing
'
' Get & Writeout current Date
ws.Cells(intRowToUse,12).value = "" & Day(Now) & "-" & Month(Now) & "-" & Year(Now)

next

'Autofit all columns
app.ActiveSheet.Columns.EntireColumn.AutoFit
' Save Audit File
app.DisplayAlerts = False
'wb.Saved = True
wb.Close True
app.DisplayAlerts = False
app.quit

MsgBox "Done"
'====================

 
1)ping the machine and check the output for a reply. If there is no reply, the machine is likely not on.

Code:
function ping (strComputer)
	ping = false

	set objShell = WScript.CreateObject("WScript.Shell")
	set objExec = objShell.Exec("%comspec% /c ping.exe " & strComputer & " -n 1 -w 100")
	do until objExec.Stdout.AtEndOfStream
		strLine = objExec.StdOut.ReadLine
		if (inStr(strLine, "Reply")) then
			ping = true
			exit function
		end if
	loop
end function

if (ping("192.168.70.24")) then
    'Computer is on
else
    'Computer is off
end if

2) put the IPs on thier own line in a file and iterate the file

Servers.txt
-----------
192.168.70.24
192.168.70.19
192.168.70.18
192.168.70.17
192.168.70.14
192.168.70.12

Code:
set objFSO = CreateObject("Scripting.FileSystemObject")
set objFile = objFSO.OpenTextFile("servers.txt", 1, true, 0)

do while NOT objFile.AtEndOfStream
    strIP = objFile.ReadLine
    if (ping(strIP)) then
        'do stuff
    end if
loop

-Geates



"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
If you would like a detailed explanation on how to structure your script with sample working code, take a look at my FAQ on the subject:

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Also, you only need to set objWMIService once, not every time you want to use it.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top