Welcome to Part 2 of my PowerShell Automation series!
In this part, we'll be creating a Microsoft Entra ID app registration that we'll eventually use with our PowerShell automation scripts to read and write data in Microsoft Intune through the Microsoft Graph API.
Before we get started, we'll also go over how I like to organize app registrations and why it's important to think about permissions before creating them.
The PowerShell Automation Series
- Part 1: Create a Group Managed Service Account (gMSA)
- Part 2: Create a Microsoft Entra ID App Registration
- Part 3: Set Up Certificate-Based Authentication
- Part 4: Schedule Your Script with Windows Task Scheduler
Getting Started
Before we create our app registration, I think it's worth taking a minute to consider how we're going to organize our automations and how many app registrations we'll actually need.
You probably don't want to create a separate app registration for every automation script you configure. Depending on how many scripts you're running, that could get out of hand pretty quickly and make things more difficult to manage.
At the same time, I wouldn't recommend creating one app registration that has permissions to everything in your environment.
Personally, I like to keep things simple by using as few app registrations as possible, while still keeping permissions separated where it makes sense.
For example, if we're building automations that interact with Intune, SharePoint, Entra ID, and other services, we might want to separate those into a few different app registrations.
Here's an example of how I organize some of mine:
- Intune-Automation: An app registration specifically for Intune automations. Permissions are assigned based on least privilege, meaning the app only receives the permissions required for its intended tasks.
- Intune-Sharepoint-PBI-Reporting: An app registration used to export information from Intune and upload it to SharePoint, where Power BI can access the data to create custom reports.
- Intune-Inventory-Sync: An app registration used to retrieve inventory information from Intune and send that data to a custom asset management service through its API.
I've separated these into different app registrations because, although they all interact with Intune, some of them also communicate with other systems.
Keeping those permissions separated makes it easier to manage what each automation can access.
Of course, this might look completely different in your environment. The important thing is to find a balance between keeping things manageable and not assigning more permissions than an automation actually needs.
Creating the App Registration
Now that we have an idea of how we want to organize things, let's go ahead and create our app registration.
Open the Microsoft Entra admin center and navigate to:
Entra ID > App registrations > New registration
Next, we'll need to provide a name for our app registration and select which account types it will support.
For the name, I recommend using something that makes it easy to identify what the application is being used for.
For most internal automation scenarios, you'll want to select Accounts in this organizational directory only (Single tenant).
This keeps the application associated with your organization's Entra ID tenant.
Once you've entered the name and selected the appropriate account type, click Register.
Our app registration has now been created!
You should be redirected to the application's Overview page, where you'll see information such as the Application (client) ID and Directory (tenant) ID.
We'll need these values later when we configure authentication and connect to Microsoft Graph using PowerShell.
For now, we can move on to configuring our API permissions.
Configuring API Permissions
Now that our app registration exists, we need to configure the permissions it will use to access Microsoft Graph.
These permissions determine what our automation scripts are allowed to do within the environment.
From your app registration, navigate to:
API permissions > Add a permission
When prompted to select an API, choose Microsoft Graph.
Microsoft Graph is what our PowerShell scripts will use to interact with Intune and other Microsoft 365 services.
Next, select Application permissions.
Since we're configuring unattended automation, we want our scripts to authenticate as the application rather than requiring a user to sign in each time they run.
Application permissions allow the script to access Microsoft Graph using the permissions assigned to the app registration, without relying on a signed-in user's delegated permissions.
Now, search for the permission required by your automation script.
For example, if your script needs to read Intune device configuration information, you might need:
DeviceManagementConfiguration.Read.All
This permission provides read access to Intune device configuration information through Microsoft Graph.
If you're using one of my automation scripts, I'll normally include the required Microsoft Graph permissions in the blog post or the script's GitHub repository.
One thing to keep in mind is that you should only assign the permissions your script actually needs.
For example, if a script only needs to read information from Intune, there's no reason to give it write permissions unless they're specifically required.
Once you've located the appropriate permission, select it and click Add permissions.
Granting Admin Consent
After adding the required permissions, you'll notice that they haven't necessarily been granted yet.
Before our application can use these permissions, an administrator with the appropriate privileges needs to approve them.
From the API permissions page, click Grant admin consent for [Your Organization].
You'll be prompted to confirm that you want to grant the selected permissions.
Review the permissions to make sure they're correct, and then click Yes to confirm.
Once admin consent has been granted successfully, you should see a green check mark next to the permission, indicating that consent has been granted for your organization.
At this point, our app registration has been created and the required Microsoft Graph permissions have been configured.
What's Next?
Our Microsoft Entra ID app registration is now configured, but we're not quite ready to start using it with our automation scripts.
The next step is to configure an authentication method so our scripts can securely connect to Microsoft Graph without requiring us to sign in manually.
There are a few different ways to handle this, but for this series, we'll be using certificate-based authentication.
Microsoft recommends using certificates instead of client secrets for application authentication. We'll also need to plan for certificate expiration and renewal so our automations don't unexpectedly stop working.
In Part 3 of this series, I'll walk through creating and configuring a certificate on our automation server, uploading the public certificate to our Entra ID app registration, and using it to authenticate with Microsoft Graph.
Once that's configured, we'll be another step closer to running our PowerShell automations without manual intervention.