0

My PowerShell script has one parameter. It is invoked by a tool, which feeds it the argument. The argument contains special characters such as ', " and %. With special characters, PowerShell expects the argument to be surrounded by single or double quotes. What if the argument contains both single and double quotes? No problem. If the argument is surrounded by single quotes, use two single quotes instead of one inside the argument. If it surrounded by double quotes, use two double quotes instead of one inside the argument.

Problem: I cannot modify the argument before passing it to the script, i.e., I cannot double the single/double quotes in the argument. Is there anything I can do?

-Rohan.

EDIT #1: Reason I cannot modify the argument is that it is automatically passed to the script by the tool. EDIT #2: The argument is a password and so, I need to accept and store it as a secure string in the script.

GigaRohan
  • 747
  • 2
  • 8
  • 21
  • 1
    Can you give us a little more to go on? What is passing the argument? Why can't you modify the argument? – TheMadTechnician Sep 23 '14 at 16:48
  • 1
    You can try to pass the argument to a parser/escaper, do what you have to do, and then call PowerShell with the new argument. – tfrascaroli Sep 23 '14 at 16:55
  • If I use PowerShell for the parser, I will face the same problem. Another language may allow me to do this. I haven't looked into using other languages. I'd like to stick to PowerShell. (This isn't a problem in batch scripts. But I can't use a batch script: the argument is a password and so, I need the script to accept and store it as a secure string, which is not available in batch scripts). – GigaRohan Sep 23 '14 at 18:04

1 Answers1

0

One option is to pass it as a base64-encoded string, and then decode it in the script:

Using Powershell -encodedcommand to pass parameters

Community
  • 1
  • 1
mjolinor
  • 66,130
  • 7
  • 114
  • 135
  • But in order to encode it, I still need to accept the argument as a parameter... I understand that the my original script will become a 'child' script. But the 'parent' script, the one that does the encoding and calling of the 'child' script needs to accept the argument and so, we are back to the same issue. – GigaRohan Sep 23 '14 at 18:21
  • Are you sure you have to accept the argument as a parameter? Can you write your script to accept that input from the "tool" through the pipeline, bypassing the parser? – mjolinor Sep 23 '14 at 18:54