Automation

 View Only
  • 1.  Error: You cannot call a method on a null-valued expression

    Posted Sep 26, 2018 08:39 AM

    I am getting below error while executing the attached script, please help

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

    At D:\get_drive.ps1:75 char:54

    +         $vmDisk = $vm | Get-HardDisk | Where-Object {$_.ExtensionData.backing.uu ...

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

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

        + FullyQualifiedErrorId : InvokeMethodOnNull

    please help



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

    Posted Sep 26, 2018 09:10 AM

    What does this return?

    Get-VM -Name vcntrapp01 | Get-HardDisk | Select Name,@{N='UUID';E={$_.ExtensionData.backing.uuid}}



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

    Posted Sep 26, 2018 09:19 AM

    Below is the output

    PS D:\> Get-VM -Name vcntrapp01 | Get-HardDisk | Select Name,@{N='UUID';E={$_.ExtensionData.backing.uuid}}

    Name                                                                                                UUID

    ----                                                                                                ----

    Hard disk 1                                                                                         6000C290-d1fc-72e1-0afa-6a5e8361c76f

    Hard disk 2                                                                                         6000C29d-f5b5-c448-13cc-a905a88b93ef

    Hard disk 3                                                                                         6000C295-a567-56aa-4504-6d1b34ea0d09

    Hard disk 4



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

    Posted Sep 26, 2018 09:38 AM

    And that explains why you get the error.
    Hard disk 4 doesn't have a UUID it seems, and calling the Replace method on a $null value gives that error.

    You could try to avoid the problem by replacing that line with

    $vmDisk = $vm | Get-HardDisk | Where-Object {$_.ExtensionData.backing.uuid -and $_.ExtensionData.backing.uuid.replace("-","") -eq $VMdiskSerialNumber.SerialNumber}

    And this proves the point I have been making since a long time once again, there is no fool-proof method to map guest OS partitions to VMDK :smileygrin:



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

    Posted Sep 26, 2018 11:21 AM

    Wow....that worked..... thanks a lot master :smileyhappy: