-s 在 powershell 中使用 Start-Job 时

-s when using Start-Job in powershell

我正在尝试在 powershell 中调用 Start-Job。当我这样做时,它会生成一个带有以下参数的后台 powershell:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Version 5.0 -s -NoLogo -NoProfile -EncodedCommand [encoded command I want to run in base64]

但是,无论我发送什么命令,powershell 命令似乎都无法完成。

我尝试生成这样的 powershell 实例:

powershell.exe -s

这似乎也创建了一个似乎冻结的实例,不执行或不做任何事情。在网上看,我似乎找不到对 -s 参数的任何引用。

有人知道它的用途或如何摆脱它以便我的开始工作正常工作吗?

编辑:-s 可能是 -sta 的 shorthand,但我的命令不会使用 -sta 冻结,但会使用 -s。

Edit2:我后来发现 -s 是 -ServerMode 的 shorthand,显然是 Legacy Powershell 2.0 选项。我不知道为什么在使用 Start-Job 时会添加它。

Edit3:我使用的命令是:

$deploymentsJobs += Start-Job -InitializationScript { SomeSmallFunction } (AnotherFunction) -ArgumentList  $arg1, $arg2, $arg3}

tl;博士:

  • -s 选项是命令行的预期部分,用于通过新的 PowerShell 进程启动后台作业 - 它将新进程置于 服务器模式,需要与后台作业管理的调用进程通信。

    • 不是遗留选项,但也没有记录,因为它仅供内部使用PowerShell.
  • 鉴于您描述的一切都符合预期,问题可能出在您通过 -InitializationScript 运行 和主脚本块中的特定命令(隐含的 -ScriptBlock 参数).


如您所见,Start-Job 调用会在幕后 产生一个 powershell -s -NoLogo -NoProfile 调用 (可通过任务管理器发现)。
也就是说,创建了一个 新的 PowerShell 进程 以 运行 后台命令。

带有 Base64 编码命令字符串的 -EncodedCommand 参数仅在您使用 -Initialization 参数调用 Start-Process 时才会出现 - 主脚本块((隐含的)-ScriptBlock 参数)是 不是 通过命令行传递的(见下文)。

-s 用于 PowerShell 内部 - 始终 - 调用后台作业,而 -s,正如您还发现的那样,是 -servermode 开关的别名。 (鉴于只有 -STA 被记录,人们会认为 -s-STA 的缩写,但事实并非如此。
-s / -servermode 是一个 实现细节 ,仅由 PowerShell 本身使用,这就是它未被记录的原因。

This location GitHub 上的 PowerShell Core 源代码向您展示了后台进程的命令行是如何构建的。

服务器模式 是后台进程必须处于的模式,以便通过以下方式与调用进程通信its standard streams (stdin, stdout, stderr):即后台执行的命令通过其stdin流发送到后台进程,后台进程通过其stdout和stderr流报告其输出.[1]

请注意,基于XML的序列化/反序列化发生在此进程间通信期间,使用与 PowerShell 远程处理 相同的基础结构 - 有关详细信息,请参阅


[1] Ohad Schneider points out that it is possible to accidentally disrupt this communication if the main script block contains commands such as Start-Process -NoNewWindow with a console program that directly write to the background process' stdout stream - see this answer.