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

PowerShell报错“无法索引空数组”求助:数组已预填充仍出错

问题排查:PowerShell数组索引错误"cannot index into a null array"

问题描述

脚本第40行的比较操作始终返回“cannot index into a null array”错误。已将数组预填充为0确保非空,且数组长度大于导入的表格,调试输出显示数组已存储值,但错误从循环首次运行时就出现。相关代码如下:

Import-Module -Name ImportExcel

$CurrentFOBDoc = Import-Excel "path\to\doc"

$CurrentElikaFOB = Import-CSV "path\to\doc"

$AuditResluts = [Object[]]::new(300)

$count = 0

foreach($value in $AuditResluts){
    $AuditResluts[$count] = 0
    $count++
}

$count = 0

foreach ($Fob in $CurrentFOBDoc){

    foreach ($EFob in $CurrentElikaFOB){
        IF($Fob.'Card Number' -like $EFob.'Wiegand 1 Code'){

            IF($Fob.'Facility Number' -like $EFob.'Facility 1 Code'){

                If(($Fob.'First Name' -like $EFob.'First Name' ) -and ($Fob.'Last Name' -like $EFob.'Last Name')){

                    $AuditResluts[$count] = "Fob $($Fob.'Facility Number') $($Fob.'Card Number') is assigned to $($Fob.'First Name')  $($Fob.'Last Name') in Elika and Log Sheet"
                                               
                }
                Else{
                    $AuditResults[$count] = "Fob $($Fob.'Facility Number') $($Fob.'Card Number') is not assigned to $($Fob.'First Name') $($Fob.'Last Name') in Elika, please reconcile"
                }
            }
        }
        
    }

    echo $AuditResluts[$count]

   #This Comparison gives the error, -eq gives the same result
    If($AuditResults[$count] -like 0){
        $AuditResults[$count] = "Fob $($Fob.'Facility Number') $($Fob.'Card Number') assigned to $($Fob.'First Name') $($Fob.'Last Name') Not found in Elika"
    }

    $count ++
}

错误原因

核心问题是数组变量名拼写不一致:

  • 初始化和预填充数组时使用的变量名是$AuditResluts(拼写错误,少了字母u)
  • 但在内层循环的Else分支、第40行的判断及后续赋值中,使用的是$AuditResults(正确拼写)

由于$AuditResults从未被初始化,它本质是一个未定义的null变量,当尝试索引$AuditResults[$count]时,自然会抛出“cannot index into a null array”错误。

修复方案

将所有数组变量名统一为正确的拼写$AuditResults(或统一为$AuditResluts,建议使用正确拼写避免后续混淆),修正后的代码如下:

Import-Module -Name ImportExcel

$CurrentFOBDoc = Import-Excel "path\to\doc"

$CurrentElikaFOB = Import-CSV "path\to\doc"

# 统一变量名拼写
$AuditResults = [Object[]]::new(300)

$count = 0

foreach($value in $AuditResults){
    $AuditResults[$count] = 0
    $count++
}

$count = 0

foreach ($Fob in $CurrentFOBDoc){

    foreach ($EFob in $CurrentElikaFOB){
        IF($Fob.'Card Number' -like $EFob.'Wiegand 1 Code'){

            IF($Fob.'Facility Number' -like $EFob.'Facility 1 Code'){

                If(($Fob.'First Name' -like $EFob.'First Name' ) -and ($Fob.'Last Name' -like $EFob.'Last Name')){

                    $AuditResults[$count] = "Fob $($Fob.'Facility Number') $($Fob.'Card Number') is assigned to $($Fob.'First Name')  $($Fob.'Last Name') in Elika and Log Sheet"
                                               
                }
                Else{
                    $AuditResults[$count] = "Fob $($Fob.'Facility Number') $($Fob.'Card Number') is not assigned to $($Fob.'First Name') $($Fob.'Last Name') in Elika, please reconcile"
                }
            }
        }
        
    }

    echo $AuditResults[$count]

   # 修正变量名后即可正常比较
    If($AuditResults[$count] -like 0){
        $AuditResults[$count] = "Fob $($Fob.'Facility Number') $($Fob.'Card Number') assigned to $($Fob.'First Name') $($Fob.'Last Name') Not found in Elika"
    }

    $count ++
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 07:45:04