20 August 2014

Script to rename groups in FIM


Add-PSSnapin FIMAutomation

$filter = "/Group[(starts-with(DisplayName,'recipients%'))]"

# import grups from FIM
$fimgroups = Export-FIMConfig -Uri http://faber:5725/ResourceManagementService -CustomConfig $filter -OnlyBaseResources

foreach ($fimgroup in $fimgroups){
   
    $groupNewName = $fimgroup.ResourceManagementObject.ResourceManagementAttributes.Where({$_.AttributeName -eq 'AccountName'}).Value.Replace('recipients ','MR_')
    $fimgroupid = $fimgroup.ResourceManagementObject.ResourceManagementAttributes.Where({$_.AttributeName -eq 'Objectid'}).Value
   
    # To perform an operation on a FIM object, first we need to create an ImportObject:
    $importObject = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject
    # "shortcut" for the enum that defines the import state
    $ImportState = [Microsoft.ResourceManagement.Automation.ObjectModel.ImportState]
    # "shortcut" for the enum that defines the operation type
    $ImportOperation = [Microsoft.ResourceManagement.Automation.ObjectModel.ImportOperation]
    # import object properties
    $importObject.ObjectType = 'Group'
    $importObject.TargetObjectIdentifier = $fimgroupid
    $importObject.SourceObjectIdentifier = $fimgroupid
    $importObject.State = $ImportState::Put

    # import change object for account name
    $importChange = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange
    # import change properties
    $importChange.Operation = $ImportOperation::Replace
    $importChange.AttributeName = 'AccountName'
    $importChange.AttributeValue = $groupNewName
    $importChange.FullyResolved = 1
    $importChange.Locale = 'Invariant'
    # add changes to importObject
    $importObject.Changes += $importChange

    # import change object for account name
    $importChange = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange
    # import change object for displayname
    $importChange.Operation = $ImportOperation::Replace
    $importChange.AttributeName = 'DisplayName'
    $importChange.AttributeValue = $groupNewName
    $importChange.FullyResolved = 1
    $importChange.Locale = 'Invariant'
    # add changes to importObject
    $importObject.Changes += $importChange

    # import change object to FIM
    Import-FIMConfig -Uri http://faber:5725/ResourceManagementService -ImportObject $importObject
}

No comments:

Post a Comment