I want to use PowerApps PowerShell module to set role assignments, but I want an application to sign in using Add-PowerAppsAccount , and not a user. I saw that ApplicationId and ClientSecret can be used to connect, which I tried. There also exists an application user for the same application in my environment and system admin role is assigned to that app user.
I ran the following to connect:
Add-PowerAppsAccount -ApplicationId xxx -ClientSecret xxx -TenantID xxx -Audience my-environment-url
It shows no error while establishing the connection, and $global:currentSession returns loggedIn = true and it also returns some userId (which objectID of the application user) along with other details. Now when I run Set-AdminPowerAppRoleAssignment cmdlet and pass required parameters. AppName is the ID of the powerapp which I want to share, EnvironmentName is Id of the environment and PrincipalObjectId is ID of the user from Azure AD to whom I want to assign the CanEdit role. The command fails with an error like shown in the image below. Is there anything that I'm missing here? Or application user cannot perform this command? Basically I'd like to share access with other users using application context in PowerShell, i.e I do not want to store admin credentials or ask him to login. I want to use client credentials for connection, and that connection should be authorized to set role assignments.
Edit: The app for which I want role assignments has been imported as a part of solution by an application user. Hence, the app's owner is an application user. When I login to Add-PowerAppAccount interactively using admin credentials and run Get-AdminPowerApp to get list of all apps across all environments, I see the app which was imported by the application user. But when I use clientSecret and applicationId to login, and then run Get-AdminPowerApp or Get-PowerApp, I do not see it. I guess this particular case should work as the logged in user ID is same as the application user who is owner of that app, but it doesn't.
Edit: I tried Get-PowerApp -Verbose while logged in as application user. I get the the error as shown in the image below. I guess it tries to find the application user in Azure AD, but the application user is not there. 
