VBS 脚本传递参数?

VBS 脚本传递参数?

大家好,我在 VBS 中有这个脚本:

    On Error Resume Next

If WScript.Arguments.Count < 2 Then
     WScript.Echo "This script requires arguments."
     WScript.Quit
End If

Dim sURL, sAuth, sRequestType
sUrl = "http://192.168.10.6:8080/rest/servicedeskapi/request"
sAuth = "YWRtafW5pc3dRyYXRvcjdddpQdW50bs0dUMyE="
sRequestType = "POST"

Dim sArg1, sArg2, sArg3, sBody

sBody = "{""serviceDeskId"":2,""requestTypeId"":30,""requestFieldValues"":{""summary"":""sArg1"",""description"":""sArg2""}}"

sResponse = HTTPPost(sURL, sBody)
WScript.Echo sResponse

Function HTTPPost(sURL, sRequest)
  Set oHTTP = CreateObject("MSXML2.ServerXMLHTTP")
  oHTTP.open sRequestType, sURL, false
  oHTTP.setRequestHeader "Content-Type", "application/json"
  oHTTP.setRequestHeader "Content-Length", Len(sRequest)
  oHTTP.setRequestHeader "Authorization", "Basic " & sAuth
  oHTTP.send sRequest
  HTTPPost = oHTTP.responseText
End Function

一切似乎都工作正常,期望我在命令行“sArg1,sArg2和sArg3”中传递的变量作为精确文本而不是变量传递,我过去曾这样做过,但我不记得我做了什么来修复它。

csript.exe c:\folder\script.vbs 测试参数1 测试参数2 测试参数3

发现什么问题了吗?我尝试在参数周围使用“”,但似乎没有帮助。

非常感谢

答案1

您在哪里将命令行参数分配给您声明的变量?

Dim sArg1, sArg2, sArg3, sBody

您必须检索并分配变量。

Dim sArg1, sArg2, sArg3, sBody
sArg1 = Wscript.Arguments.Item(0)
sArg2 = Wscript.Arguments.Item(1)
sArg3 = Wscript.Arguments.Item(2)
wscript.Echo "sArg1 = " + sArg1 + "; sArg2 = " + sArg2 + "; sArg3 = " + sArg3

如果您不想的话,您甚至不必先声明它们。

相关内容