FIM ScriptBox Item

Summary

If you would like to wipe out the AdvancedFilter property on all the SearchScope objects within FIM (e.g. you may want to wipe them clean so that this PowerShell script can configure them consistently). This script will undo any custom AdvancedFilters you may have put on Search Scopes (only those SearchScopes which lists the DisplayName attribute as part of the SearchScopeContext).

If you want to revert all of your search scopes back to the original, FIM 2010 version you can use the forementioned PowerShell script at this location to revert the search scopes.
       

Script Code

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
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<# .SYNOPSIS Reverts all the search scopes to use Contains instead of starts-with for the DisplayName Attribute, except any Search Scope where the AdvancedFilter is already configured. .PARAMETER uri Address of FIM service. Default is http://localhost:5725 .PARAMETER echoOnly When supplied, only prints out messages and does not change the Search Scopes .PARAMETER force When supplied, will overwrite any AdvancedFilters that are already configured--not null and not an empty string after being trimmed. .DESCRIPTION Updates the AdvancedFilter property on all the SearchScope objects within FIM to use Contains instead of starts-with for the DisplayName Attribute. .NOTES Version 1.0.0.0 - 28/06/2012 .EXAMPLE .\bulk-searchstyle-revert Reverts all the search scopes to use Contains instead of starts-with for the DisplayName Attribute, except any Search Scope where the AdvancedFilter is already configured. .EXAMPLE .\bulk-searchstyle-revert -force Reverts all the search scopes to use Contains instead of starts-with for the DisplayName Attribute including any Search Scope where the AdvancedFilter is already configured. .EXAMPLE .\bulk-searchstyle-revert -echoOnly Prints out the Search Scope name and what the AdvancedFilter should be without actually altering the Search Scope. Useful for seeing what this function would alter. #>
[CmdletBinding()]
param(
    [string] $uri = "http://localhost:5725",
    [switch] $echoOnly
)


# load FIM snapin, ignore errors if already loaded
Add-PSSnapin FIMAutomation -ErrorAction SilentlyContinue


# gets the value of a single-valued attribute from an exported object
function GetAttributeValue($exportObject,[string] $name) {
    $attribute = $exportObject.ResourceManagementObject.ResourceManagementAttributes | 
        Where-Object {$_.AttributeName -eq $name}
    if ($attribute.Value -ne $null)
    {
        $value = $attribute.Value
    }
    elseif($attribute.Values -ne $null)
    {
        $value = $attribute.Values
    }
    else
    {
        $value = $null
    }
    return $value
}


# Changes the specified attribute to the specified value on the supplied object
function SetAndCommitAttributeValue($object, [string] $attributeName, $attributeValue) {
    $objectID = GetAttributeValue $object "ObjectID"
# $displayName = GetAttributeValue $object "DisplayName"
    $objectType = GetAttributeValue $object "ObjectType"

# Write-Host "Setting '$displayName'.'$attributeName' to '$attributeValue'"
    
    $importChange = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange
    $importChange.Operation = [Microsoft.ResourceManagement.Automation.ObjectModel.ImportOperation]::Replace
    $importChange.AttributeName = ${attributeName}
    if (${attributeValue} -ne $null) {
        $importChange.AttributeValue = ${attributeValue}
    }
    $importChange.FullyResolved = 1
    $importChange.Locale = "Invariant"

    $importObject = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject
    $importObject.ObjectType = $objectType
    $importObject.TargetObjectIdentifier = $objectID
    $importObject.SourceObjectIdentifier = $objectID
    $importObject.State = [Microsoft.ResourceManagement.Automation.ObjectModel.ImportState]::Put
    $importObject.Changes = (,$importChange)
    
# Commit those changes to the database
    $importObject | Import-FIMConfig -uri $uri
}


# Helper method to ensure that the AttributesSearched is always an object
 # array.
function ForceValuesIntoArray($attributesSearched) {
# If there is only 1 value from the multi-value property, it will be a
 # String. Put it in an object array
    $type = $attributesSearched.GetType().Name
    if($type -eq "String") {
        $temp = @()
        $temp = $temp + $attributesSearched
        $temp
        return $temp
    } else {
        $attributesSearched
        return $attributesSearched
    }
}


# Core method for wiping the AdvancedFilter attribute (i.e. setting it to
 # null).
function WipeAdvancedFilter($object) {
    $attributesSearched = GetAttributeValue $object "SearchScopeContext"
    $sanitized = ForceValuesIntoArray $attributesSearched
   
   # Remove the surrounding if statement below to wipe out the
    # Advanced Filter for every search scope not just the ones that use
    # DisplayName.
   
    if([Array]::IndexOf($sanitized, "DisplayName") -ge 0) {
        $displayName = GetAttributeValue $object "DisplayName"
        Write-Host "Clearing AdvancedFilter on '$displayName'"

        if(-not $echoOnly)
        {
            SetAndCommitAttributeValue $object "msidmSearchScopeAdvancedFilter" $null
        }
    }
}


# helper method for checking whether the supplied object has the
 # AdvancedFilter attribute configured already.
function AdvancedFilterNotSet($object) {
    $advancedFilter = GetAttributeValue $object "msidmSearchScopeAdvancedFilter"
    if([string]::IsNullOrEmpty($advancedFilter))
    {
        return $true
    } else {
        $advancedFilter = $advancedFilter.Trim()
        if($advancedFilter.Length -gt 0) {
            return $false
        } else {
            return $true
        }
    }
}

# suppress the progress indicator (makes screen unreadable with many
 # operations)
$ProgressPreference="SilentlyContinue"

$fimtype="SearchScopeConfiguration"

# get all objects of specified type
Write-Verbose "Getting $fimtype objects..."
$objects = Export-FIMConfig -uri $uri -CustomConfig "/$fimtype" -OnlyBaseResources

if($echoOnly){
    Write-Host "No changes will be made to any search scope"
}

# iterate objects and set attributes
foreach ($object in $objects) {
    WipeAdvancedFilter $object
}

if($echoOnly){
    Write-Host "No changes were made to any search scope"
}

 

note Note
To provide feedback about this article, create a post on the FIM TechNet Forum.
For more FIM related Windows PowerShell scripts, see the  FIM ScriptBox

 



See Also