functions/roleManagement/roleManagementPolicies/Invoke-AzurePIMroleManagementPolicy.ps1

function Invoke-AzurePIMroleManagementPolicy {
    [CmdletBinding()]
    Param (
        [string[]] $SpecificResources,
        [System.Management.Automation.PSCmdlet]
        $Cmdlet = $PSCmdlet
    )
        
    
    begin
    {
        $resourceName = "roleManagementPolicies"
        if (!$script:desiredConfiguration[$resourceName]) {
            Stop-PSFFunction -String "AzurePIM.NoDefinitions" -StringValues "roleManagementPolicies"
            return
        }
        Test-AzureConnection
        $secureStringToken = (Get-AzAccessToken -AsSecureString -ResourceUrl $script:apiBaseUrl).Token
        $azureToken = [System.Net.NetworkCredential]::new('', $secureStringToken).Password
    }

    process {
        if (Test-PSFFunctionInterrupt) { return }
        $testResults = Test-AzurePIMroleManagementPolicy -Cmdlet $Cmdlet

        foreach ($result in $testResults) {
            Beautify-AzurePIMTestResult -TestResult $result -FunctionName $MyInvocation.MyCommand
            switch ($result.ActionType) {
                "Update" {
                    try {
                        $requestMethod = "PATCH"
                        $requestBody = @{
                            "properties" = @{
                                "rules" = $result.DesiredConfiguration.rules
                            }
                        }
                        $requestBody = $requestBody | ConvertTo-Json -Depth 8

                        Invoke-RestMethod -Method $requestMethod -Uri "$($script:apiBaseUrl)$($result.AzureResource.id)?api-version=2020-10-01-preview" -Headers @{"Authorization"="Bearer $($azureToken)"} -Body $requestBody -ContentType "application/json" | Out-Null
                        Write-PSFMessage -Level Host -String "AzurePIM.Invoke.ActionCompleted" -StringValues $result.Tenant, $result.ResourceType, $result.ResourceName, (Get-ActionColor -Action $result.ActionType), $result.ActionType
                    }
                    catch {
                        Write-PSFMessage -Level Error -String "AzurePIM.Invoke.ActionFailed" -StringValues $result.Tenant, $result.ResourceType, $result.ResourceName, $result.ActionType
                        throw $_
                    }
                }
                "NoActionRequired" {}
            }
        }
    }
}