Modules/AzureDevOpsDsc.Common/Resources/Functions/Private/Test-ObjectProperty.ps1

<#
.SYNOPSIS
Checks if a property exists in an object.
 
.DESCRIPTION
The Test-ObjectProperty function checks if a specified property exists in an object. It supports checking properties in hashtables, PSCustomObjects, and PSObjects.
 
.PARAMETER Object
The object to check for the existence of the property.
 
.PARAMETER PropertyName
The name of the property to check for.
 
.OUTPUTS
System.Boolean
Returns $true if the property exists in the object, otherwise returns $false.
 
.EXAMPLE
$object = [PSCustomObject]@{
    Name = "John"
    Age = 30
}
 
Test-ObjectProperty -Object $object -PropertyName "Name"
# Returns $true
 
Test-ObjectProperty -Object $object -PropertyName "Email"
# Returns $false
#>


Function Test-ObjectProperty
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.Object]
        $Object,

        [Parameter(Mandatory = $true)]
        [System.String]
        $PropertyName
    )

    # Hashtables use ContainsKey; all other objects (PSCustomObject, PSObject, etc.) expose properties via PSObject
    if ($Object -is [System.Collections.Hashtable])
    {
        return $Object.ContainsKey($PropertyName)
    }

    return $Object.PSObject.Properties.Name -contains $PropertyName
}