I am trying to write a script that will test a range of IP addresses to find machines that are on.
I wrote and ran a script last night that pings every machine in a subnet, and writes out a file with the ones that responded. The problem is that it missed a bunch of machines I know were on overnight.
So I thought, maybe ping is not the best method. Maybe Ill use WMI to attempt to pull the computer name out of each IP in the loop. The ones that work are on, the ones that don't...are off. The problem here is that the unresponsive ones take nearly 20 seconds to timeout and move to the next one, making the script take way too long.
Questions:
1. Is there a better way to do this?
2. Is there a way to do have a VBS attempt a task for a certain amount of time then move on? I would like to lower the timeout on getting the computer name to 2 or 3 seconds, instead of nearly 20.
Here's my current working version of the script, it pings machines, and for every IP in the loop, it attempts to get the computer name as well.
Thanks,
Andrew
Hard work often pays off over time, but procrastination pays off right now!
I wrote and ran a script last night that pings every machine in a subnet, and writes out a file with the ones that responded. The problem is that it missed a bunch of machines I know were on overnight.
So I thought, maybe ping is not the best method. Maybe Ill use WMI to attempt to pull the computer name out of each IP in the loop. The ones that work are on, the ones that don't...are off. The problem here is that the unresponsive ones take nearly 20 seconds to timeout and move to the next one, making the script take way too long.
Questions:
1. Is there a better way to do this?
2. Is there a way to do have a VBS attempt a task for a certain amount of time then move on? I would like to lower the timeout on getting the computer name to 2 or 3 seconds, instead of nearly 20.
Here's my current working version of the script, it pings machines, and for every IP in the loop, it attempts to get the computer name as well.
Code:
Dim strHost
Dim strResultText
On Error Resume Next
fixedMonth = Right("0" & Month(Now),2)
fixedDay = Right("0" & Day(Now),2)
fixedHour = Right("0" & hour(Now), 2)
fixedMin = Right("0" & Minute(Now), 2)
outputFileName = ".\scripts\pingResults\" & Year(Date) & "-" & fixedMonth & "-" & fixedDay & " " & fixedHour & fixedMin & " Live PCs.csv"
Dim oShell
Set oShell = CreateObject("Wscript.Shell")
strResultText = "IP,Host,Date-Time" & vbCrLf
For octet3 = 153 To 155
For octet4 = 1 To 255
strHost = "192.168." & octet3 & "." & octet4
ping strHost
Next
Next
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (outputFileName, ForWriting, True)
Set objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery("Select * from Win32_Service")
For Each objService in colServices
objTextFile.WriteLine(strResultText)
Next
objTextFile.Close
'Wscript.echo "Done."
Function ping(strHost)
dim objPing, objRetStatus
set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery ("select * from Win32_PingStatus where address = '" & strHost & "'")
For Each objRetStatus in objPing
if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 Then
pingSuccess = False
Else
pingSuccess = True
end if
Next
MsgBox "1"
hostName = getComputerName (strHost)
MsgBox "2"
If pingSuccess = True Then
strResultText = strResultText & strHost & "," & hostName & "," & Now & ",yes" & vbCrLf
Else
strResultText = strResultText & strHost & "," & hostName & "," & Now & ",no" & vbCrLf
End If
MsgBox strResultText
End Function
Function getComputerName(strHost)
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strHost & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
getComputerName = objComputer.Name
Next
End Function
Thanks,
Andrew
Hard work often pays off over time, but procrastination pays off right now!