src/Security/Remove-XrmUserRoles.ps1
|
<# .SYNOPSIS Remove security roles to user. .DESCRIPTION Unassign one or multiple roles to given user. .PARAMETER XrmClient Xrm connector initialized to target instance. Use latest one by default. (Dataverse ServiceClient) .PARAMETER UserReference Reference of the system user. (EntityReference) .PARAMETER Roles Roles unique identifier array to add. .OUTPUTS System.Void. .EXAMPLE $user = Get-XrmUser -Email "user@contoso.com"; Remove-XrmUserRoles -UserReference $user.Reference -Roles @($roleId1, $roleId2); .LINK https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/Remove-XrmUserRoles.md #> function Remove-XrmUserRoles { [CmdletBinding(SupportsShouldProcess)] [OutputType([System.Void])] param ( [Parameter(Mandatory = $false, ValueFromPipeline)] [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient] $XrmClient = $Global:XrmClient, [Parameter(Mandatory = $true)] [Microsoft.Xrm.Sdk.EntityReference] $UserReference, [Parameter(Mandatory = $true)] [Guid[]] $Roles = @() ) begin { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); } process { $roleRefs = $Roles | ForEach-Object { New-XrmEntityReference -LogicalName "role" -Id $_; }; $roleReferences = New-XrmEntityReferenceCollection -EntityReferences @($roleRefs); Split-XrmRecords -XrmClient $XrmClient -RecordReference $UserReference -RecordReferences $roleReferences -RelationShipName "systemuserroles_association"; } end { $StopWatch.Stop(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch; } } Export-ModuleMember -Function Remove-XrmUserRoles -Alias *; |