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

PowerShell数组比较空参数错误排查:仅非空比较仍报错

问题分析与修复:PowerShell数组比较空值报错

问题场景

你编写了两个用于提取匹配正则表达式数字的函数hour00_numbers和hour01_numbers,随后通过matching_numbers01函数尝试仅在两个数组均非空时执行比较,但运行时频繁报错:

Cannot bind argument to parameter 'ReferenceObject' because it is null

原函数代码

数字提取函数

function hour00_numbers {
    foreach ($event in $filter00) {
        $hour00Numbers = @()
        $hour00Numbers += [regex]::Matches($event.Source.Summary -replace ' ', '9 *[45679]( *[0-9]){6}').Value
        $hour00Numbers
    }
}
    
function hour01_numbers {
    foreach ($event in $filter01) {
        $hour01Numbers = @()
        $hour01Numbers += [regex]::Matches($event.Source.Summary -replace ' ', '9 *[45679]( *[0-9]){6}').Value
        $hour01Numbers
    }
}

匹配检查函数

function matching_numbers01 {
    if ($null -ne $(hour00_numbers) -and $null -ne $(hour01_numbers)) {
        $script:matchingNumbers01 = Compare-Object $(hour00_numbers) $(hour01_numbers) -IncludeEqual | Where-Object { $_.SideIndicator -eq '==' } | Select-Object -ExpandProperty InputObject
    }
    else {
        $matchingNumbers01 = $null
    }

    if ($null -ne $(hour00_numbers)) {
        Write-Host "hour00 numbers: $(hour00_numbers)" -ForegroundColor Green
    }
    else {
        Write-Host "hour00 is empty" -ForegroundColor Yellow
    }

    if ($null -ne $(hour01_numbers)) {
        Write-Host "hour01 numbers: $(hour01_numbers)" -ForegroundColor Green
    }
    else {
        Write-Host "hour01 is empty" -ForegroundColor Yellow
    }

    if ($null -ne $matchingNumbers01) {
        Write-Host "matching numbers are: $matchingNumbers01" -ForegroundColor Yellow
    }
    else {
        Write-Host "no matching numbers found" -ForegroundColor Green
    }
}

错误原因

  1. 数字提取函数逻辑错误:原函数在foreach循环内每次都初始化$hour00Numbers = @(),导致每次循环清空数组,最终仅返回最后一个$event的匹配结果;且无匹配时返回空数组@()而非$null。
  2. 空值判断无效:$null -ne $(hour00_numbers)无法识别空数组——PowerShell中空数组@()与$null不相等,该判断永远为$true,导致空数组仍会进入Compare-Object逻辑,而Compare-Object不接受空输入参数。
  3. 重复调用函数:每次$(hour00_numbers)都会重新执行函数,效率低下且可能因$filter00/$filter01变化导致结果不一致。
  4. 变量作用域不一致:if块使用脚本作用域的$script:matchingNumbers01,但else块使用局部作用域的$matchingNumbers01,导致变量值无法统一。

修复方案

1. 修复数字提取函数

将数组初始化移到循环外,收集所有$event的匹配结果;无匹配时返回$null便于后续判断:

function hour00_numbers {
    $hour00Numbers = @()
    foreach ($event in $filter00) {
        $currentMatches = [regex]::Matches($event.Source.Summary -replace ' ', '9 *[45679]( *[0-9]){6}').Value
        if ($currentMatches) {
            $hour00Numbers += $currentMatches
        }
    }
    if ($hour00Numbers.Count -eq 0) { $null } else { $hour00Numbers }
}

function hour01_numbers {
    $hour01Numbers = @()
    foreach ($event in $filter01) {
        $currentMatches = [regex]::Matches($event.Source.Summary -replace ' ', '9 *[45679]( *[0-9]){6}').Value
        if ($currentMatches) {
            $hour01Numbers += $currentMatches
        }
    }
    if ($hour01Numbers.Count -eq 0) { $null } else { $hour01Numbers }
}

2. 修复匹配检查函数

提前保存函数返回值避免重复调用;用布尔隐式转换判断非空;统一变量作用域:

function matching_numbers01 {
    $hour00Data = hour00_numbers
    $hour01Data = hour01_numbers

    if ($hour00Data -and $hour01Data) {
        $script:matchingNumbers01 = Compare-Object $hour00Data $hour01Data -IncludeEqual | 
                                    Where-Object { $_.SideIndicator -eq '==' } | 
                                    Select-Object -ExpandProperty InputObject
    }
    else {
        $script:matchingNumbers01 = $null
    }

    if ($hour00Data) {
        Write-Host "hour00 numbers: $hour00Data" -ForegroundColor Green
    }
    else {
        Write-Host "hour00 is empty" -ForegroundColor Yellow
    }

    if ($hour01Data) {
        Write-Host "hour01 numbers: $hour01Data" -ForegroundColor Green
    }
    else {
        Write-Host "hour01 is empty" -ForegroundColor Yellow
    }

    if ($script:matchingNumbers01) {
        Write-Host "matching numbers are: $script:matchingNumbers01" -ForegroundColor Yellow
    }
    else {
        Write-Host "no matching numbers found" -ForegroundColor Green
    }
}

关键修改说明

  • 修正数组收集逻辑:将数组初始化移到循环外,确保所有$event的匹配结果被完整收集。
  • 高效非空判断:$hour00Data -and $hour01Data利用PowerShell隐式转换——$null、空数组、空字符串都会被视为$false,一次判断即可确认两个结果均非空。
  • 避免重复执行:提前保存函数返回值,提升效率并保证结果一致性。
  • 统一作用域:else块也使用$script:matchingNumbers01,确保变量在脚本作用域内统一。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 04:30:18