using curl in VBScript
-
Tuesday, November 13, 2012 10:17 PM
Hi, I am trying to pass a variable and return one using cURL in VBScript thus:
SESSION_ID = "curl -s -f --anyauth -u strPortalID -T colFiles GET_SESSION_URI"
I want SESSION_ID to be the value returned by cURL - double quotes assigns it the actual query as a value, AND with literal variable names, not with the variable values I want to use (in italic). I know double quotes are incorrect but single quotes result in a syntax error, and "backquotes" (as used in other shell scripts) aren't allowed.
All Replies
-
Tuesday, November 13, 2012 10:35 PMModerator
Hi,
Two notes:
1. To get standard output of a console (command-line, teletype) program in VBScript, my usual recommendation is to run the console program using cmd.exe and use the ">" redirection operator to redirect its output to a temporary file, read the contents of the temporary file, and then delete it. You can use the WshShell object's Run method to run the cmd.exe command line and the FileSystemObject objects to read the temporary file and delete it.
2. To embed a double-quote character (") in a VBScript string, simply double it; e.g.: "This ""word"" is quoted"
Bill
- Edited by Bill_StewartMicrosoft Community Contributor, Moderator Tuesday, November 13, 2012 10:36 PM Clarification
-
Tuesday, November 13, 2012 10:38 PMModerator
Also: In PowerShell, it's much more straightforward:
PS C:\> $SESSION_ID = curl -s -f ...
Bill
-
Tuesday, November 13, 2012 11:01 PM
Thanks for the prompt reply, Bill.
Will the variables pass to cmd.exe using that method?
Re your point 2, I wasn't trying to embed a double quote, was just wondering what the correct "wrapper'' for the curl statement should be.
-
Wednesday, November 14, 2012 12:05 AMModerator
Will the variables pass to cmd.exe using that method?
What do you mean by "variable," and what do you mean by "pass to cmd.exe"?
Re your point 2, I wasn't trying to embed a double quote, was just wondering what the correct "wrapper'' for the curl statement should be.
You will need to construct a command line like this:
cmd.exe /c curl ... > tempfile
where tempfile is a temporary file name. Then, read in the temporary file and delete it. You have to call cmd.exe to run the command since the output redirection operator (>) is a cmd.exe feature.
You can use the WshShell object's Run method to run the above command, and you can use the FileSystemObject object's TextStream object methods to read the contents of the temporary file and delete it. (Use your favorite search engine to find WshShell Run method and FileSystemObject TextStream.)
Bill
-
Wednesday, November 14, 2012 1:28 AM
Thanks Bill.
"What do you mean by "variable," and what do you mean by "pass to cmd.exe"?"
Excuse my misuse of nomenclature, I'm new to this. I thought a value you set in VBScript is known as a "variable". I define it with "Dim" then I set a value for it. What is the correct terminology?
By variable I mean a value I set in VBScript that I want to be a parameter of the curl command e.g.
curl -s -f --anyauth -u strPortalID -T colFiles GET_SESSION_URI
All items in italic I'm setting values for in my script. I actually want the curl command to do this:
url -s -f --anyauth -u EXAMPLE -T ABC.PDF https://easymail.com/publicinterface/get_session.text?HF2973GPYYC3D7YJBy "pass to cmd.exe" I mean I want the values of the "variables" (please insert correct term here) to be reflected in the curl command.
-
Wednesday, November 14, 2012 3:35 AM
strCmd="curl -s -f --anyauth -u " & strPortalID & " -T colFiles GET_SESSION_URI+
That is how to use a variable.
You need to spend some time learning vbscript. We expect you to write the script. If you do not understand teh names or methods then use the learning materials to learn how to tscript. Post back with specific questions.
¯\_(ツ)_/¯
-
Wednesday, November 14, 2012 3:42 AMModerator
Excuse my misuse of nomenclature, I'm new to this. I thought a value you set in VBScript is known as a "variable". I define it with "Dim" then I set a value for it. What is the correct terminology?
Correct, that's a variable, from VBScript's perspective. I wanted to make sure you weren't referring to an environment variable (which is something different).
By variable I mean a value I set in VBScript that I want to be a parameter of the curl command
VBScript doesn't perform any kind of variable interpolation, so you will need to construct a string value and use the & (string concatenation) operator to build the string. For example:
Dim MyName, Result MyName = "Bill" Result = "Hello, " & MyName & "." ' Result variable contains the string 'Hello, Bill.'
By "pass to cmd.exe" I mean I want the values of the "variables" (please insert correct term here) to be reflected in the curl command.
To do that, build the string (using the aforementioned concatenation operator) and pass it to the WshShell object's Run method to execute.
Bill
- Proposed As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Wednesday, November 14, 2012 2:10 PM
- Marked As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Monday, December 31, 2012 4:05 PM
-
Wednesday, November 14, 2012 3:51 AM
Hello jrv,
I do have a script, I am learning, and I did have a specific question I believe.
Perhaps I don't understand this forum's protocols, is one expected to give their entire script?I think I was actually correct in my terminology when I used the word 'variable'. Bill's question threw me. I thought I knew what a variable was.
I would expect that someone new to this would expect help & guidance when posting in a forum such as this. I challenge you to show me where in this forum, or anywhere else, I could have answered the specific question I asked for myself.
Also, your answer is incorrect, it contains typos. That is not how to use a variable. Deduct 100 points :-)
-
Wednesday, November 14, 2012 3:59 AM
Thanks Bill, that has worked beautifully. BTW I also found that I could capture the result directly, rather than writing a file as per your previous suggestion, by using
ShellCMD = "curl -s -S -f --anyauth -u " & strPortalID & ":" & strPwd & " " & GET_SESSION_URI
SESSION_ID = objShell.Run (ShellCMD)
Thanks, Les
-
Wednesday, November 14, 2012 4:07 AMModerator
Yes, that will work if the output you're seeking is the program's exit code rather than some standard output it's generating.
Bill
-
Wednesday, November 14, 2012 4:09 AM
Thanks Bill, that has worked beautifully. BTW I also found that I could capture the result directly, rather than writing a file as per your previous suggestion, by using
ShellCMD = "curl -s -S -f --anyauth -u " & strPortalID & ":" & strPwd & " " & GET_SESSION_URI
SESSION_ID = objShell.Run (ShellCMD)
Thanks, Les
Shell run returns the process id of the executed process. It does not return a session id.
¯\_(ツ)_/¯
-
Wednesday, November 14, 2012 4:19 AM
Hello jrv,
I do have a script, I am learning, and I did have a specific question I believe.
Perhaps I don't understand this forum's protocols, is one expected to give their entire script?I think I was actually correct in my terminology when I used the word 'variable'. Bill's question threw me. I thought I knew what a variable was.
I would expect that someone new to this would expect help & guidance when posting in a forum such as this. I challenge you to show me where in this forum, or anywhere else, I could have answered the specific question I asked for myself.
Also, your answer is incorrect, it contains typos. That is not how to use a variable. Deduct 100 points :-)
YO are not asking for help butaszking for private lessons. I am suggesting that you do some work on your own and actually learn the basics of scripting with VBScript. The training material on this site can bring you up to speed in a short time. Once you can actually write a script you will be able to ask useful questions that can be answered without a large amount of private tutoring.
The help and guidance is for you to spend some time learning the basics. That is the most important step for you to take.
As in you other post. YOu completely misunderstand how a command in VBBScritp works. YOu also do not know where to go to get the documention on teh commands. It is far more efficient to look up and read teh instructions thant it is to ask broad and poorly targeted questions.
See this as an example: http://technet.microsoft.com/en-us/library/ee156605.aspx
¯\_(ツ)_/¯
-
Wednesday, November 14, 2012 4:35 AMModerator
Shell run returns the process id of the executed process. It does not return a session id.
Actually, it returns the exit code of the process it called if the method's third parameter (bWaitOnReturn) is set to true (see the documentation). Otherwise, it returns zero.
Bill
-
Wednesday, November 14, 2012 6:54 AM
Shell run returns the process id of the executed process. It does not return a session id.
Actually, it returns the exit code of the process it called if the method's third parameter (bWaitOnReturn) is set to true (see the documentation). Otherwise, it returns zero.
Bill
Sorry - you are right. I was thinking about Exec which returns an object containing the processid. In any case it does not return the session id.
In all cases spending time learning and reading the documentation is invaluable.
¯\_(ツ)_/¯
-
Wednesday, November 14, 2012 3:35 PMModerator
In any case it does not return the session id.
Right, unless the session ID happens to be the exit code for the process, which is what I meant when I said
Yes, that will work if the output you're seeking is the program's exit code rather than some standard output it's generating.
But even then, you'd still need to set the third parameter of the Run method call to true in order to cause the calling script to block until the process terminates.
Bill
-
Wednesday, November 14, 2012 3:52 PM
In any case it does not return the session id.
Right, unless the session ID happens to be the exit code for the process, which is what I meant when I said
Yes, that will work if the output you're seeking is the program's exit code rather than some standard output it's generating.
But even then, you'd still need to set the third parameter of the Run method call to true in order to cause the calling script to block until the process terminates.
Bill
The output is an exitcode as listed in the curl man pages. It is not a SESSION_ID. The value being asked for is actually text and is output to the console. This can be captured in a file.
In PowerShell the output can be captured directly to an array and extracted as needed.
GET_SESSION_URI retruns a string and not a nujmber and is not supported in most versions of curl.
¯\_(ツ)_/¯
-
Wednesday, November 14, 2012 3:58 PMModerator
The output is an exitcode as listed in the curl man pages. It is not a SESSION_ID. The value being asked for is actually text and is output to the console. This can be captured in a file.
I don't know the specifics of that tool, but I already said what you're saying. If the console program generates standard output and you need that output, then use WshShell.Run to run the command using cmd.exe, redirect its output to a temporary file, read in the temporary file, and then delete the temporary file.
Also, as noted, this workaround is not needed in PowerShell, as a console program's standard output can be stored in a variable with the assignment operator (=).
Bill

