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.
Bookmarks