Thursday, July 07, 2011

Convert a FIM ExportObject to a PowerShell PSObject

Working with the output from Export-FimConfig is not always fun because you have to dig hard to get attributes off an object.

This function I just whipped up will convert a FIM ExportObject to a PowerShell PSObject.  The advantage here is that you can then use dot-notation <sp?> to dig out the attributes.

For example the Export-FimConfig used like this produces the output below.

001
Export-FIMConfig -CustomConfig "/Person[AccountName='hi']"

Source : http://localhost:5725/ResourceManagementService
ResourceManagementObject : Microsoft.ResourceManagement.Automation.ObjectModel.ResourceManagementObject

Source : http://localhost:5725/ResourceManagementService
ResourceManagementObject : Microsoft.ResourceManagement.Automation.ObjectModel.ResourceManagementObject

Now using the fancy new function we get:

001
Export-FIMConfig -CustomConfig "/Person[AccountName='hi']" | Convert-FimExportToPSObject

ObjectID            : urn:uuid:caf0178b-b8c1-41b2-bd71-f2f48b1fdf3b
AccountName         : HI
CreatedTime         : 7/8/2011 6:02:50 AM
Creator             : urn:uuid:7fb2b853-24f0-4498-9534-4e10589723c4
DisplayName         : HoofHearted IceMelted
Domain              : africa
FirstName           : HoofHearted
IsRASEnabled        : True
JobTitle            : Sheller
LastName            : IceMelted
MailNickname        : HI
ObjectType          : Person

In addition, it is easier to get at the attributes for each object:

001
002
003
004

$person = Export-FIMConfig -CustomConfig "/Person[AccountName='hi']" | 
   
Convert-FimExportToPSObject
$person.DisplayName
$person.AccountName

HoofHearted IceMelted

HI

Finally, here is the function:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029

Function Convert-FimExportToPSObject
{
   
Param
    (
       
[parameter(Mandatory=$true, ValueFromPipeline = $true)]
        [Microsoft.ResourceManagement.Automation.ObjectModel.ExportObject]
        $ExportObject
    )
   
Process
    {       
       
$psObject = New-Object PSObject
        $ExportObject.ResourceManagementObject.ResourceManagementAttributes | ForEach-Object
{
           
if ($_.Value -ne $null
)
            {
               
$value = $_.
Value
            }
           
elseif($_.Values -ne $null
)
            {
               
$value = $_.
Values
            }
           
else
            {
               
$value = $null
            }
           
$psObject | Add-Member -MemberType NoteProperty -Name $_.AttributeName -Value $value
        }
       
Write-Output $psObject
    }
}

4 comments:

Norman S. said...

Craig... You Rock! Thank you so much for the post and your elegant code. I plan to implement this in my next project.

Thomas said...

Nice stuff! Will try this for sure.

Bryan said...

Hey Craig,
Bryan here. we still have to hit up that hockey game.

nice script.

I assume you already have this but I add this to all my scripts so that i don't have to worry about loading my modules

#load FIM module if not already loaded
If(@(Get-PSSnapin | Where-Object {$_.Name -eq "FIMAutomation"} ).count -eq 0) {Add-PSSnapin FIMAutomation}

Craig Martin said...

Hey Bryan! We do have to catch a game down in Kent! Thanks for the snippet!