How to Use PowerShell to Create a Criteria-Based Set

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"" " + `
           "xmlns=""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 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.

 

Leave a Comment
  • Please add 5 and 6 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 7. Comment: Removed (en-US) from title, added tag

  • Craig Lussier edited Revision 6. Comment: added en-US to tags and title

Page 1 of 1 (2 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • If generating a long $SetFilter in your script, you can run into a problem where powershell inserts end-of-line characters throughout the string, and it then can't be imported into FIM. To avoid this add the following line at the top of your script:

    $host.UI.RawUI.BufferSize = new-object System.Management.Automation.Host.Size(50000,50)

  • Craig Lussier edited Revision 6. Comment: added en-US to tags and title

  • Richard Mueller edited Revision 7. Comment: Removed (en-US) from title, added tag

Page 1 of 1 (3 items)