Solved

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

Posted on 2007-03-22
11
2,284 Views
Last Modified: 2012-05-05
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
loop
-------------
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."
Else
    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
    Loop
'input 1 or add another line with higher number
    if contents = "" then
        contents = 1
    else
        contents = contents + 1
    end if
    downtext.close
    Set downtext= oFSO.OpenTextFile(downfile, 8, True)
    downtext.WriteLine contents
    downtext.close
'if unable to ping 3 consecutive times, email us
    if contents = 3 then
        objShell.Run "c:\admin\serverstatus\email.vbs"
    end if
End If
Loop

'close the text file
oTS.Close
pingfile.Close
'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
        Else
            Ping = False
        End If
    Next
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
Thanks.
0
Comment
Question by:SupportECI
  • 5
  • 5
11 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18772174
Can you simply call it using scheduled tasks?

from a command line...
schtasks /create /ru system /sc onstartup /tn ServerStat /tr "cscript serverstatus.vbs"
0
 
LVL 1

Author Comment

by:SupportECI
ID: 18772244
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?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18772269
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...
0
 
LVL 8

Expert Comment

by:jwarnken
ID: 18772343
Try
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?
0
 
LVL 1

Author Comment

by:SupportECI
ID: 18772490
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:SupportECI
ID: 18772536
no go jwarnken.  I didn't see any window (logged in via RDP).  And it still doesn't finish the serverstatus script.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18772551
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
0
 
LVL 1

Author Comment

by:SupportECI
ID: 18779889
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.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18779983
Pretty slick.. ;^)
Thanx.
0
 
LVL 1

Author Comment

by:SupportECI
ID: 18780868
Okay, I got it all documented (sorta).

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

http://www.62left.com/docs/serverstatus/
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18780951
Very nicely documented!
Thanx for sharing that...
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now