Tuesday, November 22, 2022

Power Platform Audit Scripts using PowerShell -All PowerApps

Get All Power Apps using PowerShell 

Clear-Host

Install-Module -Name Microsoft.PowerApps.Administration.PowerShell

Install-Module -Name Microsoft.PowerApps.PowerShell -AllowClobber 

#Import-Module (Join-Path (Split-Path $script:MyInvocation.MyCommand.Path) "Microsoft.PowerApps.Administration.PowerShell.psm1") -Force

Add-PowerAppsAccount

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition

Set-Location -Path $scriptPath

$AppRoleAssignmentsFilePath = ".\AppsDetails.csv"

# Add the header to the app roles csv file

$appRoleAssignmentsHeaders = "EnvironmentName," `

        + "AppName," `

        + "AppDisplayName," `

        + "SharedUsers," `

        + "SharedGroups," `

        + "CreatedTime," `

        + "LastModifiedTime," `

        + "AppOwnerObjectId," `

        + "AppOwnerDisplayName," `

        + "AppOwnerDisplayEmail," `

        + "AppOwnerUserPrincipalName," `

        + "AppConnections," `

        + "RoleType," `

        + "RolePrincipalType," `

        + "RolePrincipalObjectId," `

        + "RolePrincipalDisplayName," `

        + "RolePrincipalEmail," `

        + "RoleUserPrincipalName,";

Add-Content -Path $AppRoleAssignmentsFilePath -Value $appRoleAssignmentsHeaders

#populate the app files

$apps = Get-AdminPowerApp

foreach($app in $apps)

{

    #Get the details around who created the app

    $AppEnvironmentName = $app.EnvironmentName

    $Name = $app.AppName

    $DisplayName = $app.displayName -replace '[,]'

    $OwnerObjectId = $app.owner.id

    $OwnerDisplayName = $app.owner.displayName -replace '[,]'

    $OwnerDisplayEmail = $app.owner.email

    $CreatedTime = $app.CreatedTime

    $LastModifiedTime = $app.LastModifiedTime

    $userOrGroupObject = Get-UsersOrGroupsFromGraph -ObjectId $OwnerObjectId

    $OwnerUserPrincipalName = $userOrGroupObject.UserPrincipalName

    $SharedUsers = $app.Internal.properties.sharedUsersCount

    $SharedGroups = $app.Internal.properties.sharedGroupsCount

    #Get the list of connections for the app

    $connectionList = ""

    foreach($conRef in $app.Internal.properties.connectionReferences)

    {

        foreach($connection in $conRef)

        {

            foreach ($connId in ($connection | Get-Member -MemberType NoteProperty).Name) 

            {

                $connDetails = $($connection.$connId)


                $connDisplayName = $connDetails.displayName -replace '[,]'

                $connIconUri = $connDetails.iconUri

                $isOnPremiseConnection = $connDetails.isOnPremiseConnection

                $connId = $connDetails.id

                

                $connectionList += $connDisplayName + "; "

            }

        }        

    }  


    #Get all of the details for each user the app is shared with

    $principalList = ""

    $appRoles = ($app | Get-AdminPowerAppRoleAssignment)

    Write-Host $DisplayName ':' $appRoles.Count ' Shares'

    $appShares = $appRoles.Count

    foreach($appRole in $appRoles)

    {

        $RoleEnvironmentName = $appRole.EnvironmentName

        $RoleType = $appRole.RoleType

        $RolePrincipalType = $appRole.PrincipalType

        $RolePrincipalObjectId = $appRole.PrincipalObjectId

        $RolePrincipalDisplayName = $appRole.PrincipalDisplayName -replace '[,]'

        $RolePrincipalEmail = $appRole.PrincipalEmail

        $CreatedTime = $app.CreatedTime

        $LastModifiedTime = $app.LastModifiedTime

        If($appRole.PrincipalType -eq "Tenant")

        {

            $RolePrincipalDisplayName = "Tenant"

            $RoleUserPrincipalName = ""

        }

        If($appRole.PrincipalType -eq "User")

        {

            $userOrGroupObject = Get-UsersOrGroupsFromGraph -ObjectId $appRole.PrincipalObjectId 

            $RoleUserPrincipalName = $userOrGroupObject.UserPrincipalName  

          

        }

        # Write this permission record 

        $row = $AppEnvironmentName + "," `

                + $Name + "," `

                + $DisplayName + "," `

                + $SharedUsers + "," `

                + $SharedGroups + "," `

                + $CreatedTime + "," `

                + $LastModifiedTime + "," `

                + $OwnerObjectId + "," `

                + $OwnerDisplayName + "," `

                + $OwnerDisplayEmail + "," `

                + $OwnerUserPrincipalName + "," `

                + $connectionList + "," `

                + $RoleType + "," `

                + $RolePrincipalType + "," `

                + $RolePrincipalObjectId + "," `

                + $RolePrincipalDisplayName + "," `

                + $RolePrincipalEmail + "," `

                + $RoleUserPrincipalName;

    

        Add-Content -Path $AppRoleAssignmentsFilePath -Value $row 

    }

}     


No comments:

Post a Comment