Borek Bernard
Borek Bernard

Reputation: 53352

PowerShell: String expansion in command mode

What are the rules of string expansion in command mode? If on cmd.exe, I would write this:

c:\asdoc.exe -doc-sources+=src

I need to convert this to a string where the actual source path ("src") is computed so somewhere above this line, $sourcePath = "src" is executed. Now I need to transform that cmd.exe command to PowerShell command. I have tried the following but it doesn't work:

& c:\asdoc.exe -doc-sources+=$sourcePath    # does NOT work
& c:\asdoc.exe -doc-sources+="$sourcePath"  # does NOT work
& c:\asdoc.exe -doc-sources+=($sourcePath)  # does NOT work

I used the EchoArgs utility and it gives me these results:

Arg 0 is <-doc-sources+=$sourcePath> # case 1

Arg 0 is <-doc-sources+=$sourcePath> # case 2

Arg 0 is <-doc-sources+=>            # case 3
Arg 1 is <src path>

How do I make the string expand "correctly" in this example?

Upvotes: 2

Views: 368

Answers (1)

Frode F.
Frode F.

Reputation: 54981

If i understand you properly you want everything expanded and as a single argument. Try to "collect" the argument with quotes.

PS > $sourcepath = "src"

PS > & EchoArgs.exe "-doc-sources+=$($sourcePath)"
Arg 0 is <-doc-sources+=src>

So try this:

& c:\asdoc.exe "-doc-sources+=$($sourcePath)"

The example below will also works AS LONG as you want to expand a variable an not a property inside a variable (ex $myobject.sourcepath)

& c:\asdoc.exe "-doc-sources+=$sourcePath"

Upvotes: 3

Related Questions