启动进程内的启动进程作为不同的用户

问题描述

我试图从 installshield 安装文件中传递参数,我做错了什么?

$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}"

我得到了打击错误,正如你所看到的,它删除了双引号,这需要在那里,我什至无法让它在第二个启动过程中传递 /s 参数:

start-process : A positional parameter cannot be found that accepts argument '/f2c:\windows\setuplogs\dmap110_inst.log'

解决方法

发生这种情况是因为内部实例将 /s/f2"c:\windows\setuplogs\inst.log" 视为两个单独的位置参数。您需要用引号将内部 Start-Process 的参数包装起来。我还建议使用 splatting 以便更容易地了解正在发生的事情:

$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

我还在这里使用了 format operator,因为它允许我们在不使用子表达式的情况下注入值。只要 $STArgument 中没有单引号,或者您正确地将它们转义(在这种情况下每个引号有四个引号 ''''),它应该适合您。