[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2349
  • 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...
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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