Microsoft Graph PowerShell SDK without Admin Rights

 If you have used or are still using Azure AD PowerShell for interacting with Azure AD, you should probably already have taken note that Azure AD PowerShell will be deprecated on June 30, 2023.

The alternative - Microsoft Graph PowerShell SDK, uses the power of the new Microsoft Graph and all underlying Graph APIs - allowing you to do much more than just Azure AD related tasks, supports PowerShell 7 making it work cross platform, use modern authentication and a whole bunch of other things that you expect from a modern solution. See link to learn more:

https://learn.microsoft.com/en-us/powershell/microsoftgraph/overview?view=graph-powershell-1.0

The SDK supports two types of authentication: delegated access and app-only access.

If you follow the tutorial in the documentation, you will be using the delegated permission model. This is effective for ensuring least privilege.

However, there is one important thing when dealing with delegated permissions which is mentioned in another part of the documentation: You need to run "Microsoft Graph PowerShell using a user with privileges to create applications in the tenant and the appropriate permissions."

Now this might be a deterrent because a lot of times we maybe trying to automate stuff having the right permissions assigned to our user accounts but nor necessarily creating apps or modifying them - principle of least privilege (keeps coming back).

In these scenarios, if you try to run a command as follows:


You will run into error as below:




This is because you need rights on the following Enterprise Application to be able to register the delegated permissions for the SDK


While this might be workable, most administrators might not want to do this. 

So, what next. I ran into a similar situation where getting into a lengthy discussion with Tenant Admins was not an option and I had to still automate my stuff.

The solution is actually quite easy. A hidden feature in the MS Graph PS SDK authentication commandlets is using an already obtained access token. So, simply change your previous script to the following:


And just like that, you're in. No admin help needed.


Now you can freely use your existing permissions to build scripts using the power of Microsoft Graph in PowerShell.



Comments

Popular posts from this blog

Automate Import of Functions/WebAPI in Azure API Management as backend and using OpenAPI definition and Terraform

Managing built-in cache in Azure API Management