We help IT Professionals succeed at work.

Parameter vbscript

Simon336697
Simon336697 asked
on
Medium Priority
665 Views
Last Modified: 2012-05-06
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

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Hi sirbounty that works great mate thank you.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.