VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Index operation failed; the array index evaluated to null.

Hi,

I am getting below error on vCenter 6.5 and ESXi 6.5, however, this script run fine on vCenter 5.5 and ESXi 5.5 without any errors.

Please help...

Output

Index operation failed; the array index evaluated to null.

At D:\myreports\Free_LUNs_Info\Prod1_Compute_Free_LUNs_Info.ps1:71 char:11

+           $scsiTab[$key] = $_.CanonicalName,"",$capacityGB,$lunId,""

+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : NullArrayIndex

You cannot call a method on a null-valued expression.

At D:\myreports\Free_LUNs_Info\Prod1_Compute_Free_LUNs_Info.ps1:66 char:11

+           $key = $esxImpl.ExtensionData.config.Network.DnsConfig.Hostname + "-"  ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : InvokeMethodOnNull

Index operation failed; the array index evaluated to null.

At D:\myreports\Free_LUNs_Info\Prod1_Compute_Free_LUNs_Info.ps1:71 char:11

+           $scsiTab[$key] = $_.CanonicalName,"",$capacityGB,$lunId,""

+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : NullArrayIndex

Cannot index into a null array.

At D:\myreports\Free_LUNs_Info\Prod1_Compute_Free_LUNs_Info.ps1:84 char:4

+             $scsiTab[$key][1] = $dsName

+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.

At D:\myreports\Free_LUNs_Info\Prod1_Compute_Free_LUNs_Info.ps1:85 char:13

+             $scsiTab[$key][4] = $dscName

+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : NullArray

Script

#Script

$clus = "Compute"

$date = Get-Date -format dd-MMM-yyyy

$csvName = "d:\reports\Free_LUNs_Info_$date.xlsx"

$rndNum = Get-Random -Maximum 99999

$LunInfoDef = @"

    public string ClusterName;

    public string CanonicalName;

    public string UsedBy;

    public string DSC;

    public string SizeGB;

"@

$LunInfoDef = "public struct LunInfo" + $rndNum + "{`n" + $LunInfoDef

$esxServers = Get-Cluster $clus | Get-VMHost | Sort-Object -Property Name

$esxServers | %{

    $LunInfoDef += ("`n`tpublic string " + ($_.ExtensionData.config.Network.DnsConfig.Hostname) + ";")

}

$LunInfoDef += "`n}"

Add-Type -Language CsharpVersion3 -TypeDefinition $LunInfoDef

$scsiTab = @{}

$esxServers | %{

    $esxImpl = $_

# Get SCSI LUNs

    if(([Version]$esxImpl.Version).Major -lt 6){

      $esxImpl | Get-ScsiLun | where {$_.LunType -eq "Disk"} | %{

     

          $key = $esxImpl.ExtensionData.config.Network.DnsConfig.Hostname + "-" + $_.CanonicalName.Split(".")[1]

          if(!$scsiTab.ContainsKey($key)){

              $lunId = $_.RuntimeName.Split('L')[1]

              $scsiTab[$key] = $_.CanonicalName,"",$_.CapacityGB,$lunId,""

          }

      }

    }

    Else{

      $esxImpl.ExtensionData.Config.StorageDevice.ScsiLun | Where-Object{$_.LunType -eq 'disk'} |%{

          $key = $esxImpl.ExtensionData.config.Network.DnsConfig.Hostname + "-" + $_.DiskName.Split(".")[1]

          $capacityGB = $_.Capacity.Block * $_.Capacity.BlockSize / 1GB

          $lunUuid = $_.Uuid

          $lun = $esxImpl.ExtensionData.Config.StorageDevice.MultipathInfo.Lun | Where-Object{$_.Id -eq $lunUuid}

          $lunId = $lun.Path[0].Name.Split('L')[1]

          $scsiTab[$key] = $_.CanonicalName,"",$capacityGB,$lunId,""

      }

    }

# Get the VMFS datastores

    $esxImpl | Get-Datastore | Where-Object {$_.Type -eq "VMFS"} | Get-View | %{

        $dsName = $_.Name

        $dscName = ''

        if($_.Parent.Type -eq 'StoragePod'){

            $dscName = Get-View -Id $_.Parent -Property Name -Server $esxImpl.Uid.Split('@')[1].Split(':')[0] | Select -ExpandProperty Name

        }

        $_.Info.Vmfs.Extent | %{

            $key = $esxImpl.ExtensionData.config.Network.DnsConfig.Hostname + "-" + $_.DiskName.Split(".")[1]

            $scsiTab[$key][1] = $dsName

            $scsiTab[$key][4] = $dscName

        }

    }

}

# Get the RDM disks

Get-Cluster $clus | Get-VM | Get-View | %{

    $vm = $_

    $vm.Config.Hardware.Device | Where-Object {$_.gettype().Name -eq "VirtualDisk"} | %{

        if($_.Backing.PSObject.Properties['CompatibilityMode'] -and "physicalMode","virtualMode" -contains $_.Backing.CompatibilityMode){

            $disk = $_.Backing.LunUuid.Substring(10,32)

            $key = (Get-View $vm.Runtime.Host).config.Network.DnsConfig.Hostname + "-" + $disk

            $scsiTab[$key][1] = $vm.Name + "/" + $_.DeviceInfo.Label

        }

    }

}

$scsiTab.GetEnumerator() | Group-Object -Property {$_.Key.Split("-")[1]} | %{

    $lun = New-Object ("LunInfo" + $rndNum)

    $lun.ClusterName = $clus

    $_.Group | %{

        $esxName = $_.Key.Split("-")[0]

        $lun.$esxName = $_.Value[3]

        if(!$lun.CanonicalName){$lun.CanonicalName = $_.Value[0]}

        if(!$lun.UsedBy){$lun.UsedBy = $_.Value[1]}

        if(!$lun.SizeGB){$lun.SizeGB = [math]::Round($_.Value[2],0)}

        $lun.DSC = $_.Value[4]

    }

    $lun

} | Export-Excel -Path $csvName -AutoFilter -AutoSize -WorksheetName VM_Free_Disk_Info

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Can you try changing this line

$key = $esxImpl.ExtensionData.config.Network.DnsConfig.Hostname + "-" + $_.DiskName.Split(".")[1]

to this

$key = $esxImpl.ExtensionData.config.Network.DnsConfig.Hostname + "-" + $_.CanonicalName.Split(".")[1]


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Can you try changing this line

$key = $esxImpl.ExtensionData.config.Network.DnsConfig.Hostname + "-" + $_.DiskName.Split(".")[1]

to this

$key = $esxImpl.ExtensionData.config.Network.DnsConfig.Hostname + "-" + $_.CanonicalName.Split(".")[1]


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Super Champ, Thank you very very much Smiley Happy

0 Kudos