Export Department Names

Applies To: Forefront Identity Manager 2010

This Windows PowerShell script example shows how to read Person data from the Microsoft® Forefront® Identity Manager (FIM) 2010 database and store the names of unique "Department" values in a file.

Example

This Windows PowerShell script reads the "Department" value of each Person resource (Department is a default attribute of a Person resource) and writes unique "Department" values to the "Department.txt" file.

if (@(get-pssnapin | where-object {$_.Name -eq "FIMAutomation"} ).count -eq 0)
{
    add-pssnapin FIMAutomation
}

# GetAttributeValueFromResource takes an exportObject and attribute name as
# parameters and returns the attribute value.
# If the attribute is not stored in the exportObject, the function returns null.
function GetAttributeValueFromResource
{
    PARAM ($exportObject, $attributeName)
    END
    {
        foreach ($attribute in $exportObject.ResourceManagementObject.ResourceManagementAttributes)
        {             
            if($attribute.AttributeName.Equals($attributeName))
            {
                return $attribute.Value
            }
            
        }
        return $null
    }
}

# Query FIM for all Person resources and store result in exportedPersons.
$query = "/Person"
$exportedPersons = Export-FIMConfig -uri "https://localhost:5725/ResourceManagementService" -customConfig $query -onlyBaseResources
Write-Host ("The script has exported " + $exportedPersons.Count + " persons based on the filter: " + $query)

# Declare departmentList hashtable to store distinct department names.
$departmentList = @{} 

# Use GetAttributeValueFromResource function to retrieve the Department values from
# each exportObject in exportedPersons. Store Department values in the departmentList hashtable. 
foreach ($i in $exportedPersons)
{
    $attributeValue = GetAttributeValueFromResource -exportObject $i -attributeName "Department"
    if ($attributeValue -ne $null)
    {
        $departmentList.Set_Item($attributeValue, 0)
    }
}

# Write all unique Department values into a file.
Write-Host "Writing the distinct Department to file 'Department.txt'"
$departmentList.Keys | Out-File Department.txt

Remarks

This example may not work for deployments that contain a very large number of Person resources. If you have to export data from a very large number of resources, you should consider modifying the code to send multiple queries to the Export-FIMConfig object to retrieve only a subset of resources at a time.

The Create Distribution Groups Based on Department Names example uses the "Department.txt" file that is created in this example to create a distribution group for each department that includes all users in that department.

See Also

Reference

Export-FIMConfig

Concepts

Create Distribution Groups Based on Department Names
FIM Windows PowerShell Cmdlet Examples