How to Use PowerShell to Bulk Update the Value of an Attribute for a Certain Type
FIM ScriptBox Item
Summary
Changes the value of the given attribute for all the objects of the given type.
The script currently works only for single-valued attributes.
Cannot be used for datetime attributes (limitation of FIMAutomation snapin).
Warning
This script is suitable to be run only against a limited number of objects.
Since queries with the FIMAutomation snapin always return all the attributes of the results, with a high number of objects most likely the query made by the script to get the objects to update would never complete.
In case you want to update many objects, you should definitely check Brad Turner’s ROPU-based approach on this forum thread: Bulk modify users in FIM portal.
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 |
<# .SYNOPSIS Bulk update the value of an attribute for a certain type. .PARAMETER fimtype The type of the resource to update (Person, Group, etc). .PARAMETER attributeName The name of the attribute to modify. .PARAMETER attributeValue The value of the attribute. If not specified, unsets the attribute. .PARAMETER uri Address of FIM service. Default is http://localhost:5725 .PARAMETER ask If specified, asks for confirmation before each modification. .DESCRIPTION Changes the value of the given attribute for all the objects of the given type. Can be used only for single-valued attributes. Cannot be used for datetime attributes (limitation of FIMAutomation snapin). .NOTES Version 1.0.0.0 - 25/01/2011 - Migrated to FIM Scriptbox .EXAMPLE .\bulk-update Person Department HR Set the value of the "Department" attribute to "HR" for all users .EXAMPLE .\bulk-update Person Department -ask Unset the value of the "Department" attribute to "HR" for all users and ask for confirmation before each operation #> [CmdletBinding()] param( [Parameter(Mandatory=$true, HelpMessage="Specify the name of the class to update.")] [string] $fimtype, [Parameter(Mandatory=$true, HelpMessage="Specify the name of the attribute to update.")] [string] $attributeName, $attributeValue, [string] $uri = "http://localhost:5725", [switch] $ask) # 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 -ne $null -and $attribute.Value) { $attribute.Value } } # suppress the progress indicator (makes screen unreadable with many operations) $ProgressPreference="SilentlyContinue" # get all objects of specified type Write-Verbose "Getting $fimtype objects..." $objects = Export-FIMConfig -uri $uri -CustomConfig "/$fimtype" -OnlyBaseResources # confirmation message if (${attributeValue} -ne $null) { $confirmationMessage = "Set ${attributeName} to '${attributeValue}' for ${fimtype}" } else { $confirmationMessage = "Unset ${attributeName} for ${fimtype}" } # iterate objects and set attributes foreach ($object in $objects) { $objectID = GetAttributeValue $object "ObjectID" $displayName = GetAttributeValue $object "DisplayName" $objectType = GetAttributeValue $object "ObjectType" # ask for confirmation if specified if ($ask -and $(Read-Host "$confirmationMessage '$displayName'? (y/n)") -ne "y") { continue } Write-Host "Setting ${attributeName} for ${fimtype} '$displayName'" $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) $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.