public/new-rbacReport.ps1

Function new-rbacReport {
    [CmdletBinding(DefaultParameterSetName='console')]
    Param (
        [Parameter(parametersetname='console',Mandatory=$True, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$True, Position=0)]
        [Parameter(parametersetname='file',Mandatory=$True, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$True, Position=0)]
        [ArgumentCompleter( { @("Global","Org","Component") } )]
        [ValidateScript( { $_ -in @("Global","Org","Component") } )]
        [String]$Report,

        [Parameter(parametersetname='console',Mandatory=$false, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$True, Position=1)]
        [Parameter(parametersetname='file',Mandatory=$false, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$True, Position=1)]
        [ArgumentCompleter( { @("JSON","Table","AsRaw") } )]
        [ValidateScript( { $_ -in @("JSON","Table","AsRaw") } )]
        [String]$displayformat = "table",

        [Parameter(parametersetname='file',Mandatory=$true, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$True, Position=2)]
        [ArgumentCompleter( { @("JSON","CSV","CliXML") } )]
        [ValidateScript( { $_ -in @("JSON","CSV","CliXML") } )]
        [String]$format,

        [Parameter(parametersetname='file',Mandatory=$false, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$True, Position=3)]
        [String]$path = "$HOME\Documents\",

        [Parameter(parametersetname='file',Mandatory=$false, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$True)]
        [Switch]$Quiet
    )

    Begin {
        $TemplateMap = @{
            Global = $GlobalTemplate
            Org = $OrgTemplate
            Component = $ComponentTemplate
        }
    }

    Process {
        $Template = $TemplateMap[$Report]
        $RightsList = @{}
        # Create the default report line chart to be reused by each right
        $reportLine = [Ordered]@{"RightName" = ""}
        foreach ($role in $Template.DefaultRoles) {
            $reportLine[$role.nameSuffix] =""
            foreach ($aux in $role.AuxiliaryGroups) {
                #Add "rights" that don't appear in the rights list
                $RightsList[$aux] = @{
                    Type = "Auxiliary"
                    Name = $aux
                    Displayname = $aux
                    Description = "Well known group"
                }
            }
        }
        
        foreach ($right in $Template.DefaultRights ) {
            $RightsList[$right] = @{
                DisplayName = "...$($right.NameSuffix)"
                Type = "Right"
                Name = $right.NameSuffix
                Description = $Right.description
            }
        }
        
        $RightsChart =  foreach ($right in $RightsList.GetEnumerator()) {
            $reportLine["RightName"] = $right.Value.DisplayName
            $reportLine["Description"] = $right.Value.Description
            
            foreach ($role in $Template.DefaultRoles) {
                $HasRight = $false
                if ($right.Value.type -eq "Right") {
                    $HasRight = $role.rights -contains $right.Value.name
                } elseif ($right.Value.type -eq "Auxiliary" -and $role.AuxiliaryGroups){
                    $HasRight = $role.auxiliaryGroups -contains $right.Value.name
                }
                if ($HasRight) {
                    $reportLine[$role.nameSuffix] = "X"
                } else {
                    $reportLine[$role.nameSuffix] = ""
                }
            }
            
            [PSCustomObject]$reportLine
        }
        $RightsChart = $RightsChart | sort-object rightName

        if (-not $quiet) {
            switch ($displayformat) {
                "JSON" { $RightsChart | convertTo-JSON }
                "Table" { $RightsChart | format-table -autosize}
                "AsRaw" { $RightsChart }
            }
        }
        if ($format) {
            $dateStamp = Get-Date -format FileDateTime
            $filePath = "$path\RBAC-$Report-$dateStamp.$format"
            write-host ("Writing report: {0,-10} as {1,-5} to {2}" -f $report, $format, $filepath)
            try {
                switch ($format) {
                    "CliXML" {$RightsChart | Export-Clixml -path $filePath  }
                    "CSV" { $RightsChart | export-csv -path $filePath }
                    "JSON" { $RightsChart | convertTo-JSON | out-file $filePath }
                }
            } Catch {
                write-warning ("Failed to write report: {0,-10} as {1,-5} to {2}" -f $report, $format, $filepath)
                throw $_
            }

        }
    }
}