Named Arguments

上一篇 / 下一篇  2010-03-26 22:36:28 / 个人分类:VBScript

Q.

What is one reason for using named arguments?

A.

With named arguments, when you have multiple command-line arguments, you don't need to remember in which order to type the arguments.

Q.

How do you run a script. with named arguments?

A.

To run a script. with named arguments, you use a forward slash and then enter the name of the argument. You follow this with a colon and the value you want to use.

 

YourNameCheckNamedArgCS.vbs

Option Explicit
'On Error Resume Next
Dim computerName
Dim serviceName
Dim wmiRoot
Dim wmiQuery
Dim objWMIService
Dim colServices
Dim oservice
Dim colNamedArguments
Set colNamedArguments = WScript.Arguments.Named
computerName = colNamedArguments("computer")
serviceName = colNamedArguments("service")
subCheckArgs
wmiRoot = "winmgmts:\\" & computerName & "\root\cimv2"
wmiQuery = "Select state from Win32_Service" & " where name = " & "'" & serviceName & "'"

Set bjWMIService = GetObject(wmiRoot)
Set colServices = objWMIService.ExecQuery(wmiQuery)
For Each oservice In colServices
   WScript.Echo serviceName & " Is: " & oservice.state & " on: " & computerName
Next


Sub subCheckArgs
If colNamedArguments.Count < 2 Then
If colNamedArguments.exists("computer") Then
serviceName = "spooler"
WScript.Echo "using default service: spooler"
Else If colNamedArguments.Exists("Service") Then
computerName = "localHost"
WScript.Echo "using default computer: localhost"
Else
WScript.Echo "you must supply two arguments" _
& " to this script." & VbCrLf & "Try this: " _
& "Cscript. checkNamedArgCS.vbs /computer:" _
& "localhost /service:spooler"      
WScript.Quit
End If
End If
End If
End Sub

 

From

Microsoft@ VBScript. Step by Step
By Ed Wilson

 

 

 


TAG:

 

评分:0

我来说两句

Open Toolbar