Public/Get-PolicyInitiativeList.ps1

Function Get-PolicyInitiativeList{

    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $PolicyName
    )

    Begin{
        
        Write-Verbose "Intializing the function $($MyInvocation.MyCommand.Name)."
        $PolicyUsage=@()
        Try{
            Write-Verbose "Getting the details of the azure policy $PolicyName."
            $PolicyDefinition = Get-AzPolicyDefinition -Name $PolicyName -ErrorAction Stop
        }
        Catch{
            Write-Error "An error occured while getting the policy details. Error - $($_.Exception.Message)."
        }

        try {

            Write-Verbose "Getting the details of all the custom initiatives."
            $AllPolicySets = Get-AzPolicySetDefinition -Custom -ErrorAction Stop
            If($null -eq $AllPolicySets){
                Write-Warning "No custom initiatives found."
                Break
            }
        }
        catch {
            Write-Error "An error occured while getting the custom Initiatives. Error - $($_.Exception.Message)."
        }
        
        

    }
    Process{

        
        $PolicyUsage = $AllPolicySets | Where-Object {
            $_.PolicyDefinition.policyDefinitionId -ieq $PolicyDefinition.Id
        }

        if($PolicyUsage){
            Write-Verbose "The given policy $($PolicyName) is part of [$($PolicyUsage.count)] custom initiatives."
            Return $PolicyUsage 
        } 
        else{
            Write-Warning "The policy '$($PolicyName)' is not part of any policy sets (initiatives)."    
            Write-Verbose "The policy '$($PolicyName)' is not part of any policy sets (initiatives)."
                
        }
    }
    End{

        Write-Verbose "Execution completed $($MyInvocation.MyCommand.Name)."
    }
}