functions/New-PpacTenantRuleBasedPolicy.ps1

<#
    .SYNOPSIS
        Create a tenant governance rule-based policy.
 
    .DESCRIPTION
        Creates a governance rule-based policy definition through the Power
        Platform API.
 
        The rule set objects are passed through to the service.
 
    .PARAMETER Name
        The display name of the new policy.
 
    .PARAMETER RuleSet
        One or more rule set objects with id, version, and inputs.
 
    .EXAMPLE
        PS C:\> New-PpacTenantRuleBasedPolicy -Name "Sharing baseline" -RuleSet @(@{ id = "Sharing"; version = "1.0"; inputs = @{} })
 
        Creates a policy with a Sharing rule set.
        It uses -Name to name the policy.
        It uses -RuleSet to supply the rule set definitions.
 
    .NOTES
        Author: d365bap.tools contributors
#>

function New-PpacTenantRuleBasedPolicy {
    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
    [OutputType('D365Bap.Tools.PpacTenantRuleBasedPolicy')]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string] $Name,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [object[]] $RuleSet
    )

    process {
        $body = [ordered]@{
            name     = $Name
            ruleSets = @($RuleSet)
        } | ConvertTo-Json -Depth 20

        $shouldAffected = $Name
        $shouldLabel = 'Create governance rule-based policy'

        if (-not $PSCmdlet.ShouldProcess($shouldAffected, $shouldLabel)) { return }

        $objSecureToken = Get-BapAccessToken -PowerPlatform
        $tokenValue = ConvertFrom-SecureString -AsPlainText `
            -SecureString $objSecureToken

        $hashHeaders = @{
            Authorization = "Bearer $tokenValue"
            Accept        = 'application/json'
        }

        $apiVersion = $script:BapApiVersions.PowerPlatform
        $uri = 'https://api.powerplatform.com/governance/ruleBasedPolicies?api-version={0}' `
            -f $apiVersion

        try {
            $created = Invoke-RestMethod -Method Post `
                -Uri $uri `
                -Headers $hashHeaders `
                -Body $body `
                -ContentType 'application/json' `
                -ErrorAction Stop
        }
        catch {
            $messageString = "Failed to create governance policy <c='em'>$Name</c>. $($_.Exception.Message)"
            Write-PSFMessage -Level Important `
                -Message $messageString `
                -Exception $_.Exception

            Stop-PSFFunction -Message "Stopping because the governance policy create request failed." `
                -Exception $_.Exception

            return
        }

        if (-not [string]::IsNullOrWhiteSpace($created.id)) {
            Get-PpacTenantRuleBasedPolicy -PolicyId $created.id
        }
    }
}