验证vbScript参数名称 [英] Validate vbScript argument names

查看:344
本文介绍了验证vbScript参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从PowerShell调用vbScript,它带有在文本字符串中定义的参数,并且我想验证所使用的参数是否正确.假设我的有效参数是 ARG1 ARG2 & ARG3 ,并且某人有一个字符串"/ARGG1:one/ARG2:two" ,我想针对有效参数数组来验证命名参数,并捕获该 ARGG1 .我要失败的地方是获取命名参数的名称.例如,我可以使用 WScript.Arguments.Named("ARG1")引用特定的命名参数.但是我似乎无法遍历WScript.Arguments.Named并返回使用的实际参数名称.像这样

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

推荐答案

在迭代 Named 参数集合时,您正在访问键(参数名称)而不是 WshNamed 对象本身.如果将密钥传递回集合中,则可以访问该命名参数的值.

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

输出:

ARG1
one
ARG2
two

这篇关于验证vbScript参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆