FIMService PowerShell Module

A nice to have PowerShell Module when doing different kinds of PowerShell scripting against the FIMService in FIM 2010 R2 or MIM 2016. But just using the builtin FIMAutomation PSSnapin.

On codeplex and other places there are some great PowerShell libraries to use when scripting against FIM/MIM. But I often run into customers where third party dlls and addons are hard to get approved. So I started to make myself a small PS Module so that I didn’t have to rewrite the most common things all over. In the latest version of this I have also added pipeline support to some of the functions in it making it possible to for example make the following command.

GetByxPath -xPath "/Person[EmployeeType = 'Contractor']" | ImportObject | DeleteObject

This would delete all Contractors in the FIM Service.

Feel free to use it as is or cut functions into your own scripts when you need one.

<#
.SYNOPSIS
	PowerShell module containing functions to work with the FIMService in FIM 2010 R2 and/or MIM 2016.
	Accepts FIMService URL as Argument.
	Errorhandling within module is very limited. Calling script needs to handle errors.
.EXAMPLE
	Import-Module FIMServiceModule.psm1 -Argumentlist 'http://idmservice.konab.net:5725/ResourceManagementService'
.EXAMPLE
	Import-Module FIMServiceModule.psm1
	Will use default URI http://localhost:5725/ResourceManagementService
#>

PARAM([string]$URI = "http://localhost:5725/ResourceManagementService")
if(@(Get-PSSnapin | Where-Object {$_.Name -eq "FIMAutomation"} ).count -eq 0) {Add-PSSnapin FIMAutomation -ErrorAction SilentlyContinue}

function CreateObject
{
    <#
    .SYNOPSIS
		Creates a new object of type Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject.
		This object needs to be saved using the SaveObject in order to be commited to the FIMService.
		Supports pipeline input.
	.PARAMETER objectType
		The system name of the FIMService resource type.
	.EXAMPLE
		CreateObject -objectType Person
	.EXAMPLE
		Objects.Type | CreateObject
    #>

    PARAM(
         [Parameter(ValueFromPipeline=$true)]
         [string[]]$ObjectType
         )
    BEGIN{}
    PROCESS
    {
       foreach($Type in $ObjectType)
       {
       $NewObject = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject
       $NewObject.ObjectType = $Type
       $NewObject.SourceObjectIdentifier = [System.Guid]::NewGuid().ToString()
       return [Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject]$NewObject
       }
    }
    END{} 
}

function ImportObject
{
	<#
	.SYNOPSIS
		Converts an $ExportObject of type [Microsoft.ResourceManagement.Automation.ObjectModel.ExportObject] 
		to an $ImportObject of type [Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject]
		Supports pipeline input.
	#>
    PARAM
        (
        [Parameter(ValueFromPipeline=$true)]
        [Microsoft.ResourceManagement.Automation.ObjectModel.ExportObject[]]$ExportObject
        )
    BEGIN{}
    PROCESS
    {
       foreach($RMObject in $ExportObject.ResourceManagementObject)
       {
       $ImportObject = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject
       $ImportObject.ObjectType = $RMObject.ObjectType
       $ImportObject.TargetObjectIdentifier = $RMObject.ObjectIdentifier
       $ImportObject.SourceObjectIdentifier = $RMObject.ObjectIdentifier
       $ImportObject.State = 1
       return [Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject]$ImportObject
       }
    }
    END{}
}

function SetAttribute
{#Only for SingleValue attributes
    PARAM([Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject]$ImportObject, [string]$AttributeName, $AttributeValue)
    END
    {
        $ImportChange = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange
        $ImportChange.Operation = 1
        $ImportChange.AttributeName = $AttributeName
        $ImportChange.AttributeValue = $AttributeValue
        $ImportChange.FullyResolved = 1
        $ImportChange.Locale = "Invariant"
        if ($ImportObject.Changes -eq $null) {$ImportObject.Changes = (,$ImportChange)}
        else {$ImportObject.Changes += $ImportChange}
    }
}

function AddMultiValue
{
    PARAM([Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject]$ImportObject, [string]$AttributeName, $AttributeValue)
    END
    {
        $ImportChange = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange
        $ImportChange.Operation = 0
        $ImportChange.AttributeName = $AttributeName
        $ImportChange.AttributeValue = $AttributeValue
        $ImportChange.FullyResolved = 1
        $ImportChange.Locale = "Invariant"
        if ($ImportObject.Changes -eq $null) {$ImportObject.Changes = (,$ImportChange)}
        else {$ImportObject.Changes += $ImportChange}
    }
}

function RemoveMultiValue
{
    PARAM([Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject]$ImportObject, [string]$AttributeName, $AttributeValue)
    END
    {
        $ImportChange = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange
        $ImportChange.Operation = 2
        $ImportChange.AttributeName = $AttributeName
        $ImportChange.AttributeValue = $AttributeValue
        $ImportChange.FullyResolved = 1
        $ImportChange.Locale = "Invariant"
        if ($ImportObject.Changes -eq $null) {$ImportObject.Changes = (,$ImportChange)}
        else {$ImportObject.Changes += $ImportChange}
    }
}

function SetFilter
{
	<#
	.SYNOPSIS
		Special function to set set the filter attribute in Set or Group. 
		Expects the inner xPath query as input and adds the XML around it before setting the value.
		Does not support pipeline input.
	#>
    PARAM([Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject]$ImportObject,[string]$xPath)
	BEGIN{}
	PROCESS
    {
        $FilterXMLBegin ='<Filter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Dialect="http://schemas.microsoft.com/2006/11/XPathFilterDialect" xmlns="http://schemas.xmlsoap.org/ws/2004/09/enumeration">'
        $FilterXMLEnd = '</Filter>'
        $Filter = $FilterXMLBegin + $xPath + $FilterXMLEnd
        SetAttribute -object $ImportObject -attributeName Filter -attributeValue $Filter
    }
	END{}
}

function GetReference
{
	<#
	.SYNOPSIS
		Returns the ObjectId (Guid) as string of the returned object. Expects a single object to be found in the query.
        .PARAMETER objectType
            The system name of the FIMService resource type.
        .PARAMETER attributeName
            The system name of the FIMService attribute.
        .PARAMETER attributeValue
            The value in the format expected by the attribute.
	#>
    PARAM([string]$ObjectType,[string]$AttributeName, $AttributeValue)
    END
    {
        $xPath = "/"+$ObjectType+"["+$AttributeName+"='"+$AttributeValue+"']"
        $ExportObject = export-fimconfig -uri $URI –onlyBaseResources -customconfig $xPath
        if($ExportObject)
            {return $ExportObject.ResourceManagementObject.ObjectIdentifier.Substring(9)}
        else
            {return $null}
     } 
}

function GetObject
{
    <#
        .SYNOPSIS
            Returns object based on single attribute/value match.
            Intended to be used when only single result is expected.
            Does not support pipeline input.
        .PARAMETER objectType
            The system name of the FIMService resource type.
        .PARAMETER attributeName
            The system name of the FIMService attribute.
        .PARAMETER attributeValue
            The value in the format expected by the attribute.
    #>
    PARAM([string]$ObjectType,[string]$AttributeName, $AttributeValue)
    BEGIN
    {}
    PROCESS
    {
        $xPath = "/"+$ObjectType+"["+$AttributeName+"='"+$AttributeValue+"']"
        $ExportObject = export-fimconfig -uri $URI –onlyBaseResources -customconfig $xPath
    }
    END
    {
        return $ExportObject
    }
}

function GetByxPath
{
	<#
	.SYNOPSIS
		Returns array of [Microsoft.ResourceManagement.Automation.ObjectModel.ExportObject] based on xPath query.
		Returns only base resources not dereferenced objects.
		Does not support pipeline input.
	.PARAMETER xPath
		xPath query to use.
		Example: /Person[AccountName='Kent' and extActive=True]
	#>
	PARAM([string]$xPath)
	BEGIN
    {}
	PROCESS
	{
		$ExportObject = export-fimconfig -uri $URI –onlyBaseResources -customconfig $xPath
	}
	END
	{
		return $ExportObject
	}
}

function SaveObject
{
	<#
	.SYNOPSIS
		Saves/Updates the object in FIM Service.
		Supports pipelining.
	.PARAM importObject
		The [Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject] to commit to FIM Service.
	#>
    PARAM(
		[Parameter(ValueFromPipeline=$true)]
		[Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject[]]$ImportObject
	)
	BEGIN{}
	PROCESS
	{
		foreach($Object in $ImportObject)
		{
			$Object | Import-FIMConfig -Uri $URI -ErrorAction Stop
		}
	}
    END{}
}

function DeleteObject
{
	<#
	.SYNOPSIS
		Delete the object in FIMService.
		Supports pipelining
	.PARAM importObject
		The [Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject] to delete in FIM Service.
	#>
    PARAM(
		[Parameter(ValueFromPipeline=$true)]
		[Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject[]]$ImportObject
	)
	BEGIN{}
	PROCESS
	{
		foreach($Object in $ImportObject)
		{
			$Object.State = 2
			$Object | Import-FIMConfig -Uri $URI -ErrorAction Stop
		}
	}
    END{}
}

UAG does not detect FW in Windows 10

Just upgraded my client to Windows 10 and discovered that UAG does not really like my client anymore.

The new Edge browser isn’t supported at all if client components are required.
If I start my IE11 on my Windows 10 client UAG fails to detect the FireWall causing all access requiring a compliant FW to fail.
UAGFindNoFWonWin10
If your clients start using Windows 10 you might need to remove any requirements for FW in your UAG policies.

Using FIM Portal as administrative tool for AD

In the April FIM Team User Group meeting I talked about how to use the FIM Portal as administrative tool using just the built-in OOB functionality in FIM.
If you have any questions regarding this session, please comment to this post.

Training for Microsoft Identity Manager

The syllaby for the next version of my FIM/MIM training is now starting to take shape. After the summer delivery will start of my new Mastering Microsoft Identity Manager class.

In co-operation with partners like Labcenter in Sweden and TrueSec Inc in USA I will also make this training available as remote class.

For all my customers in the US, I am happy to announce that we are planning to have a class-room training available in Seattle around october/november… Stay in touch for final dates.
[EDIT 2015-04-25] The first date for training in US (Redmond, WA) is now set to Oct 27-29 2015. Register here.

FIM MVP 2015

I am happy to announce that I have been awarded MVP for FIM/MIM as of April 2015!
Or as it said in the email…
Dear Kent Nordstrom,
Congratulations! We are pleased to present you with the 2015 Microsoft® MVP Award! This award is given to exceptional technical community leaders who actively share their high quality, real world expertise with others. We appreciate your outstanding contributions in Forefront Identity Manager technical communities during the past year.

MVP_Logo_Horizontal_Preferred_Cyan300_RGB_300ppi

FIM 2010 licensing changing

In April 2015 the licensing model for FIM is changing. Basically you can say that FIM synchronization service is now free!
FIMLicenses
Read this blog by Peter Geelen for more information, or download the Microsoft Product Use Rights (WW, English, April 2015) document.

FIM 2010 R2 Pre-Requisites

Installing a complete FIM 2010 R2 environment is not an easy task. Lots of things can go wrong. A FIM setup might use almost any number of servers for example. In this post I give you access to scripts and configuration files required to do all FIM 2010 R2 pre-requisites in a typical 4-server FIM deployment.
FIMFourServerSetup

In the FIMScriptsAndCode.zip you will find PowerShell scripts to create service accounts, configure Kerberos, configure SQL ports and aliases among other things. You will also find ini-files to perform unattended setups of the SQL servers used by FIM. Basically everything you need to do before you can start the installation of FIM.

In the Installation folder in the download package you will find a ReadMeFIRST.txt file that explains the order to perform the steps. Please note that many of the scripts and configuration files used requires editing to match the target environment.

In the RulesExtension folder you will find a simple code example showing how to use rules extension in FIM to import the last logon timestamp from AD into FIM. This is one of the things I show in my FIM 2010 R2 Video Course and this is to give the viewers of that a chance to download the example.

Finding Managers in FIM 2010

When working with FIM 2010 we often use Reference attributes like the Manager attribute. But in FIM we cannot create a corresponding Set with all referenced Managers. The xPath query simply doesn’t allow this kind of referenced object lookup.

So what if I would like a Set or a Group containing all Managers! How do we solve that problem?

In this post I will show you one way of doing this, but please remember that there are many variations of this you could use.

Add IsManager to Schema

First we add a new boolean attribute “IsManager” to the schema in FIM. Depending on how you plan to use this you might need to add it to both the FIM Service and the FIM Synchronization Service schema.
This step I hope you all know involves creating a binding as well as adding the new attribute to the Filter Permissions.
You will also need to add it to some MPR’s to allow the management of this new attribute.
Ones we have this new boolean attribute creating a Set and/or a Group is now reduced to look at this boolean value.

Manage the IsManager flag

The problem is now to have some kind of activity that sets the IsManager attribute to “true” when a manager is configured on a user.
This can be done using a PowerShell WorkFlow activity. I show you a solution working with the PowerShell Workflow Activity you can download from codeplex.. The PowerShell I use in the activity can be downloaded UpdateIsManager.

PowerShell Activity

Let’s look at the PowerShell script I use to understand what it does and how it works.

First we set the basics like URI and load the FIMAutomation snapin.

set-variable -name URI -value "http://localhost:5725/resourcemanagementservice' " -option constant
if(@(get-pssnapin | where-object {$_.Name -eq "FIMAutomation"} ).count -eq 0) {add-pssnapin FIMAutomation}

We then get the object that has been changed. Note that I use the “/*” in the xPath since I get the object based on ObjectID I cannot get duplicates and don’t need to specify the object type.

$exportObject = export-fimconfig -uri $URI -onlyBaseResources -customconfig ("/*[ObjectID='{0}']" -F $fimwf.TargetID.Guid)

From the modified user we read the Manager attribute to get the “target” for our change.

$target = $exportObject.ResourceManagementObject.ResourceManagementAttributes | Where-Object {$_.AttributeName -eq "Manager"}

We then build the importChange to set the IsManager attribute to true.

$importChange = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange
$importChange.Operation = 1
$importChange.AttributeName = "IsManager"
$importChange.AttributeValue = $true
$importChange.FullyResolved = 1
$importChange.Locale = "Invariant"

Finally we import the change on the target user

$importObject = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject
$importObject.ObjectType = "Person"
$importObject.TargetObjectIdentifier = $target.Value
$importObject.SourceObjectIdentifier = $target.Value
$importObject.State = 1 
$importObject.Changes = (,$importChange)
$importObject | Import-FIMConfig -uri $URI

If you apply this logic to a MultiValue reference attribute the script needs to be extended with a loop like the one below to apply the change to all referenced objects.

ForEach($value in $target.Values)

The importObject’s ObjectIdentifier would than just be $value within the loop.

The MPR

The MPR to trigger this Workflow is just a simple Request MPR that fires whenever someone changes the Manager attribute on any user.

What about deletes?

As you can see this solution will only make sure that the IsManager is set to true for all referenced managers. But it will not set it to “false” if they are no longer referenced. The problem is that if you were to detect the deletion of a manager you cannot set it to false since it might be referenced on other users. Doing that lookup, to verify this was the “last” reference, is quite costly from a resource perspective and I don’t think it’s a good idea to do it as part of the WorkFlow activity.

I think that a good approach to set the IsManager to “false” is to have a maintenance job running a PowerShell that verifies that all IsManager=”true” indeed still are referenced. That PowerShell will however have to wait for another post, another time.

Summary

Adding small boolean flags to the schema makes filters in Set’s, Group’s and Synchronization Rules very easy to implement. Using a PowerShell Workflow Activity is often the easiest and quickest way of solving workflow needs to make sure these boolean flags are kept up-to-date in FIM 2010.