Share via


How to Use PowerShell to Create a Criteria-Based Set

FIM ScriptBox Item

Summary

The script code below creates a criteria-based sample Set.

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
#----------------------------------------------------------------------------------------------------------
 Set-Variable -Name URI       -Value "http://localhost:5725/resourcemanagementservice" -Option Constant 
 Set-Variable -Name SetName   -Value "Test Set"                                        -Option Constant
 Set-Variable -Name SetFilter -Value "/Person[EmployeeType = 'Contractor']"            -Option Constant
#----------------------------------------------------------------------------------------------------------
 Function SetAttribute
 {
    Param($object, $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 ($object.Changes -eq $null) {$object.Changes = (,$importChange)}
        Else {$object.Changes += $importChange}
    }

#----------------------------------------------------------------------------------------------------------
 Function CreateObject
 {
    Param($objectType)
    End
    {
       $newObject = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject
       $newObject.ObjectType = $objectType
       $newObject.SourceObjectIdentifier = [System.Guid]::NewGuid().ToString()
       $newObject
     } 
 }
#----------------------------------------------------------------------------------------------------------
 If(@(Get-PSSnapin | Where-Object {$_.Name -eq "FIMAutomation"} ).count -eq 0) {Add-PSSnapin FIMAutomation}
 Clear-Host
 $exportObject = export-fimconfig -uri $URI `
                                  â€“onlyBaseResources `
                                  -customconfig "/Set[DisplayName='$SetName']"
 
 If($exportObject) {Throw "L:Set already exists: $SetName"}

 $newSet = CreateObject -objectType "Set"
 SetAttribute -object $newSet `
              -attributeName  "DisplayName" `
              -attributeValue $SetName 

 $filter = "<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"" " + `
           "http://schemas.xmlsoap.org/ws/2004/09/enumeration"">" + `
           $SetFilter + `
           "</Filter>" 

 SetAttribute -object $newSet `
              -attributeName  "Filter" `
              -attributeValue $filter 
#----------------------------------------------------------------------------------------------------------
 $newSet | Import-FIMConfig -uri $URI
 Write-Host "`nSet created successfully`n"
#----------------------------------------------------------------------------------------------------------
 Trap 
 { 
    $exMessage = $_.Exception.Message
    if($exMessage.StartsWith("L:"))
    {Write-Host "`n" $exMessage.substring(2) "`n" -foregroundcolor white -backgroundcolor darkblue}
    else {Write-Host "`nError: " $exMessage "`n" -foregroundcolor white -backgroundcolor darkred}
    Exit 1
 }
#----------------------------------------------------------------------------------------------------------

 

Note

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

 


See Also