Discussion:
Script to Ping and write results to file, then check services
(too old to reply)
becky
2008-07-16 04:55:38 UTC
Permalink
I'm pretty new to scripting, but I wrote a script that I want to take
a list of machiens from a file hostnames.txt, use the win32_Pingstatus
to write out the results of the machines successfully pinged and then
use that file to check that auto services were started and attempt to
start the services (it won't start services that have dependencies,
but that's okay).

I'm having trouble with the ping status part, it pings the boxes, but
then it freezes up on the boxes that aren't present. I know the code
works to check services, but I'm having trouble with part that writes
out the whole ping status part. Here is the code:

On Error Resume Next

hostnames = "C:\hostnames.txt"
hostlog = "c:\hostlog.txt"
Const ForWriting = 2
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputFile = objFSO.Opentextfile(hostnames, ForReading, True)
Set OutputFile = objfso.OpenTextFile(hostlog, ForWriting, True)
Do Until objInputFile.atEndofStream
strComputer = objInputFile.ReadLine
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& strComputer & "'")
For Each objStatus in objPing
If objStatus.StatusCode = 0 Then
WScript.Echo strComputer & " is reachable"
OutputFile.WriteLine strComputer
End If
Next
Loop
objInputFile.Close


INPUT_FILE_NAME = "C:\hostlog.txt"
Outputfile = "C:\host2.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FORREADING)
Set OutputFile = objfso.CreateTextFile(Outputfile, ForWriting, True)
strComputers = objFile.ReadAll
objFile.Close
arrComputers = Split(strComputers, vbCrLf)
For Each strComputer In arrComputers
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root
\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where State = 'Stopped' and
StartMode = " _
& "'Auto'")
For Each objService In colRunningServices
Wscript.Echo strcomputer & " " & objService.DisplayName & vbTab
& objService.State
objService.StartService()
outputFile.WriteLine strComputer & (objService.DisplayName) &
vbtab & objService.State
Next
Next

Anyone have suggestions, you are my hero.
urkec
2008-07-17 14:58:01 UTC
Permalink
Post by becky
I'm pretty new to scripting, but I wrote a script that I want to take
a list of machiens from a file hostnames.txt, use the win32_Pingstatus
to write out the results of the machines successfully pinged and then
use that file to check that auto services were started and attempt to
start the services (it won't start services that have dependencies,
but that's okay).
I'm having trouble with the ping status part, it pings the boxes, but
then it freezes up on the boxes that aren't present. I know the code
works to check services, but I'm having trouble with part that writes
On Error Resume Next
hostnames = "C:\hostnames.txt"
hostlog = "c:\hostlog.txt"
Const ForWriting = 2
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputFile = objFSO.Opentextfile(hostnames, ForReading, True)
Set OutputFile = objfso.OpenTextFile(hostlog, ForWriting, True)
Do Until objInputFile.atEndofStream
strComputer = objInputFile.ReadLine
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& strComputer & "'")
For Each objStatus in objPing
If objStatus.StatusCode = 0 Then
WScript.Echo strComputer & " is reachable"
OutputFile.WriteLine strComputer
End If
Next
Loop
objInputFile.Close
INPUT_FILE_NAME = "C:\hostlog.txt"
Outputfile = "C:\host2.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FORREADING)
Set OutputFile = objfso.CreateTextFile(Outputfile, ForWriting, True)
strComputers = objFile.ReadAll
objFile.Close
arrComputers = Split(strComputers, vbCrLf)
For Each strComputer In arrComputers
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root
\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where State = 'Stopped' and
StartMode = " _
& "'Auto'")
For Each objService In colRunningServices
Wscript.Echo strcomputer & " " & objService.DisplayName & vbTab
& objService.State
objService.StartService()
outputFile.WriteLine strComputer & (objService.DisplayName) &
vbtab & objService.State
Next
Next
Anyone have suggestions, you are my hero.
What happens when you remove the On Error Resume Next line?
--
urkec
becky
2008-07-17 18:59:34 UTC
Permalink
Post by urkec
Post by becky
I'm pretty new to scripting, but I wrote a script that I want to take
a list of machiens from a file hostnames.txt, use the win32_Pingstatus
to write out the results of the machines successfully pinged and then
use that file to check that auto services were started and attempt to
start the services (it won't start services that have dependencies,
but that's okay).
I'm having trouble with the ping status part, it pings the boxes, but
then it freezes up on the boxes that aren't present. I know the code
works to check services, but I'm having trouble with part that writes
On Error Resume Next
hostnames = "C:\hostnames.txt"
hostlog = "c:\hostlog.txt"
Const ForWriting = 2
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputFile = objFSO.Opentextfile(hostnames, ForReading, True)
Set OutputFile = objfso.OpenTextFile(hostlog, ForWriting, True)
Do Until objInputFile.atEndofStream
strComputer = objInputFile.ReadLine
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& strComputer & "'")
For Each objStatus in objPing
If objStatus.StatusCode = 0 Then
WScript.Echo strComputer & " is reachable"
OutputFile.WriteLine strComputer
End If
Next
Loop
objInputFile.Close
INPUT_FILE_NAME = "C:\hostlog.txt"
Outputfile = "C:\host2.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FORREADING)
Set OutputFile = objfso.CreateTextFile(Outputfile, ForWriting, True)
strComputers = objFile.ReadAll
objFile.Close
arrComputers = Split(strComputers, vbCrLf)
For Each strComputer In arrComputers
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root
\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where State = 'Stopped' and
StartMode = " _
& "'Auto'")
For Each objService In colRunningServices
Wscript.Echo strcomputer & " " & objService.DisplayName & vbTab
& objService.State
objService.StartService()
outputFile.WriteLine strComputer & (objService.DisplayName) &
vbtab & objService.State
Next
Next
Anyone have suggestions, you are my hero.
What happens when you remove the On Error Resume Next line?
--
urkec
Well today it works just fine. Go figure, no changes, etc. I did try
it removing On error Resume next and it bombs if a machine name is
weird. But putting it back it, it ran, NO OTHER MODIFICATION.
Strange. Could it be because there are issues with connecting to WMI
on a remote machine that is having issues and sending back no or
incorrect information?

Becky
urkec
2008-07-17 20:12:08 UTC
Permalink
Post by becky
Post by urkec
Post by becky
I'm pretty new to scripting, but I wrote a script that I want to take
a list of machiens from a file hostnames.txt, use the win32_Pingstatus
to write out the results of the machines successfully pinged and then
use that file to check that auto services were started and attempt to
start the services (it won't start services that have dependencies,
but that's okay).
I'm having trouble with the ping status part, it pings the boxes, but
then it freezes up on the boxes that aren't present. I know the code
works to check services, but I'm having trouble with part that writes
On Error Resume Next
hostnames = "C:\hostnames.txt"
hostlog = "c:\hostlog.txt"
Const ForWriting = 2
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputFile = objFSO.Opentextfile(hostnames, ForReading, True)
Set OutputFile = objfso.OpenTextFile(hostlog, ForWriting, True)
Do Until objInputFile.atEndofStream
strComputer = objInputFile.ReadLine
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& strComputer & "'")
For Each objStatus in objPing
If objStatus.StatusCode = 0 Then
WScript.Echo strComputer & " is reachable"
OutputFile.WriteLine strComputer
End If
Next
Loop
objInputFile.Close
INPUT_FILE_NAME = "C:\hostlog.txt"
Outputfile = "C:\host2.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FORREADING)
Set OutputFile = objfso.CreateTextFile(Outputfile, ForWriting, True)
strComputers = objFile.ReadAll
objFile.Close
arrComputers = Split(strComputers, vbCrLf)
For Each strComputer In arrComputers
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root
\cimv2")
What happens when you remove the On Error Resume Next line?
--
urkec
Well today it works just fine. Go figure, no changes, etc. I did try
it removing On error Resume next and it bombs if a machine name is
weird. But putting it back it, it ran, NO OTHER MODIFICATION.
Strange. Could it be because there are issues with connecting to WMI
on a remote machine that is having issues and sending back no or
incorrect information?
Becky
I don't know, winmgmts usually connects or returns an error. You could
rewrite the script to use one loop instead of two, and try to connect to
remote WMI service after the ping part is successful.
--
urkec
Tom Lavedas
2008-07-17 20:52:55 UTC
Permalink
Post by urkec
Post by becky
Post by urkec
Post by becky
I'm pretty new to scripting, but I wrote a script that I want to take
a list of machiens from a file hostnames.txt, use the win32_Pingstatus
to write out the results of the machines successfully pinged and then
use that file to check that auto services were started and attempt to
start the services (it won't start services that have dependencies,
but that's okay).
I'm having trouble with the ping status part, it pings the boxes, but
then it freezes up on the boxes that aren't present. I know the code
works to check services, but I'm having trouble with part that writes
On Error Resume Next
hostnames = "C:\hostnames.txt"
hostlog = "c:\hostlog.txt"
Const ForWriting = 2
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputFile = objFSO.Opentextfile(hostnames, ForReading, True)
Set OutputFile = objfso.OpenTextFile(hostlog, ForWriting, True)
Do Until objInputFile.atEndofStream
strComputer = objInputFile.ReadLine
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& strComputer & "'")
For Each objStatus in objPing
If objStatus.StatusCode = 0 Then
WScript.Echo strComputer & " is reachable"
OutputFile.WriteLine strComputer
End If
Next
Loop
objInputFile.Close
INPUT_FILE_NAME = "C:\hostlog.txt"
Outputfile = "C:\host2.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FORREADING)
Set OutputFile = objfso.CreateTextFile(Outputfile, ForWriting, True)
strComputers = objFile.ReadAll
objFile.Close
arrComputers = Split(strComputers, vbCrLf)
For Each strComputer In arrComputers
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root
\cimv2")
What happens when you remove the On Error Resume Next line?
--
urkec
Well today it works just fine. Go figure, no changes, etc. I did try
it removing On error Resume next and it bombs if a machine name is
weird. But putting it back it, it ran, NO OTHER MODIFICATION.
Strange. Could it be because there are issues with connecting to WMI
on a remote machine that is having issues and sending back no or
incorrect information?
Becky
I don't know, winmgmts usually connects or returns an error. You could
rewrite the script to use one loop instead of two, and try to connect to
remote WMI service after the ping part is successful.
--
urkec
Maybe something like this ...

Const ForWriting = 2
Const ForReading = 1

hostnames = "C:\hostnames.txt"
hostlog = "C:\hostlog.txt"
Outputfile = "C:\host2.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set OutputFile_1 = objfso.OpenTextFile(hostlog, ForWriting, True)
Set OutputFile_2 = objfso.CreateTextFile(Outputfile, ForWriting, True)

Set objInputFile = objFSO.Opentextfile(hostnames, ForReading, True)
arrComputers = Split(objInputFile.ReadAll, vbCrLf)
objInputFile.Close

For Each strComputer in arrComputers

Set objPing = GetObject("winmgmts:
{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& strComputer & "'")
For Each objStatus in objPing

If not IsNull(objStatus.StatusCode) Then
If objStatus.StatusCode = 0 Then

WScript.Echo strComputer & " is reachable"
OutputFile_1.WriteLine strComputer & " is reachable"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer _
& "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where State = 'Stopped' " _
& "and StartMode = 'Auto'")

For Each objService In colRunningServices
Wscript.Echo strcomputer & " " & objService.DisplayName _
& vbTab & objService.State
objService.StartService()
outputFile_2.WriteLine strComputer & " " _
& objService.DisplayName & vbtab & objService.State
Next ' Service

End If ' StatusCode = 0
End If ' StatusCode <> Null

Next ' Ping

Next ' strComputer

OutputFile_1.Close
OutputFile_2.Close

(User beware - code is untested, but believed to be accurate.)

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
becky
2008-07-18 05:35:33 UTC
Permalink
On Jul 17, 1:52 pm, Tom Lavedas <***@cox.net> wrote:


Thanks, Tom. That's good feedback and something I'll try for the next
one.
Balaji-j2k
2010-05-10 08:37:59 UTC
Permalink
Tom Lavedas,

The script works

--
Balaji-j2
-----------------------------------------------------------------------
Balaji-j2k's Profile: http://forums.techarena.in/members/218525.ht
View this thread: http://forums.techarena.in/server-scripting/1001529.ht

http://forums.techarena.i

Loading...