Powershell – Dynamically List Properties & Property values

Background

Back in Dec 2011, I stitched together a script to dynamically display property names and values.

Today I found it is no longer working.

Btw, here is the original post.

Script

Original

Code


Set-StrictMode -Version Latest;

function listObjectProperties($object)
{

    [int] $iPropertyIndex = 0;

    #prepare formatting
    $strFormat = "{0}) Name :- {1} - Value :- {2}"

    # get Object Properties
    $objProperties = $object.PsObject.Properties;

    #Iterate Object Properties
    Foreach ($objProperty in $objProperties)
    {

       # increment property counter
       $iPropertyIndex = $iPropertyIndex + 1;

       $objPropertyName = $objProperty.Name;

       #place variable name in single quotes to ensure that
       #PowerShell does not evaluate\substite value
       $objPropertyNameFull = '$object' + '.' + $objPropertyName

       #prepare to use variable substitution
       # Invoke-Expression
       # http://technet.microsoft.com/en-us/library/dd347550.aspx

       $objPropertyValue = invoke-expression $objPropertyNameFull;

       #format data
       $strLog = [String]::Format(
                                         $strFormat
                                       , $iPropertyIndex
                                       , $objPropertyName
                                       , $objPropertyValue
                                  );

       # display data
       $strLog;

     }

}

#PowerShell 2.0 Commands
#http://ss64.com/ps/
Clear-Host;

# instantiate object
[Datetime] $object = Get-Date;

if (!$object)
{
     "Object is null (empty)"
     return
}

# Keith Hill - Get Type name
# http://rkeithhill.wordpress.com/2007/10/28/powershell-quicktip-using-pstypenames-to-see-#
# the-typename-and-inheritance-chain/
$strLog = "Type name is " + $object.GetType().Fullname;
$strLog

# get Object Properties
$objProperties = $object.PsObject.Properties;

if (!$objProperties)
{
     "Object has no properties"
     return
}

#list properties
listObjectProperties $object

Output

Explanation

In the original example, I passed in a date object and things worked well.

Revised

Objective

Today tried passing in the results of get-azureaduser, but it was failing terrible.

Error

Image

Image #1

Image #2

Textual

Textual #1


The property 'ExtensionProperty' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $object.ExtensionProperty
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

1) Name :- ExtensionProperty - Value :-
The property 'DeletionTimestamp' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $object.DeletionTimestamp
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

2) Name :- DeletionTimestamp - Value :-
The property 'ObjectId' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $object.ObjectId
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

3) Name :- ObjectId - Value :-
The property 'ObjectType' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $object.ObjectType
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

4) Name :- ObjectType - Value :-
The property 'AccountEnabled' cannot be found on this object. Verify that the property exists.
At line:1 char:1
-- More  --

Textual #2


The property 'TelephoneNumber' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $object.TelephoneNumber
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict

The property 'UsageLocation' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $object.UsageLocation
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict

The property 'UserPrincipalName' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $object.UserPrincipalName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict

The property 'UserType' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $object.UserType
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict

Remediation

Summary

Replace


$objPropertyValue = invoke-expression $objPropertyNameFull;

with


$objPropertyValue = "$($objProperty.Value)"

Code


Set-StrictMode -Version Latest;
 
function listObjectProperties($object)
{
 
    [int] $iPropertyIndex = 0;
 
    #prepare formatting
    $strFormat = "{0}) Name :- {1} - Value :- {2}"

    # get Object Properties
    $objProperties = $object.PsObject.Properties;
     
    if (!$objProperties)
    {
         "Object has no properties"
         return
    }
    
    #Iterate Object Properties
    Foreach ($objProperty in $objProperties)
    {
 
       # increment property counter
       $iPropertyIndex = $iPropertyIndex + 1;
 
       $objPropertyName = $objProperty.Name;
 
       #place variable name in single quotes to ensure that
       #PowerShell does not evaluate\substite value
       $objPropertyNameFull = '$object' + '.' + $objPropertyName
 
       #prepare to use variable substitution
       # Invoke-Expression
       # http://technet.microsoft.com/en-us/library/dd347550.aspx
       
       # dadeniji 2018-01-22
       # $objPropertyValue = invoke-expression $objPropertyNameFull;
       
       $objPropertyValue = "$($objProperty.Value)"
 
       #format data
       $strLog = [String]::Format(
                                         $strFormat
                                       , $iPropertyIndex
                                       , $objPropertyName
                                       , $objPropertyValue
                                  );
 
       # display data
       $strLog;
 
     }
 
}
 
#PowerShell 2.0 Commands
#http://ss64.com/ps/
Clear-Host;
 
# instantiate object
[Datetime] $object = Get-Date;
 
if (!$object)
{
     "Object is null (empty)"
     return
}
 
# Keith Hill - Get Type name
# http://rkeithhill.wordpress.com/2007/10/28/powershell-quicktip-using-pstypenames-to-see-#
# the-typename-and-inheritance-chain/
$strLog = "Type name is " + $object.GetType().Fullname;
$strLog
 

 
#list properties
listObjectProperties $object

Source Code

Github

DanielAdeniji/PowerShellDynamicallyListProperties
Link

Summary

Wish I could tell you what went wrong.

But, hopefully if you are just as perplexed as I found myself, google on some terms and you will find this post, or a more able one.

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