Share via


How to Use Powershell to Turn Static Groups into Dynamic Groups

FIM ScriptBox Item

Summary

This script transforms static groups into dynamic groups. The script reads a CSV file (delimited by tabs), "MyFile.csv" to identify and modify static groups into dynamic groups. The CSV file takes in DisplayName-Filter pairs.

The script will look up the groups by DisplayName, removes all its explicit members, and sets the appropriate attributes to make those groups dynamic.

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
if (@(get-pssnapin | where-object {$_.Name -eq "FIMAutomation"} ).count -eq 0)
{
 Add-PSSnapIn FIMAutomation
}

function GenerateFilter
{
 PARAM ($xpathFilter)
 END
 {    
  return "<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`">" + $xpathFilter + "</Filter>"
 }
}

function CreateImportChange
{
 PARAM($AttributeName, $AttributeValue, $Operation)
 END
 {
  $importChange = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange
  $importChange.Operation = $Operation
  $importChange.AttributeName = $AttributeName
  $importChange.AttributeValue = $AttributeValue
  $importChange.FullyResolved = 1
  $importChange.Locale = "Invariant"
  return $importChange
 }
}

function GetAttributeValueFromResource
{
 PARAM ($exportObject, $attributeName)
 END
 {
  foreach ($attribute in $exportObject.ResourceManagementObject.ResourceManagementAttributes)
  {    
   if($attribute.AttributeName.Equals($attributeName))
   {
    if ($attribute.IsMultiValue)
    {
     return $attribute.Values
    }
    else
    {
     return $attribute.Value
    }
   }
   
  }
  return $null
 }
}

$csv = Import-Csv -delimiter `t -header "GroupName","Filter" "MyFile.csv"

foreach ($entry in $csv)
{
 $myGroupName=$entry.GroupName
 $myFilter = $entry.Filter

 $group = Export-FIMConfig -customConfig "/Group[DisplayName='$myGroupName']" -onlyBaseResources
 if ($group -eq $NULL) #if group doesn't exist, continue
 {
  continue
 }
 $filter = GenerateFilter -xpathFilter $myFilter

 #construct the web service operation
 $importObject = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject
 #the object type is Group
 $importObject.ObjectType = "Group"
 #we are modify the group we've identified above
 $importObject.SourceObjectIdentifier = $group.ResourceManagementObject.ObjectIdentifier
 $importObject.TargetObjectIdentifier = $group.ResourceManagementObject.ObjectIdentifier
  #Put operation is enum 1
 $importObject.State = 1

 #construct the operation to Replace filter, Replace attribute operation is enum 1
 $importObject.Changes += CreateImportChange -attributeName "Filter" -attributeValue $filter -operation 1

 #construct the operation to change membership add workflow to None. Replace attribute operation is enum 1
 $importObject.Changes += CreateImportChange -attributeName "MembershipAddWorkflow" -attributeValue "None" -operation 1

 #construct the operation to change membership locked to True. Replace attribute operation is enum 1
 $importObject.Changes += CreateImportChange -attributeName "MembershipLocked" -attributeValue "True" -operation 1

 #construct the operations to remove explicit members. Remove attribute operation is enum 2

 $explicitMembers = GetAttributeValueFromResource -exportObject $group -attributeName "ExplicitMember"
 if ($explictMembers -ne $NULL)
 {
  foreach ($explicitMember in $explicitMembers)
  {
   $importObject.Changes += CreateImportChange -attributeName "ExplicitMember" -attributeValue $explicitMember -Operation 2
  }
 }
 
 $importObject | Import-FIMConfig -Uri $URI
}

 

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