powershell not null检入if语句

问题描述 投票:-1回答:1

在powershell脚本中是新的。我试图检查一个if语句有数组索引是null然后控制移动到else部分我尝试了一些方法但不工作

if(-not $node[$i] -ne $null ){

   }else {
      # do something
   }

当$ node [$ i]为null时,控件如何移动到else部分。以上情况,收到错误

enter image description here

powershell powershell-v2.0
1个回答
2
投票

无法索引到空数组。

......告诉你$node为空,而不是$node[$i]为空。试试这个:

if ($node -ne $null) {
    if ($node[$i] -ne $null) {
        # All good; do something interesting.
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.