Automation

 View Only
Expand all | Collapse all

You cannot call a method on a null-valued expression

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

    Posted Apr 22, 2019 01:13 PM

    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



  • 2.  RE: You cannot call a method on a null-valued expression

    Posted Apr 22, 2019 01:35 PM

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



  • 3.  RE: You cannot call a method on a null-valued expression

    Posted Apr 23, 2019 04:25 AM

    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.



  • 4.  RE: You cannot call a method on a null-valued expression

    Posted Apr 23, 2019 04:47 AM

    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]

       }

       }

    }



  • 5.  RE: You cannot call a method on a null-valued expression

    Posted Apr 23, 2019 05:08 AM

    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



  • 6.  RE: You cannot call a method on a null-valued expression

    Posted Apr 23, 2019 07:14 AM

    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]

       }

       }



  • 7.  RE: You cannot call a method on a null-valued expression

    Posted Apr 23, 2019 08:11 AM

    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



  • 8.  RE: You cannot call a method on a null-valued expression

    Posted Apr 23, 2019 08:26 AM

    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?



  • 9.  RE: You cannot call a method on a null-valued expression

    Posted Apr 23, 2019 08:33 AM

    attached the updated script



  • 10.  RE: You cannot call a method on a null-valued expression

    Posted Apr 23, 2019 09:01 AM

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



  • 11.  RE: You cannot call a method on a null-valued expression

    Posted Apr 23, 2019 09:13 AM

    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

    Posted Apr 23, 2019 09:39 AM

    And this version?



  • 13.  RE: You cannot call a method on a null-valued expression

    Posted Apr 23, 2019 10:18 AM

    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



  • 14.  RE: You cannot call a method on a null-valued expression
    Best Answer

    Posted Apr 23, 2019 11:04 AM

    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).



  • 15.  RE: You cannot call a method on a null-valued expression

    Posted Apr 23, 2019 11:46 AM

    You are simply superb. Thank you very much Champ :smileyhappy: