Solved

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

Posted on 2007-03-22
11
2,292 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

929 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

12 Experts available now in Live!

Get 1:1 Help Now