• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2365
  • Last Modified:

VBscript run as Service with svrany.exe - Will not complete called script

I need to run a vbscript as a service (or somehow else run it continuously) on my W2k3 server.  I tried using srvany.exe.  It installs the service, and I see wscript.exe in task manager, I also see it call the second script (2 instances of wscript.exe).  But the second task never completes.  After 5 minutes, I see it kick off another instance of the script.
When these are run simply by double clicking they all work perfectly.  But I need this script to run even when I am not logged on (or anyone else for that matter).  Below is the code to my scripts:
Set objShell = Wscript.CreateObject("WScript.Shell")
i = 1
do while i > 0
objShell.Run "c:\admin\serverstatus\serverstatus.vbs"
Wscript.sleep 300000
as you can see, all it does is call another script, and wait for 5 minutes, then do it again.  Here is the code to serverstatus.vbs:
sFile1 = "serverlist.txt"
Set objShell = Wscript.CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
'pings computers, records results, passes to webserver
Set pingfile= oFSO.CreateTextFile("ping.txt", True)

'open list of client names
Set oTS = oFSO.OpenTextFile(sFile1)
allgood = 0
'get server names from text file
Do Until oTS.AtEndOfStream
    contents = ""
    'get next client name
    sClient = oTS.ReadLine
If Ping(sClient) = True Then
    pingfile.WriteLine sClient & " is pingable."
    pingfile.WriteLine sClient & " is NOT pingable."
'this line prevents the allgood.vbs from running--
'that script deletes any files created due to unable to ping
    allgood = 1
'creates or opens a text file named <server>.txt
    downfile = sClient & ".txt"
    Set downtext= oFSO.OpenTextFile(downfile, 1, True)
'read the last line
    Do Until downtext.AtEndOfStream
        contents = downtext.ReadLine
'input 1 or add another line with higher number
    if contents = "" then
        contents = 1
        contents = contents + 1
    end if
    Set downtext= oFSO.OpenTextFile(downfile, 8, True)
    downtext.WriteLine contents
'if unable to ping 3 consecutive times, email us
    if contents = 3 then
        objShell.Run "c:\admin\serverstatus\email.vbs"
    end if
End If

'close the text file
'this copies the file to the webserver so I can check our server status
Wscript.sleep 1500
oFSO.DeleteFile "\\webserver\c$\Inetpub\wwwroot\serverstatus\ping.txt", True
Wscript.sleep 1500
oFSO.CopyFile "c:\admin\serverstatus\ping.txt", "\\webserver\c$\Inetpub\wwwroot\serverstatus\"
'if no missed pings, then call this to clean up
if allgood = 0 then
    objShell.Run "c:\admin\serverstatus\allgood.vbs"
end if

'ping function from www.robvanderwoude.com
Function Ping( myHostName )
' This function returns True if the specified host could be pinged.
' myHostName can be a computer name or IP address.
' The Win32_PingStatus class used in this function requires Windows XP or later.
' This function is based on the TestPing function in a sample script by Don Jones
' http://www.scriptinganswers.com/vault/computer%20management/default.asp#activedirectoryquickworkstationinventorytxt

    ' Standard housekeeping
    Dim colPingResults, objPingResult, strQuery

    ' Define the WMI query
    strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & myHostName & "'"

    ' Run the WMI query
    Set colPingResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )

    ' Translate the query results to either True or False
    For Each objPingResult In colPingResults
        If Not IsObject( objPingResult ) Then
            Ping = False
        ElseIf objPingResult.StatusCode = 0 Then
            Ping = True
            Ping = False
        End If
End Function

And that one does a lot of stuff.  I threw in some comments.  I hope you have a script editor with color coding.

Please let me know if you see anything as to why my script will not run as a service under svrany.exe
  • 5
  • 5
1 Solution
Can you simply call it using scheduled tasks?

from a command line...
schtasks /create /ru system /sc onstartup /tn ServerStat /tr "cscript serverstatus.vbs"
SupportECIAuthor Commented:
I thought that ran similar to startup scripts and therefore since the script would never complete, the computer would never actually boot.  Am I wrong about that?
Not to my knowledge...you can use either onstartup, or schedule it at a certain time each day (/sc weekly /d MON,TUE,WED,etc) - at 12:01:00 if you'd rather.
But since your script loops - the onstartup seemed the best option to me...
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

objShell.Run "c:\admin\serverstatus\serverstatus.vbs",1,True

If you are logged into the console you should see the window with the script.
Also is the service using the local system account or a domain account?
SupportECIAuthor Commented:
Okay, that seems to work.  The main issue is that I don't have any way to restart it if it fails.  I think I can use some scheduled tasks to call vbscripts to check if the wscript.exe is running.  Any experience with that?

If anyone has any clue about getting it to run a service, I would still prefer that method.
SupportECIAuthor Commented:
no go jwarnken.  I didn't see any window (logged in via RDP).  And it still doesn't finish the serverstatus script.
Something like this...

Dim objWMI: Set objWMI = GetObject("winmgmts://./root/cimv2")
Set colProcess = objWMI.ExecQuery ("Select * from Win32_Process Where Name='cscript.exe'")
If colProcess.Count = 0 Then ' not running
  Wscript.Shell.Run "yourscript.vbs"
End If
SupportECIAuthor Commented:
Thanks sirbounty,

I now have a fully functional server status webpage, complete with email notification.  If anyone wants to view how I did the whole thing you can see it at www.62left.com/docs/serverstatus/
The documentation is incomplete right now, but eventually I will have it cleaned up and completed.
Pretty slick.. ;^)
SupportECIAuthor Commented:
Okay, I got it all documented (sorta).

Feel free to email me if you want to set it up and have questions.

Very nicely documented!
Thanx for sharing that...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now