gpt4 book ai didi

powershell - PowerShell中Start-Job的ScriptBlock/InitializationScript的最大大小

转载 作者:行者123 更新时间:2023-12-02 23:57:40 27 4
gpt4 key购买 nike

当您使用Start-Job开始新工作时,可以将其传递给ScriptBlockInitializationScript,例如:

Function FOO {
Write-Host "HEY"
}
Start-Job -ScriptBlock {FOO} -InitializationScript {
Function Foo { $function:FOO }
} | Wait-Job | Receive-Job

您可以传递的初始化脚本的大小似乎受到限制,如果太大,则会出现如下错误:

[localhost]启动后台进程时发生错误。错误
已报告:文件名或扩展名太长。
+ CategoryInfo:OpenError:(localhost:String)[],PSRemotingTransportException
+ FullyQualifiedErrorId:-2147467259,PSSessionStateBroken

在后台,PowerShell正在创建一个新进程,并将 InitializationScript作为Base64编码的命令行参数传递。

根据Win32 CreateProcess() 函数,命令ine的最大大小为32,768个字符。所以很明显,如果您的Base64编码的 InitializationScript接近此大小,则可能会收到错误消息。

我尚未找到 ScriptBlock参数的大小限制。有人可以确认没有限制吗?

我认为没有限制,因为看起来 ScriptBlock是通过标准输入传输到子进程的?

最佳答案

脚本块实际上有限制。您可以通过3种方式使用scriptblock运行命令:

$scriptblock = '
get-help
get-command
dir
get-help *command*
get-command *help*
'

iex $scriptblock

或使用它:
$scriptblock = {
get-help
get-command
dir
get-help *command*
get-command *help*

}

Start-Process powershell.exe -ArgumentList "Command $scriptblock"

或使用这个:
Start-Process powershell {iex  '
get-help
get-command
dir
get-help *command*
get-command *help*


'
}

可以传递给powershell.exe的脚本限制为 12190字节
但是对于脚本,我从未从Powershell获得超过4000行代码的限制。

关于powershell - PowerShell中Start-Job的ScriptBlock/InitializationScript的最大大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40086313/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com