Last active
January 10, 2024 14:19
-
-
Save techdecline/4f420f48b4b80127e1232d7c2a5d6460 to your computer and use it in GitHub Desktop.
Manage Azure Privileged Identity Management using Powershell and Azure Resource Manager. Docs available at https://techdecline.bearblog.dev/remove-azure-privileged-identity-management-assignments-using-powershell-and-azure-resource-manager/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Get-AzPimAssignment { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory = $false)] | |
| [string]$Scope | |
| ) | |
| # Checking Azure Context | |
| try { | |
| $ctx = Get-AzContext | |
| } | |
| catch { | |
| Write-Warning "Could not get Azure Context: $($error[0].Exception.Message)" | |
| return $false | |
| } | |
| if (!($Scope)) { | |
| Write-Verbose "No Scope has been identified, using current Subscription" | |
| $Scope = "/subscriptions/$($ctx.Subscription.Id)" | |
| } | |
| try { | |
| return (Get-AzResource -ResourceId "$Scope/providers/Microsoft.Authorization/roleEligibilitySchedules" -ApiVersion "2020-10-01" -ExpandProperties | ` | |
| Select-Object -Property @{Name = 'SubscriptionName'; Expression = { $ctx.Subscription.Name } }, | |
| @{Name = 'SubscriptionId'; Expression = { $ctx.Subscription.Id } }, | |
| @{Name = "RoleDefinitionName"; Expression = { (Get-AzRoleDefinition -Id ($_.Properties.RoleDefinitionId -split "/")[-1]).Name } }, | |
| @{Name = 'PrincipalName'; Expression = { Get-MgIdentityName -IdentityType $_.Properties.PrincipalType -IdentityId $_.Properties.PrincipalId } }, | |
| @{Name = 'PrincipalId'; Expression = { $_.Properties.PrincipalId } }, | |
| @{Name = 'RoleDefinitionId'; Expression = { $_.Properties.RoleDefinitionId } }, | |
| ResourceId) | |
| } | |
| catch { | |
| Write-Warning "Could not get Azure PIM Assignments: $($error[0].Exception.Message)" | |
| return $false | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Remove-AzPimAssignment { | |
| [CmdletBinding()] | |
| param ( | |
| [string]$PrincipalId, | |
| [string]$Scope, | |
| [string]$RoleDefinitionId, | |
| [string]$ApiVersion = "2020-10-01" | |
| ) | |
| process { | |
| $guid = (New-Guid).Guid | |
| $Properties = [ordered]@{RoleDefinitionId = $RoleDefinitionId; PrincipalId = $PrincipalId; RequestType = "AdminRemove" } | |
| $payload = [ordered]@{Properties = $properties } | |
| try { | |
| $restParam = @{ | |
| Method = "PUT" | |
| Payload = $($payload | ConvertTo-Json) | |
| } | |
| # Check if scope is subscription | |
| $regex = '^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$' | |
| if ([regex]::IsMatch($Scope, $regex)) { | |
| $restParam.Add("SubscriptionId", $Scope) | |
| $restParam.Add("ApiVersion", "2020-10-01") | |
| $restParam.Add("ResourceProviderName", "Microsoft.Authorization") | |
| $restParam.Add("ResourceType", "roleEligibilityScheduleRequests") | |
| $restParam.Add("Name", $guid) | |
| } | |
| else { | |
| $restParam.Add("Path", "$Scope/providers/Microsoft.Authorization/roleEligibilityScheduleRequests/$($guid)?api-version=$($ApiVersion)") | |
| Write-Host $restParam.Path | |
| } | |
| $restResult = Invoke-AzRestMethod @restParam | |
| if ($restResult.StatusCode -eq "201") { | |
| return $true | |
| } | |
| else { | |
| Write-Warning $restResult.Content | |
| return $false | |
| } | |
| } | |
| catch { | |
| throw "Could not remove PIM Assignment: $($Error[0].Exception.Message)" | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment