Tuesday, March 03, 2015

Using Group-Object and Expressions on FIM ExportObjects

I am generating a PowerShell Desired State Configuration (DSC) configuration script from an existing FIM Service configuration (why write script when I can get my script to do that for me?).  The resulting document is quite large so I thought it would be cool to organize it by object type.  Going a step further, I could use #region tags in the script to allow the user to collapse and expand whole sections of the configuration.

The Group-Object command in PowerShell makes this pretty easy but there is a slight trick to it.  Group-Object groups by a property, which is normally pretty simple when the property is on the object itself.  For the FIM ExportObject the property I need to group by is buried down a couple of levels so it isn’t straight forward but still pretty easy (thanks to PowerShell!).  The Property parameter accepts an expression, so you can get pretty fancy with what you put in there.  In this case I wanted to dig into the FIM Export object to pick out the ObjectType property.

Here’s how to use Group-Object to group using a property that is buried somewhere in the object:

### Export the FIM Service configuration

Add-PSSnapin FimAutomation

$fimExportObjects = Export-FIMConfig -PolicyConfig -PortalConfig -SchemaConfig

 

<#

Each ExportObject looks like this:

 

    ExportObject

    ExportObject.ResourceManagementObject

    ExportObject.ResourceManagementObject.ObjectType

    ExportObject.ResourceManagementObject.ObjectIdentifier

    ExportObject.ResourceManagementObject.IsPlaceholder

    ExportObject.ResourceManagementObject.ResourceManagementAttributes

    ExportObject.ResourceManagementObject.ResourceManagementAttributes.AttributeName

    ExportObject.ResourceManagementObject.ResourceManagementAttributes.HasReference

    ExportObject.ResourceManagementObject.ResourceManagementAttributes.IsMultiValue

    ExportObject.ResourceManagementObject.ResourceManagementAttributes.Values

    ExportObject.ResourceManagementObject.ResourceManagementAttributes.Value

 

#>

 

### Group the resulting objects by ObjectType

$fimExportObjects | Group-Object -Property {$_.ResourceManagementObject.ObjectType}

 

<#

The output will look something like this:

 

Count Name                           

----- ----                           

    2 FilterScope                    

  127 SynchronizationRule            

   99 ActivityInformationConfiguration

   68 SearchScopeConfiguration       

   87 HomepageConfiguration          

    1 PortalUIConfiguration          

   66 NavigationBarConfiguration     

    1 SynchronizationFilter          

  500 Set

    2 Ponies                            

   70 WorkflowDefinition             

    2 ManagementPolicyRule           

    1 ObjectVisualizationConfiguration

   66 EmailTemplate                  

#>

 

The beauty here is not just the host output, but that we can now process each group in a loop.  Sweet!

No comments: