2

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)

10
  • cant you get rid of batch file and do stuff with powershell directly? can you dump text to a file and read it in the batch file (as a workaround)? can you save text to env variable and read it in the batch file? Commented Jun 13, 2017 at 15:34
  • @4c74356b41 I can't get rid of the batch file, I'm using it as an HTA/Batch Hybrid. The url is being passed to basically open an HTA and load an iframe with the URL. I could probably dump it to a file as a workaround, but I feel like that would be a slower approach and for this application I need to to be as fast as possible. Commented Jun 13, 2017 at 15:35
  • Windows command prompt takes the =-sign as a token separator just like the space; perhaps it helps when you use set "link=%*" in your batch file as you are passing only the link anyway... Commented Jun 13, 2017 at 16:07
  • 2
    Start-Process "files\hybrid.bat" """$content""" note tripled double quotes. Commented Jun 13, 2017 at 16:08
  • @JosefZ That did it. I didn't think it would have to be quotes multiple times. I never would have thought of that ^.^ Could you please post as an answer and explain why this needs to be done? Commented Jun 13, 2017 at 16:09

1 Answer 1

3
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 "").

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.