VBS Script passing argument

scriptingvbscriptwindows-command-prompt

Hello everyone I have this script in 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

Everything seems to work fine expect the variables that i pass in the command line "sArg1, sArg2 and sArg3" get passed as exact text instead of variables, I have had this working on the past but I don't remember what I did to fix it.

csript.exe c:\folder\script.vbs testsarg1 testsarg2 testsarg3

Anything see anything wrong? I have tried playing around with "" around the args but that doesn't seem to help.

Many Thanks

Best Answer

Where are you assigning your command line arguments to the variables you declared?

Dim sArg1, sArg2, sArg3, sBody

You have to retrieve and assign the variables.

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

You don't even have to declare them first if you don't want to.

Related Topic