【发布时间】:2020-02-24 09:23:11
【问题描述】:
我想在验证该参数之前测试是否提供了特定的命名参数,因此我可以为缺失和无效条件提供有意义的错误代码。 我现在有这个
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colArgs = WScript.Arguments.Named
If colArgs.Item("Script") Then
If not objFSO.FileExists(colArgs.Item("Script")) Then
intReturn = 1805
End If
Else
intReturn = 1639
End If
If Not intReturn Then
msgBox colArgs.Item("Script"), 0, "Script"
Else
msgBox intReturn, 0, "Error"
End If
我的期望是,如果我根本不提供一个名为 Script 的参数,我会得到值为 1639 的 Error msgBox。相反,我得到了好的 msgBox,其中 Script 为空白。 我也试过了
If Not colArgs.Item("Script") = "" Then
编辑:根据@Tomalak,我现在有了这个
Option Explicit
Dim objShell, objFSO, colArgs, intReturn
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colArgs = WScript.Arguments.Named
If Not IsEmpty(colArgs.Item("Script")) Then
If Not objFSO.FileExists(colArgs.Item("Script")) Then
intReturn = 1805
End If
Else
intReturn = 1639
End If
If IsEmpty(intReturn) Then
msgBox colArgs.Item("Script"), 0, "Script"
Else
msgBox intReturn, 0, "Error"
End If
为了它的价值,我像这样从 PowerShell 调用 VBScript
$script = "\\Mac\iCloud Drive\Px Tools\Dev 4.0\#Spikes\Helper\Helper Target.ps1"
$arguments ="`"\\Mac\iCloud Drive\Px Tools\Dev 4.0\#Spikes\Helper\PxHelper.vbs`" //nologo /script:`"$script`" /wait:1"
Start-Process -filePath:Wscript.exe -argumentList:$arguments
现在即使提供了脚本,我也会得到错误情况。 Grrr,星期一。
【问题讨论】:
-
@tomalak 我认为这很明显可以假设,但无论如何都要修改。
-
最好始终定义您在示例代码中使用的所有内容。
标签: vbscript command-line-arguments