Results 1 to 5 of 5

Thread: Script to Ping and write results to file, then check services

  1. #1
    becky Guest

    Script to Ping and write results to file, then check services

    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.

  2. #2
    urkec Guest
    What happens when you remove the On Error Resume Next line?

    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.

  3. #3
    becky Guest

    Re: Script to Ping and write results to file, then check services

    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?

  4. #4
    Tom Lavedas Guest
    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.)

  5. #5
    Join Date
    May 2010
    Posts
    1

    Re: Script to Ping and write results to file, then check services

    The script works.

Similar Threads

  1. Bad ping results on pingtest.net
    By Quasim in forum India BroadBand
    Replies: 4
    Last Post: 08-08-2011, 10:26 PM
  2. Exclude results in SQL script
    By Hamlet in forum Software Development
    Replies: 4
    Last Post: 14-10-2009, 09:20 PM
  3. Ping results in cisco routers
    By jade in forum Networking & Security
    Replies: 3
    Last Post: 22-07-2009, 10:59 PM
  4. Check Here Maharashtra HSC Results - MAR 2009
    By Aadarsh in forum Education Career and Job Discussions
    Replies: 1
    Last Post: 03-06-2009, 06:11 PM
  5. Check here IIT JEE 2009 Results
    By Rahul45 in forum Education Career and Job Discussions
    Replies: 1
    Last Post: 25-05-2009, 09:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,992,067.59783 seconds with 16 queries