GrumpyCrouton
GrumpyCrouton

Reputation: 8621

Losing part of a url while passing it from powershell to batch

I've got some powershell that connects to my server and returns an address to be passed to a batch file.

This batch file is a requirement, as it's an HTA/Batch Hybrid that allows me to run a created UI for the project. The URL is being passed as an iframe source to load some results from the server.

I have a variable $content in powershell, which equals http://example.com/selectMultiple.php?choice=[{"id":51,"p":100},{"id":52,"p":94}] (Slightly modified to protect my server address)

I then launch the batch file, while passing that $content variable to it like this

"Launching $content"
Start-Process "files\hybrid.bat" "$content"

In the batch file I have some code that echo's the value that was passed to it.

set "link=%~1"
echo %link%

But after being passed, some of the variable is trimmed. This echos http://example.com/selectMultiple.php?choice - which leads me to believe that there is something with the = sign that is breaking the string.

I've tried urldecode methods in powershell and from my server (php) and neither fixed the issue.

I am at a loss here and would much appreciate any help resolving this issue./

(I tagged PHP as well, to show that I do have the ability to work with the code that is returning the URL)

Upvotes: 2

Views: 185

Answers (1)

JosefZ
JosefZ

Reputation: 30123

Start-Process "files\hybrid.bat" """$content"""

Note that $content variable contains characters (e.g. = and ,) which are treated as parameter delimiters in batch scripting:

Delimiters separate one parameter from the next - they split the command line up into words.

Parameters are most often separated by spaces, but any of the following are also valid delimiters:

  • Comma (,)
  • Semicolon (;)
  • Equals (=)
  • Space ( )
  • Tab ( )

The called batch (see set "link=%~1" command) strips the first supplied parameter from enclosing double quotes in the right way. Hence, you need to pass the string from $content variable enclosed in double quotes from powershell. Use doubled inner double quotes as follows:

#                                ↓            ↓ string delimiters are not supplied
Start-Process "files\hybrid.bat" """$content"""
#                                 ↑↑        ↑↑  escaped inner double quotes are supplied

Double-Quoted Strings (")

When you enclose a string in double quotation marks, any variable names in the string such as "$myVar" will be replaced with the variable's value when the command is processed. … Any embedded double quotes can be escaped using the grave-accent as `" or doubled (replace " with "").

Upvotes: 3

Related Questions