You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

以管理员权限执行PowerShell脚本遇长度限制的解决办法

解决PowerShell长脚本高权限执行的Base64长度限制问题

方案一:通过临时文件执行(推荐)

直接将脚本内容写入临时文件,再以高权限执行该文件,彻底规避命令行参数长度限制:

# 读取脚本内容
$scriptContent = Get-Content "MyPSscript.ps1" -Raw

# 创建临时ps1文件
$tempFile = [System.IO.Path]::GetTempFileName() -replace '\.tmp$', '.ps1'
Set-Content -Path $tempFile -Value $scriptContent -Encoding Unicode

# 高权限执行临时脚本,如需指定其他用户账户,添加 -Credential 参数
Start-Process -FilePath powershell.exe -Verb RunAs -ArgumentList "-NoExit -ExecutionPolicy Bypass -File `"$tempFile`""

# 可选:执行完成后删除临时文件(如果不需要保留的话)
# Remove-Item -Path $tempFile -Force

方案二:通过标准输入传递脚本内容

启动高权限PowerShell进程时,将脚本内容通过管道传入,避免将长内容作为命令行参数:

$scriptContent = Get-Content "MyPSscript.ps1" -Raw

# 将脚本内容通过管道传入高权限PowerShell进程
$scriptContent | Start-Process -FilePath powershell.exe -Verb RunAs -ArgumentList "-NoExit -ExecutionPolicy Bypass -Command -" -Wait

方案三:跨用户账户高权限执行

如果需要在其他用户账户下同时获取高权限,可结合-Credential参数(需提前获取目标用户凭据):

$scriptContent = Get-Content "MyPSscript.ps1" -Raw
$tempFile = [System.IO.Path]::GetTempFileName() -replace '\.tmp$', '.ps1'
Set-Content -Path $tempFile -Value $scriptContent -Encoding Unicode

# 获取目标用户凭据(会弹出输入框)
$credential = Get-Credential -Message "输入目标用户账户信息"

# 在目标用户账户下高权限执行脚本
Start-Process -FilePath powershell.exe -Credential $credential -Verb RunAs -ArgumentList "-NoExit -ExecutionPolicy Bypass -File `"$tempFile`""

关于Base64长度限制的说明

Windows命令行参数的最大长度限制为32767字符,Base64编码会让原内容体积膨胀约1.33倍,因此当原脚本内容超过约24KB时,编码后的字符串就会触发这个限制。上述方案均绕过了直接传递长参数的问题,是更可靠的替代方案。

内容的提问来源于stack exchange,提问作者Headbanging_coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 00:59:50