Scripting

VBS 腳本傳遞參數?

  • October 10, 2017

大家好,我在 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 測試arg1 測試arg2 測試arg3

有什麼看到有什麼不對嗎?我曾嘗試在 args 周圍使用 "" ,但這似乎無濟於事。

非常感謝

您在哪裡將命令行參數分配給您聲明的變數?

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

如果您不想,您甚至不必先聲明它們。

引用自:https://serverfault.com/questions/877689