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?

1
  • 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! Commented Sep 16, 2011 at 20:40

7 Answers 7

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.

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

1 Comment

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.
1

I found that defining

$quote = '"'

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

Comments

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"

7 Comments

Hi, Shay. It does not; I get the same error. I added the backticks to attempt at escaping the quotes.
@Shay - I don't see how this will work! And what is the difference from just using $group instead of "$group" in this case.
I can't test it but the quotes are needed if $group contain spaces and the underlying utility requires them.
Yeah, but that is when you say "stuff I want" and not when you use $group right?
@Carlos Can you provide a sample command that works in cmd.exe?
|
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.

2 Comments

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
I am very surprised at this considering that MS has been pushing DFSR hard for the last few years.
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.

Comments

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

Comments

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

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.