14 Replies Latest reply on Apr 23, 2019 4:46 AM by ganapa2000

    You cannot call a method on a null-valued expression

    ganapa2000 Hot Shot

      Hi,

       

      please help as I am getting the below error,

       

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

      At D:\myreports\Free_LUNs_Info.ps1:54 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.ps1:59 char:11

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

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

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

          + FullyQualifiedErrorId : NullArrayIndex

       

       

       

      Hereby attached the script

       

       

      PowerCLI Version

      ----------------

         VMware PowerCLI 11.2.0 build 12483598

      ---------------

      Component Versions

      ---------------

         VMware Cis Core PowerCLI Component PowerCLI Component 11.2 build 12483642

         VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 11.2 build 12483638

        • 1. Re: You cannot call a method on a null-valued expression
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          Could it be that there are no VMFS datastores on the ESXi nodes in the cluster?

          • 2. Re: You cannot call a method on a null-valued expression
            ganapa2000 Hot Shot

            There are many VMFS datastores, this script was working when we are using 5.5. After we upgraded from 5.5 to 6.5, this script is not working.

            • 3. Re: You cannot call a method on a null-valued expression
              LucD Guru
              User ModeratorsvExpertCommunity Warriors

              Ok, let's try to pinpoint where the problem is exactly.
              Does the following also produce that error?

               

              $clus = "IS"

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

              $esxServers | %{

                  $esxImpl = $_


              # Get SCSI LUNs

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

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

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

                 }

                 }

                 Else{

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

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

                 }

                 }

              }

              • 4. Re: You cannot call a method on a null-valued expression
                ganapa2000 Hot Shot

                LucD, here is the output

                 

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

                At D:\mytest.ps1:14 char:4

                +    $esxImpl.ExtensionData.config.Network.DnsConfig.Hostname + "-" + $_.DiskName. ...

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

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

                    + FullyQualifiedErrorId : InvokeMethodOnNull

                 

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

                At D:\mytest.ps1:14 char:4

                +    $esxImpl.ExtensionData.config.Network.DnsConfig.Hostname + "-" + $_.DiskName. ...

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

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

                    + FullyQualifiedErrorId : InvokeMethodOnNull

                • 5. Re: You cannot call a method on a null-valued expression
                  LucD Guru
                  vExpertCommunity WarriorsUser Moderators

                  Ok, can you do the following as well?

                   

                  $clus = "IS"

                   

                  Get-Cluster $clus | Get-VMHost -PipelineVariable esx |

                  ForEach-Object -Process {

                     $esx.ExtensionData.Config.StorageDevice.ScsiLun |

                     Where-Object { $_.LunType -eq 'disk' } | % {

                     $_ | Select @{N = 'Version'; E = { $esx.Version}}, DiskName, CanonicalName

                     }

                  }

                   

                  It looks as if the issue with the CanonicalName property from the Get-ScsiLun cmdlet is fixed in vSphere 6.5.
                  Can you try replacing that part in the script with

                   

                     # Get SCSI LUNs

                     $version = [Version]$esxImpl.Version

                     if ($version.Major -ne 6 -and $version.Minor -ne 0)

                     {

                     $esxImpl | Get-ScsiLun -LunType disk | % {

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

                     }

                     }

                     Else

                     {

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

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

                     }

                     }

                   

                  • 6. Re: You cannot call a method on a null-valued expression
                    ganapa2000 Hot Shot

                    LucD,

                     

                    Below is the output after I replacing

                     

                    # Get SCSI LUNs

                       $version = [Version]$esxImpl.Version

                       if ($version.Major -ne 6 -and $version.Minor -ne 0)

                       {

                       $esxImpl | Get-ScsiLun -LunType disk | % {

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

                       }

                       }

                       Else

                       {

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

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

                       }

                       }

                     

                    PS D:\> .\mytest1.ps1

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

                    At D:\mytest1.ps1:17 char:4

                    +    $esxImpl.ExtensionData.config.Network.DnsConfig.Hostname + "-" + $_.DiskName. ...

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

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

                        + FullyQualifiedErrorId : InvokeMethodOnNull

                     

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

                    At D:\mytest1.ps1:17 char:4

                    +    $esxImpl.ExtensionData.config.Network.DnsConfig.Hostname + "-" + $_.DiskName. ...

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

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

                        + FullyQualifiedErrorId : InvokeMethodOnNull

                     

                     

                    and if I execute the below code

                     

                    $clus = "IS"

                     

                    Get-Cluster $clus | Get-VMHost -PipelineVariable esx |

                    ForEach-Object -Process {

                       $esx.ExtensionData.Config.StorageDevice.ScsiLun |

                       Where-Object { $_.LunType -eq 'disk' } | % {

                       $_ | Select @{N = 'Version'; E = { $esx.Version}}, DiskName, CanonicalName

                       }

                    }

                     

                    I am getting the output but DiskName shows blank

                     

                    Version                                                            DiskName                                                           CanonicalName
                    -------                                                            --------                                                           -------------
                    6.5.0                                                                                                                                 naa.624a9370c377
                    6.5.0                                                                                                                                 naa.6006016001d0
                    6.5.0                                                                                                                                 naa.6006016001d0
                    6.5.0                                                                                                                                 naa.6006016001d0

                    • 7. Re: You cannot call a method on a null-valued expression
                      LucD Guru
                      vExpertUser ModeratorsCommunity Warriors

                      That is strange, it shouldn't go through the else-block when the ESXi version is not 6.0.
                      Can you attach the updated version of the script?

                      • 8. Re: You cannot call a method on a null-valued expression
                        ganapa2000 Hot Shot

                        attached the updated script

                        • 9. Re: You cannot call a method on a null-valued expression
                          LucD Guru
                          vExpertUser ModeratorsCommunity Warriors

                          The intention was to include that part of the code in the original script.
                          Like the attached code.

                          • 10. Re: You cannot call a method on a null-valued expression
                            ganapa2000 Hot Shot

                            LucD,

                             

                            below is the output from attached code

                             

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

                            At D:\New folder\mytest4.ps1:51 char:13

                            +             $esxImpl.ExtensionData.config.Network.DnsConfig.Hostname + "-" + $_. ...

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

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

                                + FullyQualifiedErrorId : InvokeMethodOnNull

                             

                            Cannot index into a null array.

                            At D:\New folder\mytest4.ps1:65 char:13

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

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

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

                                + FullyQualifiedErrorId : NullArray

                            • 12. Re: You cannot call a method on a null-valued expression
                              ganapa2000 Hot Shot

                              LucD,

                               

                              Below is the output of attahed script

                               

                              isesx02-624a937000002547995

                              isesx02-624a937000002547995

                              isesx02-6006016000002547995

                              isesx02-6006016000002547995

                               

                              Cannot index into a null array.

                              At D:\New folder\test.ps1:66 char:13

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

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

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

                                  + FullyQualifiedErrorId : NullArray

                               

                              Cannot index into a null array.

                              At D:\New folder\test.ps1:67 char:13

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

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

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

                                  + FullyQualifiedErrorId : NullArray

                              • 13. Re: You cannot call a method on a null-valued expression
                                LucD Guru
                                User ModeratorsCommunity WarriorsvExpert

                                Ok, that is good news.
                                That means the script is now passing the failing line.

                                 

                                Now back to the original script (included the changes we just tested).

                                • 14. Re: You cannot call a method on a null-valued expression
                                  ganapa2000 Hot Shot

                                  You are simply superb. Thank you very much Champ