2

I'm having a surprisingly difficult time embedding variables with quotes to an external command with PoSH. For example, this command

 dfsradmin membership list /rgname:`"stuff I want`"

gives me the following expected result:

 Failed:
 Replication group with name stuff I want cannot be found.

This command, however

 $group = "stuff I want"
 dfsradmin membership list /rgname:`"$group`"

fails with this error:

 Failed:
 The subobject "/rgname:"stuff is not a valid subobject.

Is this a bug with Powershell or am I missing/misunderstanding something?

Carlos Nunez
  • 2,047
  • 1
  • 18
  • 20
  • It seems that with this particular command, there's no way to do it except by using cmd to write to a file and reading from it, i.e. the old-school way. Thanks! – Carlos Nunez Sep 16 '11 at 20:40

7 Answers7

2

Yeah there are known issues in Powershell ( including v2.0) around this: http://connect.microsoft.com/PowerShell/feedback/details/376207/executing-commands-which-require-quotes-and-variables-is-practically-impossible

See if the alternatives discussed in the link above work for you. I cannot try it out as I don't have that executable.

Also echoargs.exe is a useful tool that you can use to see what arguments have been recevied from Powershell.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Actually PowerShell performs automatic quoting in a way that works for 99+% of command-line tools. it appears that the `dfsradmin.exe` executable uses its own parser that doesn't work correctly with PowerShell's automatic quoting. – Bill_Stewart Apr 23 '18 at 15:29
1

I found that defining

$quote = '"'

and then using /command$quote"test"$quote works as well

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
Erik Thue
  • 11
  • 1
0

I found a workaround which doesn't call cmd but uses Invoke-Expression instead. The command has to be put in a variable first:

$var = "string with spaces"

$command = "first part " + [char]96 + [char]34 + $var + [char]96 + [char]34 + " second part"

Invoke-Expression $command

Not that pretty but it works. You can replace [char]96 with '`' and [char]34 with '"' if you prefer. Easy to create a function which does it if you use it a lot.

0

All of the above did not work for me but based on Carlos idea, this is the solution that worked posted here

# get msdeploy exe
$MSDeploy = ${env:ProgramFiles}, ${env:ProgramFiles(x86)} |
        ForEach-Object {Get-ChildItem -Path $_ -Filter 'MSDeploy.exe' -Recurse} |
        Sort-Object -Property @{Expression={[version]$_.VersionInfo.FileVersion}} -Descending |
        Select-Object -First 1 -ExpandProperty FullName

#build deploy command        
$deplyCmd = """""$MSDeploy"" -verb:sync -dest:iisApp=""Default Web Site"" -enableRule:DoNotDeleteRule -source:iisApp=""$ExtraWebFilesFolder"""    

#execute    
&cmd /c $deplyCmd
Alobidat
  • 462
  • 7
  • 16
0

I know this is old thread but just posting here in case my solution works for somebody as it worked for me. This particular command (dfsradmin) expects natively seen quotes so I just enclosed value with quotes in single quotes thus passing quotes as well:

   dfsradmin membership list /rgname:'"stuff I want"'

or if using through variable:

    $group = '"stuff I want"'
    dfsradmin membership list /rgname:$group
spricer
  • 7
  • 3
0

There's no need to add back ticks in front of quotes. Does this work for you?

 $group = "stuff I want"
 dfsradmin membership list /rgname:"$group"
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
0

So I was able to get around this by executing it in CMD.exe and doing string manipulations to get what I need.

$str = &cmd /c 'dfsradmin membership list /rgname:"blah blah"'
$str = &cmd /c "dfsradmin membership list /rgname:$blah"       # with vars

Thanks for the help! I hope this has been resolved in Powershell 3.0.

Carlos Nunez
  • 2,047
  • 1
  • 18
  • 20
  • No, it hasn't been fixed in PowerShell 3.0 and to rub salt into the wounds, the native DFS-R PowerShell commands didn't make it into Windows 2012 but are coming in R2 – Rob Nicholson Aug 09 '13 at 16:42
  • I am very surprised at this considering that MS has been pushing DFSR hard for the last few years. – Carlos Nunez Aug 09 '13 at 16:44