power shell not null 检查 if 语句

power shell not null check in if statement

Powershell 中的新功能script.Am 尝试检查数组索引为 null 的 if 语句,然后控制移动到其他部分我尝试了一些方法但没有用

代码

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

   }else {
      # do something
   }

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

Cannot index into a null array.

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

if ($node -ne $null) {
    if ($node[$i] -ne $null) {
        # All good; do something interesting.
    }
}