Trinitrotoluene
Trinitrotoluene

Reputation: 1458

Brackets or quotation marks breaking a powershell command

I know this must be simple but I can't figure out how to do it. I have a string in a variable that is being read in via XML. The variable is called $software and its value is Java Flash Reader "Flash (IE)"

This is then launched from a command line later:

C:\Deployment\Ninite\NiniteOne.exe /select $xmlsoftware $installtype /remote file:"$NiniteTargets" $shortcuts $autoupdate /cachepath "$NiniteCache" $userinterface "$NiniteLog"

The command is breaking when it gets to the "Flash (IE)" portion. I think something is happening with the brackets being in that variable but I am unsure. Any help would be appreciated.

Upvotes: 1

Views: 2367

Answers (2)

Nick
Nick

Reputation: 4362

Try declaring $software like this:

$software = $('Java Flash Reader "Flash (IE)"')

The other option is to try declaring like this:

$software = "Java Flash Reader `"Flash `(IE`)`""

Adding the back tics to escape the ( and "

Upvotes: 0

Joey
Joey

Reputation: 354356

First of all: Why delegate to cmd just for running a program? PowerShell is a shell, too:

blabla.exe /select $software /silent

But the problem is a little more complex than that, even with the complications of cmd's argument parsing removed:

PS> $software = 'Java Flash Reader "Flash (IE)"'
PS> .\echoargs.exe /select $software /silent
arg 1: /select
arg 2: Java Flash Reader Flash
arg 3: (IE)
arg 4: /silent

You can replace quotation marks in the value with \" to make it work, apparently:

PS> $software = $software -replace '"', '\"'
PS> .\echoargs.exe /select $software /silent
arg 1: /select
arg 2: Java Flash Reader "Flash (IE)"
arg 3: /silent

Upvotes: 1

Related Questions