Solved

running vbs script on windows 2012

Posted on 2014-12-11
11
406 Views
Last Modified: 2015-01-16
I have this VBS script that worked fine in windows 2008 R2 but in windows 2012 R2 I get the following error.

Line:  5
Char: 1
Error: Subscript out of range
Code: 800A0009
Source: MIcrosoft VBscript runtime error

Going by that error char 1 on line 5 would be 's' from strComputer. Is that no longer used in windows 2012?

Here is the VBS script (Below)

'Declare Variables
Dim objWMIService, objProcess, colProcess, Status, strComputer, strService
 
'Assign Arguments
strComputer = WScript.Arguments(0)
strService = WScript.Arguments(1)
Status= false
 
'Check For Arguments - Quit If None Found
If Len(strService) < 1 Then
    Wscript.echo "No Arguments Entered - Exiting Script"
    WScript.Quit
End If
 
'Setup WMI Objects
Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("SELECT DisplayName, Status, State FROM Win32_Service WHERE DisplayName = '" & strService & "'")
 
'Check For Running Service
For Each objProcess in colProcess
    If InStr(objProcess.DisplayName,strService) > 0 And objProcess.State = "Running" Then
      Status = true
    End If
Next
 
If Status = true Then
    Wscript.echo "Service: " & UCase(strComputer) & " " & strService & " Running"
    'Perform Some Pass Logic Here
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "service@mycompany.com"
objEmail.To = "admin@mycompany.com"
objEmail.Subject = "service is Running"
objEmail.Textbody = ""
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
"92ycas1.int.92y.org"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send

Else
    Wscript.echo "Service: " & UCase(strComputer) & " " & strService & " Not Running"
    'Perform Some Failed Logic Here
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "Service@mycompany.org"
objEmail.To = "admin@companyemail.com"
objEmail.Subject = "Service has stopped"
objEmail.Textbody = "ATTENTION is NEEDED"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
"companyemail.com"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
      
End If
0
Comment
Question by:iamuser
11 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 40494109
out of range means your trying to access and object that does not exist on the machine

OR your array isn't big enough to hold the data your trying to get
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 40494114
out of range means your trying to access and object that does not exist on the machine

OR

your array isn't big enough to hold the data your trying to get
0
 

Author Comment

by:iamuser
ID: 40494710
so how do I fix this? the service does exist on the server. I installed it myself
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 22

Expert Comment

by:plusone3055
ID: 40494721
Change  WScript.Arguments(0)
to  WScript.Arguments(1)

and  tell me if you receive the same error :)
0
 

Author Comment

by:iamuser
ID: 40494753
I did, it still error out
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 40494839
same error ?
0
 

Author Comment

by:iamuser
ID: 40495481
yup same error with this change
 
'Assign Arguments
strComputer = WScript.Arguments(1)
strService = WScript.Arguments(1)
Status= false
0
 

Author Comment

by:iamuser
ID: 40502680
anyone?
0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 total points
ID: 40510061
Move this code:
'Check For Arguments - Quit If None Found
If Len(strService) < 1 Then
    Wscript.echo "No Arguments Entered - Exiting Script"
    WScript.Quit
End If

Open in new window


to: before the 'Assign Arguments part.
And... supply at least one argument when calling the program.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40553076
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

803 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