Overview

PowerShell is a fantastic glue language.  We can use it to integrate many technologies, through APIs, services, and other interfaces.

When designing PowerShell solutions that use these technologies, discovering and exploring data and its schema is a very manual process.

This function will take in an object, and return a flat object with property names that are actual PowerShell syntax to get to the property in question.

The intent and core functionality is built around exploring the data from web APIs - JSON and XML in particular.  See notes section below for some caveats when converting other PowerShell objects.

Wrote a quick blog post on the topic - Exploring PowerShell Objects: Flattening

Instructions 

 

 

PowerShell
Edit|Remove
# dot source the function   
    . "\\Path\To\ConvertTo-FlatObject.ps1"   
   
# Get help for ConvertTo-FlatObject 
    Get-Help ConvertTo-FlatObject -Full   
 
#Pull unanswered PowerShell questions from StackExchange, Flatten the data to date a feel for the schema 
    Invoke-RestMethod "https://api.stackexchange.com/2.0/questions/unanswered?order=desc&sort=activity&tagged=powershell&pagesize=10&site=stackoverflow" | 
        ConvertTo-FlatObject -Include Title, Link, View_Count 
        
        <# 
        $object.items[0].owner.link : http://stackoverflow.com/users/1946412/julealgon 
        $object.items[0].view_count : 7 
        $object.items[0].link       : http://stackoverflow.com/questions/26910789/is-it-possible-to-reuse-a-param-block-across-multiple-functions 
        $object.items[0].title      : Is it possible to reuse a &#39;param&#39; block across multiple functions? 
        $object.items[1].owner.link : http://stackoverflow.com/users/4248278/nitin-tyagi 
        $object.items[1].view_count : 8 
        $object.items[1].link       : http://stackoverflow.com/questions/26909879/use-powershell-to-retreive-activated-features-for-sharepoint-2010 
        $object.items[1].title      : Use powershell to retreive Activated features for sharepoint 2010 
        ... 
        #> 
 
#Fun with XML 
    #Set up some XML to work with 
    $object = [xml]' 
        <catalog> 
           <book id="bk101"> 
              <author>Gambardella, Matthew</author> 
              <title>XML Developers Guide</title> 
              <genre>Computer</genre> 
              <price>44.95</price> 
           </book> 
           <book id="bk102"> 
              <author>Ralls, Kim</author> 
              <title>Midnight Rain</title> 
              <genre>Fantasy</genre> 
              <price>5.95</price> 
           </book> 
        </catalog>' 
     
    #Call the flatten command against this XML, include only leafs we specify 
    ConvertTo-FlatObject $object -Include Author, Title, Price 
     
        <# 
        $object.catalog.book[0].author : Gambardella, Matthew 
        $object.catalog.book[0].title  : XML Developers Guide 
        $object.catalog.book[0].price  : 44.95 
        $object.catalog.book[1].author : Ralls, Kim 
        $object.catalog.book[1].title  : Midnight Rain 
        $object.catalog.book[1].price  : 5.95 
        #> 
 
    #Invoking the property names should return their data if the orginal object is in $object: 
        $object.catalog.book[1].price 
           # 5.95 
 
        $object.catalog.book[0].title 
           # XML Developers Guide 
 
#Another XML example illustrating special characters in property names. 
 
    #Set up XML.  
        $Object = [xml]'<?xml version="1.0"?>  
        <contact-info>  
        <name>Jane Smith</name>  
        <company>AT&amp;T</company>  
        <phone>(212) 555-4567</phone>  
        </contact-info>'  
  
    #Run the command...  
        ConvertTo-FlatObject -InputObject $object 
  
        <#  
            $object.xml                    : version="1.0"  
            $object.{contact-info}.company : AT&T  
            $object.{contact-info}.phone   : (212) 555-4567  
        #>  
  
    #We now know how to access an example property  
        $object.{contact-info}.phone  
  
        #(212) 555-4567  
 
# View the properties and values under $MyInvocation while in a function. 
    Function View-MyInvocation {  
        param($Test)  
  
        #Within the function you are writing add a line similar to this...  
        #We use maxdepth 4 and exclude scriptblock just to simplify output  
        $MyInvocation | 
            ConvertTo-FlatObject -ExcludeDefault $false -exclude ScriptBlock -MaxDepth 4 | 
            Out-File C:\temp\myinvocation.txt 
  
    } 
 
    View-MyInvocation -Test "testing!" 
 
    #View the results... 
    notepad C:\temp\myinvocation.txt 
 
    <# 
        #subset of output from this simplified example 
 
        $Object.MyCommand.Module                   :  
        $Object.MyCommand.Name                     : View-MyInvocation 
        $Object.MyCommand.Parameters['Test'].Name  : Test 
        $Object.BoundParameters                    : {[Test, testing!]} 
        $Object.BoundParameters['Test']            : testing! 
        $Object.Line                               :     View-MyInvocation -Test "testing!"  
        $Object.InvocationName                     : View-MyInvocation 
 
    #>

 

Notes:

Changs