gpt4 book ai didi

powershell - Start-Process cmdlet 的 -ArgumentList 参数的最大长度是多少

转载 作者:行者123 更新时间:2023-12-03 00:25:50 41 4
gpt4 key购买 nike

我想使用 Start-Process 从 PS 调用一个程序并将一堆参数传递给该程序应该在后台处理的调用。

有时可能会发生,提供给该程序的参数的总列表可能有数百个(总共大约 200-300 个),每个又是一个最长 32 字节长度的 string。我试图找出 -ArgumentList 的最大长度,但到目前为止找不到任何引用。

我怀疑我会在提供的参数数量上遇到任何问题,但它确实让我感到困惑,-ArgumentList 参数实际上可能有多少个参数或总共多长时间?

最佳答案

可能是 8191 个字符的组合长度。或者可能取决于您正在运行的程序。

来源:反复试验(Windows 8.1/PSv4):

Start-Process -FilePath cmd -ArgumentList (@('/k','echo 1') + (2..1852))
# works

Start-Process -FilePath cmd -ArgumentList (@('/k','echo 1') + (2..1853))
# doesn't work

在 6769 左右触发异常:

PS C:\> Start-Process -FilePath cmd -ArgumentList (@('/k','echo 1') + (2..6768))
Start-Process : This command cannot be run due to the error: The filename or extension is too long.
At line:1 char:1
+ Start-Process -FilePath cmd -ArgumentList (@('/k','echo 1') + (2..676 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

但是,如果我将数字移动一点 (2..1852|%{$_*100}) 那么它会更快地失败。建议重要的不是参数的数量,而是组合结果的字符串长度。

((@('/k','echo 1') + (2..1852)) -join " ").Length
# 8160 when it works, 8165 when it breaks

Google for 8165 limit cmd 并得到:

Maximum Length of Command Line String

https://support.microsoft.com/en-gb/kb/830473

On computers running Microsoft Windows XP or later, the maximum length of the string that you can use at the command prompt is 8191 characters.

所以,要么是 8191 个字符,要么......也许这取决于你正在调用的程序。

300 * 32 会破坏它。

但是话又说回来,如果您已经有一个可以处理它的程序 - start-process 似乎没有问题,将 1,800 个项目的数组作为参数列表。

关于powershell - Start-Process cmdlet 的 -ArgumentList 参数的最大长度是多少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40521809/

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