2

I'm trying to pass arguments from an installshield setup file, what am I doing wrong ?

$STExecute = "C:\Files\setup.exe"
$STArgument = '/s /f2"c:\windows\setuplogs\inst.log"'

Start-Process Powershell.exe -Credential "Domain\userTempAdmin" `
-ArgumentList "-noprofile -command &{Start-Process $($STExecute) $($STArgument) -verb runas}"

I get the blow error, as you can see it removes the double quotes, that needs to be there, I cant even get it to pass the /s argument in the 2nd start-process:

Start-Process : A positional parameter cannot be found that accepts argument '/f2c:\windows\setuplogs\dmap110_inst.log'
BenDK
  • 131
  • 2
  • 7
  • Start-Process accept only one argument, if you want to pass more arguments to the starting process, you need to use -ArgumentList –  Feb 23 '21 at 16:50
  • Thanks, but the double quotes er still being removed – BenDK Feb 23 '21 at 17:20
  • As an aside: Note that wrapping commands passed to the PowerShell CLI's `-command` / `-c` parameter in `& { ... }` is unnecessary. – mklement0 Feb 23 '21 at 17:42

1 Answers1

1

This is happening because the inner instance is seeing /s and /f2"c:\windows\setuplogs\inst.log" as two separate positional parameters. You need to wrap the arguments for the inner Start-Process with quotes. I'd also suggest using splatting to make it easier to understand what is happening:

$STExecute = "C:\Files\setup.exe"
$STArgument = '/s /f2"c:\windows\setuplogs\inst.log"'
$SPArgs = @{
    FilePath = 'powershell.exe'
    ArgumentList = "-noprofile -command Start-Process '{0}' '{1}' -Verb runas" -f
        $STExecute, $STArgument
}
Start-Process @SPArgs

I have also used the format operator here, as it allows us to inject values without using subexpressions. As long as there are no single quotes in $STArgument, or you escape them properly (four quotes '''' per quote in this case), it should work for you.

TheFreeman193
  • 467
  • 1
  • 7
  • 14
  • Nice - should work here, but: Unfortunately, the use of an _array_ of arguments with `-ArgumentList` isn't fully robust, due to a long-standing bug in `Start-Process`, still present as of this writing (v7.1) - see [GitHub issue #5576](https://github.com/PowerShell/PowerShell/issues/5576). For now, using a _single_ string comprising _all_ arguments, enclosed in _embedded_ `"..."` quoting as necessary, is the only robust approach. As discussed in the linked GitHub issue, an `-ArgumentArray` parameter that supports robust array-based argument passing may be introduced in the future. – mklement0 Feb 23 '21 at 17:40
  • 1
    Thanks @mklement0, you make a good point. I have amended my example. – TheFreeman193 Feb 23 '21 at 18:21