【问题标题】:Start-Process inside start-process as different user启动进程内的启动进程作为不同的用户
【发布时间】:2021-02-23 16:42:25
【问题描述】:

我正在尝试从 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'

【问题讨论】:

  • Start-Process 只接受一个参数,如果要向启动进程传递更多参数,则需要使用-ArgumentList
  • 谢谢,但双引号仍然被删除
  • 顺便说一句:请注意,在& { ... } 中包装传递给PowerShell CLI 的-command / -c 参数的命令是不必要的。

标签: powershell arguments start-process


【解决方案1】:

发生这种情况是因为内部实例将/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 中没有单引号,或者您正确地转义它们(在这种情况下,每个引号四个引号''''),它应该适合您。

【讨论】:

  • 很好 - 应该在这里工作,但是:不幸的是,使用带有 -ArgumentList 的参数的 array 并不完全可靠,因为在Start-Process,在撰写本文时仍然存在 (v7.1) - 请参阅 GitHub issue #5576。目前,使用包含 all 参数的 single 字符串,并根据需要包含在 embedded "..." 引用中,是唯一可靠的方法。正如链接的 GitHub 问题中所讨论的,将来可能会引入支持基于数组的稳健参数传递的 -ArgumentArray 参数。
  • 谢谢@mklement0,你说得很好。我修改了我的例子。
猜你喜欢
  • 1970-01-01
  • 2016-11-14
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 2011-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多