问题
I am calling a vbScript from PowerShell, with arguments that are defined in a text string, and I want to validate that the arguments being used are correct. Say my valid arguments are ARG1, ARG2 & ARG3, and someone has a string of "/ARGG1:one /ARG2:two"
, I want to validate the named arguments against an array of valid arguments and catch that ARGG1.
Where I am failing is in getting the names of the named arguments. I can reference a particular named argument with WScript.Arguments.Named("ARG1")
for example. But I can't seem to loop through WScript.Arguments.Named and return the actual argument names used.
Something like this
For Each Argument In WScript.Arguments.Named
msgBox Argument.name
Next
回答1:
When iterating the Named
argument collection you are accessing the key (argument name) not the WshNamed
object itself. If you pass the key back into the collection you will be able to access the value for that Named Argument.
Dim key
For Each key In WScript.Arguments.Named
Dim argument: argument = WScript.Arguments.Named.Item(key)
Call WScript.Echo(key) 'Return argument name
Call WScript.Echo(argument) 'Return argument value
Next
Output:
ARG1
one
ARG2
two
来源:https://stackoverflow.com/questions/60446977/validate-vbscript-argument-names