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

如何修复PowerShell中“找不到匹配参数名RegSetting”的错误

修复PowerShell注册表查询报错及逻辑问题

错误原因

  1. 参数不存在:Get-ItemProperty cmdlet 没有 -RegSetting 这个参数,这是报错的直接原因。
  2. 逻辑顺序错误:你在 ForEach 循环外就调用了未定义的 $WUSetting 变量;而且循环里只是重复输出提前定义的 $RegValue,根本没遍历数组中的注册表项去执行查询。

修复后的代码

$WUSettingsArray = @(
    "AutoRestartDeadlinePeriodInDays",
    "AutoRestartNotificationSchedule",
    "AutoRestartRequiredNotificationDismissal",
    "BranchReadinessLevel",
    "DeferFeatureUpdates",
    "DeferFeatureUpdatesPeriodInDays",
    "DeferQualityUpdates",
    "DeferQualityUpdatesPeriodInDays",
    "DisableDualScan",
    "DoNotConnectToWindowsUpdateInternetLocations",
    "ElevateNonAdmins",
    "EngagedRestartDeadline",
    "EngagedRestartSnoozeSchedule",
    "EngagedRestartTransitionSchedule",
    "PauseFeatureUpdatesStartTime",
    "PauseQualityUpdatesStartTime",
    "ScheduleImminentRestartWarning",
    "ScheduleRestartWarning",
    "SetAutoRestartDeadline",
    "SetAutoRestartNotificationConfig",
    "SetAutoRestartNotificationDisable",
    "SetAutoRestartRequiredNotificationDismissal",
    "SetEDURestart",
    "SetEngagedRestartTransitionSchedule",
    "SetRestartWarningSchd",
    "WUServer",
    "WUStatusServer"
)

$WURegKey = "HKLM:Software\Policies\Microsoft\Windows\WindowsUpdate"

# 遍历数组中的每个注册表项
ForEach ($WUSetting in $WUSettingsArray) {
    # 尝试获取指定项的值,忽略不存在的项避免报错
    $RegValue = Get-ItemPropertyValue -Path $WURegKey -Name $WUSetting -ErrorAction SilentlyContinue
    
    # 根据值是否存在输出对应信息
    if ($RegValue) {
        Write-Output "路径: $WURegKey | 项: $WUSetting | 值: $RegValue"
    } else {
        Write-Output "路径: $WURegKey | 项: $WUSetting | 未找到该项"
    }
}

代码说明

  • 用 @() 直接初始化数组,比多次 += 更简洁高效。
  • 使用 Get-ItemPropertyValue 直接获取单个注册表项的值,比 Get-ItemProperty 更精准,无需处理多余属性。
  • 添加 -ErrorAction SilentlyContinue 忽略项不存在的报错,避免脚本中断。
  • 将查询操作放到 ForEach 循环内部,确保逐个处理数组中的每个注册表项。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 08:05:27