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

通过注册表调用PowerShell脚本时报找不到接受$null的位置参数错误

PowerShell脚本通过注册表右键菜单调用时报错问题说明

问题复现流程

测试脚本代码

测试脚本RegistryBoundParams.ps1代码如下:

[CmdletBinding()]
param (
    [Parameter(Mandatory = $true)]
    [string]
    $Target,

    [Parameter(Mandatory = $false)]
    [switch]
    $MySwitch
)

if(!(Test-IsAdmin)){
    Request-AdminRights -NoExit
    Exit
}

if($MySwitch){
    "Do something" | Out-Host
}else {
    "Do something else" | Out-Host
}

Show-AllArguments

正常调用表现

直接在PowerShell终端执行以下命令可正常运行:
C:\Tools\scripts> .\RegistryBoundParams.ps1 -Target "C:\Test\" -MySwitch

注册表调用报错表现

将调用命令写入注册表右键菜单配置后触发报错,错误信息如下:
RegistryBoundParams.ps1: A positional parameter cannot be found that accepts argument '$null'.

注册表中配置的调用命令如下:

pwsh -noexit -file "C:\Tools\scripts\RegistryBoundParams.ps1" -Target "C:\Program Files\Python39\python.exe" -MySwitch

完整注册表项配置文件内容:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\1_TestRegistry]
@="Test Powershell Script from Registry"
"Icon"="C:\\Tools\\icons\\apps\\Powershell 1.ico,0"
"NeverDefault"=""

[HKEY_CLASSES_ROOT\*\shell\1_TestRegistry\command]
@="pwsh -noexit -file \"C:\\Tools\\scripts\\RegistryBoundParams.ps1\" -Target \"C:\\Program Files\\Python39\\python.exe\""

问题排查与解决方案

  • 临时绕过方案:新增$catchall参数接收多余参数,可临时解决报错,但不属于长期最优解。
  • 根因定位:报错来源于使用的由mklement0开发的Request-AdminRights提权脚本存在参数传递bug。
  • 最终修复方案:该bug现已修复,需要跨平台单行自提权、支持绑定/未绑定参数的实现,可搜索Stack Overflow回答ID 69775592获取修复后的版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 05:06:04