Powershell – Error – “The property …. cannot be found on this object. Verify that the property exists.”

Background

Playing around with Powershell and ran into an error that one should avoid through defensive programming.

Error

Error – Image

property.notFound.01.20200507.1003PM.PNG

Error – Text


The property 'nationality' cannot be found on this object. Verify that the property exists.
At property\customObject.property.ps1:57 char:33
+         Write-Host "`tNationality: '$($person.nationality)'";
+                                       ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

        Nationality: ''

Outline

  1. We will create a few standalone objects using PSCustomObject
  2. Add created objects into an array
  3. Iterate through the array
  4. Access each object
    • Retrieve property
  5. Because our object’s properties are created on the fly, there is no inherent type checking
  6. On our last object, we will intentionally skip setting the nationality property

Code


Set-StrictMode -Version 2.0

# initialize the array
[PsObject[]]$objCandidate = @()

$objHuman1 = [pscustomobject]@{
    name='L.L. Cool J';
    gender='M';
    nationality='American';
}

$objHuman2 = [pscustomobject]@{
    name='Fresh Prince';
    gender='M';
    nationality='American';

}

$objHuman3 = [pscustomobject]@{
    name='Slick Rick';
    gender='M';
}

$objCandidate += $objHuman1;
$objCandidate += $objHuman2;
$objCandidate += $objHuman3;

foreach($person in $objCandidate) {

    Write-Host "`tName:- '$($person.Name)'";

    Write-Host "`tGender:- '$($person.gender)'";

    Write-Host "`tNationality: '$($person.nationality)'";

    Write-Host "";

}

Workaround

Outline

  1. Get a list of the object’s properties name
  2. Perform an in search against the list

Code


[string] $propertyNationality = 'nationality';

function hasProperty($object, $propertyName)
{
    $hasProperty = $propertyName -in $object.PSobject.Properties.Name;

    return($hasProperty);
}

foreach($person in $objCandidate) {

    # Does object have property
    $propNationalityExists = hasProperty $person $propertyNationality;

    Write-Host "`tName:- '$($person.Name)'";

    Write-Host "`tGender:- '$($person.gender)'";

    if ( ($propNationalityExists -eq $True)
    {

        Write-Host "`tNationality: '$($person.nationality)'";
    }

    Write-Host "";

}

Crediting

dan-gph

Link

dangph

Code Sharing

GitHub

Repository

DanielAdeniji/powershell.customObject

Folder

Link

Files
  1. property.dadeniji.noValidation.ps1
    Link
  2. property.dadeniji.validation.ps1
    Link

 

References

  1. Stack Overflow
    • How can you test if an object has a specific property
      Link
    • How to dynamically add new properties to custom object in PowerShell
      Link
    • How to initialize an array of custom objects
      Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s