How to Use PowerShell to Update Resources from a CSV File
FIM ScriptBox Item
Summary
This script will update resources in the FIM Portal using values in a CSV file. Currently this has only been tested on single-valued string attributes.
The CSV file must have the following:
- A header row,
- The first three columns must be as follows:
- ObjectType - the resource type name as used in the Portal,
- IDAttribute - the name of the attribute used to identity the target resource,
- IDValue - that value of the attribute used to identify the target resource.
- The remaining columns have the target Attribute Name from the FIM Portal as header.
For example:
ObjectType,IDAttribute,IDValue,Department,JobTitle
Person,Email,carol@myorg.com,IT,Engineer
Person,Email,bob@myorg.com,HR,Advisor
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
PARAM($CSVFile,$FIMServer="localhost",$Delimiter=";",$LogFile="ImportCSV-Attributes.log") function GetAttribute { PARAM($exportObject,[string] $name) END { $attribute = $exportObject.ResourceManagementObject.ResourceManagementAttributes | Where-Object {$_.AttributeName -eq $name} if ($attribute -ne $null -and $attribute.Value) { $attribute.Value } } } 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 WriteLog { PARAM($msg) END { Add-Content -Path $LogFile -Encoding ASCII -value $msg write-host $msg } } if (Test-Path $LogFile) {Remove-Item $LogFile} if(@(get-pssnapin | where-object {$_.Name -eq "FIMAutomation"} ).count -eq 0) {add-pssnapin FIMAutomation} $URI = "http://" + $FIMServer + ":5725/ResourceManagementService" # Parse CSV file. Note we're not using import-csv because we don't know what the column headers will be. $csv = Get-Content $CSVFile $header = $csv[0].split($Delimiter) $numcols = $header.length $rowcount = 1 while ($rowcount -lt $csv.length) { $rowvals = $csv[$rowcount].split($Delimiter) $filter = "/" + $rowvals[0] + "[" + $rowvals[1] + "='" + $rowvals[2] + "']" WriteLog -msg "Searching on $filter" $FIMObject = $null $FIMObject = export-fimconfig -uri $URI -customconfig ($filter) -ErrorVariable Err -ErrorAction SilentlyContinue if ($FIMObject.length -gt 1) {$FIMObject = $FIMObject[0]} $FIMObjectID = GetAttribute $FIMObject "ObjectID" $FIMObjectType = GetAttribute $FIMObject "ObjectType" if (($FIMObject -eq $null) -or ($FIMObjectID -eq $null)) { WriteLog -msg " Not found" } else { $bUpdateNeeded = $false # Create Import object that will update object in FIM $importObject = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject $importObject.ObjectType = $FIMObjectType $importObject.TargetObjectIdentifier = $FIMObjectID $importObject.SourceObjectIdentifier = $FIMObjectID $importObject.State = [Microsoft.ResourceManagement.Automation.ObjectModel.ImportState]::Put # Add the attributes $colcount = 3 while ($colcount -lt $rowvals.length) { $currentVal = $null $currentVal = $FIMObject.ResourceManagementObject.ResourceManagementAttributes | where-object {$_.AttributeName -eq $header[$colcount]} if ($rowvals[$colcount].length -eq 0) { $message = " No value to set for " + $header[$colcount] WriteLog -msg $message } elseif (($currentVal -ne $null) -and ($rowvals[$colcount] -eq $currentVal.Value)) { $message = " Value for " + $header[$colcount] + " is already correct" WriteLog -msg $message } else { $bUpdateNeeded = $true $message = " Setting " + $header[$colcount] + " to " + $rowvals[$colcount] WriteLog -msg $message SetAttribute -object $importObject -attributeName $header[$colcount] -attributeValue $rowvals[$colcount] } $colcount += 1 } # Import the changes into FIM if ($bUpdateNeeded) { WriteLog -msg " Importing changes" $importObject | Import-FIMConfig -uri $URI } } $rowcount += 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.