Share via


FIM : PowerShell to add user to multiple Groups from CSV file

  • Before You Begin : 

The Import-Csv cmdlet makes it easy to import data as a comma-separated values (CSV) file; all you need to do is call Import-Csv followed by the path to the CSV file. The Header of CSVe will have your DisplayName of user and GroupName and save it as CSV( comma delimited)

 DisplayName GroupName 
 Anirban Singha  FIMTechnet
 Ansi Desouza  FIMAdmin
  • Change the path of CSV file in below Script.           
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
#------------------------------------------------------------------------------------
# Adding user to Multiple Group in FIM portal
#--------------------------------------------------------------------------------------
If(@(Get-PSSnapin | Where-Object {$_.Name -eq "FIMAutomation"} ).count -eq 0) {Add-PSSnapin FIMAutomation}
Import-Csv C:\Users\Anirban\Desktop\Data.csv | %{
$a=$_.GroupName
$b=$_.DisplayName
Function GetUser($b)
{
Export-FIMConfig -Uri "http://localhost:5725" -onlyBaseResources `
-customConfig "/Person[DisplayName = '$b']"
}
Function GetGroup
{
param($Filter)
End
{
Export-FIMConfig -Uri "http://localhost:5725" -onlyBaseResources `
-customConfig "$Filter"
}
}

Write-Host "Query for Group $a"

$groupObject = GetGroup -Filter "/Group[DisplayName = '$a']"

Write-Host "Query for User $b"
$userObject = GetUser $b
 
$importChange = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange
$importChange.Operation = 0
$importChange.AttributeName = "ExplicitMember"
$importChange.AttributeValue = $userObject.ResourceManagementObject.ObjectIdentifier
$importChange.FullyResolved = 0
$importChange.Locale = "Invariant"
 
$importObject = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject
$importObject.ObjectType = "Group"
$importObject.TargetObjectIdentifier = $groupObject.ResourceManagementObject.ObjectIdentifier
$importObject.SourceObjectIdentifier = $groupObject.ResourceManagementObject.ObjectIdentifier
$importObject.State = 1 
$ImportObject.Changes = (,$ImportChange)
 
$importObject | Import-FIMConfig -Uri "http://localhost:5725"

}

#----------------------------------------------------------------