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

Parameter vbscript

Hi everyone hope you are well.
I have a vbscript that at the moment, just echoes out the parameters passed.
What Id like to do is to improve it so that I can input a service name, and then it will return what status that service is in.

Output im looking for is:

Server: <server>
Service: <servicename>
Status: eg.stopped, started, running etc.

Any help greatly appreciated.


----------------------------------------------------------------------------------------------------- cscript me.vbs /srv:<servername> /svc:browser
 
 
 
'-- namedCmdLineArgs.vbs - example of how to pass named
'-- arguments to a script
'--
'----------------------------------------------------------
option explicit
 
'----------------------------------------------------------
'-- declare the local variables to be used
'----------------------------------------------------------
dim unamedArgs, namedArgs, scriptName, serverName, serviceName
 
'----------------------------------------------------------
'-- get the name of the running script
'----------------------------------------------------------
scriptName = wscript.scriptname
 
'----------------------------------------------------------
'-- get the named arguments
'----------------------------------------------------------
set namedArgs = wscript.arguments.named
 
'----------------------------------------------------------
'-- check to see if all the required arguments were passed
'----------------------------------------------------------
if not namedArgs.exists("srv") then
   wscript.echo "Usage: " & scriptName & " /srv:<server name> is required"
   wscript.quit
else
   serverName = namedArgs.item("srv")
end if
 
if not namedArgs.exists("svc") then
   wscript.echo "Usage: " & scriptName & " /svc:<service name> is required"
   wscript.quit
else
   serviceName = namedArgs.item("svc")
end if
 
 
 
'----------------------------------------------------------
'-- echo out the arguments
'----------------------------------------------------------
wscript.echo "Server: " & serverName & vbcrlf & "Service: " & serviceName
 
--------------------------------------------------------------------------------------------------

Open in new window

0
Simon336697
Asked:
Simon336697
1 Solution
 
sirbountyCommented:
Try this...
----------------------------------------------------------------------------------------------------- cscript me.vbs /srv:<servername> /svc:browser
 
 
 
'-- namedCmdLineArgs.vbs - example of how to pass named
'-- arguments to a script
'--
'----------------------------------------------------------
option explicit
 
'----------------------------------------------------------
'-- declare the local variables to be used
'----------------------------------------------------------
dim unamedArgs, namedArgs, scriptName, serverName, serviceName
 
'----------------------------------------------------------
'-- get the name of the running script
'----------------------------------------------------------
scriptName = wscript.scriptname
 
'----------------------------------------------------------
'-- get the named arguments
'----------------------------------------------------------
set namedArgs = wscript.arguments.named
 
'----------------------------------------------------------
'-- check to see if all the required arguments were passed
'----------------------------------------------------------
if not namedArgs.exists("srv") then
   wscript.echo "Usage: " & scriptName & " /srv:<server name> is required"
   wscript.quit
else
   serverName = namedArgs.item("srv")
end if
 
if not namedArgs.exists("svc") then
   wscript.echo "Usage: " & scriptName & " /svc:<service name> is required"
   wscript.quit
else
   serviceName = namedArgs.item("svc")
end if
 
 
 
'----------------------------------------------------------
'-- echo out the arguments
'----------------------------------------------------------
wscript.echo "Checking server: " & serverName & vbcrlf & "Service: " & serviceName
 
--------------------------------------------------------------------------------------------------
 
Set objWMIService = GetObject("winmgmts:\\" & serverName & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name = '" & serviceName & "'")
 
For Each objService in colServiceList
  wscript.echo "Server: " & serverName
  wscript.echo "Service: " & serviceName
  wscript.echo "Status: " & objService.State
Next

Open in new window

0
 
Simon336697Author Commented:
Hi sirbounty that works great mate thank you.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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