How to Use PowerShell to Add a Member to a Group in FIM
FIM ScriptBox Item
Summary
This script adds a member to a group
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 |
#-------------------------------------------------------------------------------------------------------------------- Set-Variable -Name GROUPNAME -Value "<My Group>" -Option Constant Set-Variable -Name USERNAME -Value "<My User>" -Option Constant Set-Variable -Name URI -Value "http://localhost:5725/resourcemanagementservice" -Option Constant #-------------------------------------------------------------------------------------------------------------------- Function GetSingleResource { Param($Filter) End { $exportResource = export-fimconfig -uri $URI ` –onlyBaseResources ` -customconfig ("$Filter")` -ErrorVariable Err ` -ErrorAction SilentlyContinue If($Err){Throw $Err} If($exportResource -eq $null) {Throw "Resource not found: $Filter"} If(@($exportResource).Count -ne 1) {Throw "More than one resource found: $Filter"} $exportResource } } #-------------------------------------------------------------------------------------------------------------------- If(@(Get-PSSnapin | Where-Object {$_.Name -eq "FIMAutomation"} ).count -eq 0) {Add-PSSnapin FIMAutomation} Clear-Host $ProgressPreference = "SilentlyContinue" #-------------------------------------------------------------------------------------------------------------------- $ProgressPreference = "Continue" Write-Progress -Activity "Retrieving group object" ` -Status "Please wait..." ` -CurrentOperation "Processing step 1 of 3" $ProgressPreference = "SilentlyContinue" $groupObject = GetSingleResource -Filter "/Group[DisplayName='$GROUPNAME']" $ProgressPreference = "Continue" Write-Progress -Activity "Retrieving user object" ` -Status "Please wait..." ` -CurrentOperation "Processing step 2 of 3" $ProgressPreference = "SilentlyContinue" $userObject = GetSingleResource -Filter "/Person[DisplayName='$USERNAME']" $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) $ProgressPreference = "Continue" Write-Progress -Activity "Adding user to group" ` -Status "Please wait..." ` -CurrentOperation "Processing step 3 of 3" $ProgressPreference = "SilentlyContinue" $importObject | Import-FIMConfig -Uri $URI -ErrorVariable Err -ErrorAction SilentlyContinue | Out-Null If($Err){Throw $Err} #-------------------------------------------------------------------------------------------------------------------- Write-Host "Command completed successfully" #-------------------------------------------------------------------------------------------------------------------- Trap { Write-Host $_.Exception.Message -foregroundcolor white -backgroundcolor darkred Write-Host $_.Exception.GetType().FullName -foregroundcolor white -backgroundcolor darkred Write-Host "`n" Exit 1 } #-------------------------------------------------------------------------------------------------------------------- |
Note
To provide feedback about this article, create a post on the FIM TechNet Forum.
For more FIM related Windows PowerShell scripts, see the FIM ScriptBox