BenDK
BenDK

Reputation: 141

Start-Process inside start-process as different user

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'

Upvotes: 2

Views: 770

Answers (1)

TheFreeman193
TheFreeman193

Reputation: 527

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.

Upvotes: 1

Related Questions