Share via


How to Use PowerShell to Delete a User in the FIM Portal

FIM ScriptBox Item

Summary

The objective of this script is to delete a user in the 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
#----------------------------------------------------------------------------------------------------------
 set-variable -name URI -value "http://localhost:5725/resourcemanagementservice' " -option constant 
#----------------------------------------------------------------------------------------------------------
 function DeleteObject
 {
    PARAM($objectType, $objectId)
    END
    {
       $importObject = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject
       $importObject.ObjectType = $objectType
       $importObject.TargetObjectIdentifier = $objectId
       $importObject.SourceObjectIdentifier = $objectId
       $importObject.State = 2 
       $importObject | Import-FIMConfig -uri $URI
     } 
 }
#----------------------------------------------------------------------------------------------------------
 if(@(get-pssnapin | where-object {$_.Name -eq "FIMAutomation"} ).count -eq 0) {add-pssnapin FIMAutomation}
 clear-host

 if($args.count -ne 1) {throw "Missing name parameter"}
 $objectName = $args[0]

 if(0 -eq [String]::Compare($objectName,"administrator", $true))
 {throw "You can't delete administrator"}
 if(0 -eq [String]::Compare($objectName,"Built-in Synchronization Account", $true))
 {throw "You can't delete Built-in Synchronization Account"}

 $exportObject = export-fimconfig -uri $URI `
                                  â€“onlyBaseResources `
                                  -customconfig "/Person[DisplayName='$objectName']"

 if($exportObject -eq $null) {throw "L:Object not found"}
 $objectId = (($exportObject.ResourceManagementObject.ObjectIdentifier).split(":"))[2]

 DeleteObject -objectType "Person" `
              -objectId $objectId

 write-host "`nObject Deleted successfully`n"
#----------------------------------------------------------------------------------------------------------
 trap 
 { 
    $exMessage = $_.Exception.Message
    if($exMessage.StartsWith("L:"))
    {write-host "`n" $exMessage.substring(2) "`n" -foregroundcolor white -backgroundcolor darkblue}
    else {write-host "`nError: " $exMessage "`n" -foregroundcolor white -backgroundcolor darkred}
    Exit
 }
#----------------------------------------------------------------------------------------------------------

 

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.

 


See Also