powershellparametersescapingdouble-quotesexternal-script

Powershell Script: Pass parameter with "" like $Arg="$somePath" should result in "c:\temp\" including the ""


this is my first question on stackoverflow, so please be easy on me.

Problem: The call to an external EXE file from a Powershell Script should look like this.

C:\temp\someEXE.exe "batch" "B:\some path with spaces\file name with spaces.jpg" "" "C:\some other path"

My $Arguments Variable in the ps1-script looks like this.

$Arguments = '"batch"', "$Originalpath", '""', "$Outpath\$Outfile"
Start-Process -Wait -FilePath "C:\Temp\SomeExe.exe" -ArgumentList "$Arguments"

Argument 1 "batch" and 3, the empty "" works fine with the '""' (single - double - double - single).

But when i use the '"$Variable"' (single-double double-single) it is not interpreted according to the Quoting Rules (https://learn.microsoft.com/de-de/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.1)

So my question is: How do i get from $Originalpath to "B:\some path with spaces\file name with spaces.jpg" incl. the ""

Thank you in advance

UPDATE: When i add a line to write the Parameters to a logfile, this line gives me what i need.

Write-output '"batch"',`"$Original`",'""',`"$AIAusgabepfad\$Originaldatei$Originaltyp`" | Out-file C:\Temp\Startup.log -append

But when i try to my $Arguments in the same way, the scriptwindow pops up and disapears in the same second.


Solution

  • Generally: To synchronously execute console applications or batch files, call them directly (C:\temp\someEXE.exe ...), do not use Start-Process - see this answer and this GitHub docs issue detailing appropriate vs. non-appropriate use cases and requesting that guidance be added to the Start-Process help topic.


    If you do need to use Start-Process:

    While the -ArgumentList parameter is array-typed ([string[]]), allowing you to pass arguments individually - which you've tried to take advantage of - unfortunately, Start-Process doesn't handle such individually passed arguments properly if they contain embedded spaces (see GitHub issue #5576), so the robust solution is to use a single -ArgumentList argument comprising all arguments, with embedded double-quoting, as necessary (see this answer for a more detailed discussion).

    Start-Process -Wait -FilePath "C:\Temp\SomeExe.exe" -ArgumentList `
      "batch `"$Originalpath`" `"`" `"$Outpath\$Outfile`""
    

    Note that this method of passing arguments - as a single string with embedded quoting and escaping passed to Start-Process -ArgumentList - is not subject to the bugs that plague direct invocation: the resulting string is passed through to the process as-is (on Windows).