1

I have an issue with passing a parameter with spaces into a powershell script. It seems to only work with the first part of the parameter string before the space

[CmdletBinding()]
Param(

 [Parameter(Mandatory=$True, ParameterSetName='firewallCommand',Position=0,HelpMessage="Input Firewall Index")]
 [string]$fwName,

 [Parameter(Mandatory=$True, ParameterSetName='firewallCommand',Position=1,  HelpMessage="Input Firewall Command")]
 [string]$fwCommand)

Within the script this will pass the parameters to the following command as such:

 cprid_util -server $firewallIpAdd -verbose rexec -rcmd  "$fwCommand"

On the command line if I run this it works:

.\fwcommand_run.ps1 -fwName firewall-name -fwCommand "ps"

If I run this it doesn't work at all:

 .\fwcommand_run.ps1 -fwName firewall-name -fwCommand "ps -ef"

But if I invoke the command directly it does work:

cprid_util -server 10.1.128.4  -verbose rexec -rcmd  ps -ef

2 Answers 2

2

$fwCommand are registered as a single value. Echoargs from Powershell Community Extensions are very useful for situations like this:

$fwCommand = "ps -ef"
$firewallIpAdd = "127.0.0.1"

$command = "cprid_util -server $firewallIpAdd -verbose rexec -rcmd '$fwCommand'"
[management.automation.psparser]::Tokenize($command,[ref]$null) | Format-Table Content, Type -AutoSize

Content                Type
-------                ----
cprid_util          Command
-server    CommandParameter
127.0.0.1   CommandArgument
-verbose   CommandParameter
rexec       CommandArgument
-rcmd      CommandParameter
ps -ef               String

.\EchoArgs.exe -server $firewallIpAdd -verbose rexec -rcmd $fwCommand

Arg 0 is <-server>
Arg 1 is <127.0.0.1>
Arg 2 is <-verbose>
Arg 3 is <rexec>
Arg 4 is <-rcmd>
Arg 5 is <ps -ef>

You can split up the values on whitespace or use Invoke-Expression.

$firewallIpAdd = "127.0.0.1"
$fwCommand = "ps -ef"
Invoke-Expression ".\EchoArgs.exe -server $firewallIpAdd -verbose rexec -rcmd $fwCommand"

Arg 0 is <-server>
Arg 1 is <127.0.0.1>
Arg 2 is <-verbose>
Arg 3 is <rexec>
Arg 4 is <-rcmd>
Arg 5 is <ps>
Arg 6 is <-ef>
Sign up to request clarification or add additional context in comments.

4 Comments

Yes I have tried removing the quotes from the script and the command line, I have also tried adding 2 x double quotes, I've tried using single quotes. Pretty much all permutations .
Have you tried using the Call operator & ? & cprid_util ...
I managed to get it working by splitting it up and passing both in. Will post the resolution.
Thanks you. Very interesting
0

I managed to get this working by splitting the $fwCommand string up

 $commandSplit = $fwCommand -split ' '
 cprid_util -server $firewallIpAdd -verbose rexec -rcmd  $commandSplit[0] $commandSplit[1]}

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.