I had a problem with runbook showing error: Invalid JWT access token
I found this https://github.com/
And I removed newest Microsoft.Graph.Authentication module, then imported older one: 2.25.0
Now there is no issue to connect to MS Graph from runbook
The roles are not necessarily needed when using system assigned MIs. This is because the authentication in this case is done by the app itself, no user is involved in the process, so only Application type permissions need to be assigned to the MI, not roles.
In general, when you perform API calls as an app you only need Application type permissions. Very rarely you also need roles. In case you perform the Graph API calls the situation is a bit different, the permissions are formed by the intersection of the Delegated permissions and the user's roles.
I will provide you some explanations about what the Mis are and how these work, along with some steps you can follow for assigning permissions to a system assigned MI for performing Graph API calls in a Runbook.
General aspects
You can use managed identities instead of secrets or certificates to request an access token from Azure AD when you authenticate as an application. This is the best approach that you can use because managed identities eliminate the need for developers to manage these credentials. Basically, you don't have to use a secret or a certificate anymore to authenticate to Azure AD and get an access token. You can check this article for more details: https://docs.microsoft.com/en-
- Create your Automation Account and enable the system-assigned MI:
You can assign it a System-Assigned Identity via the Identity option under Settings. Switch Status from Off to On
After saving, you will get an Object ID. Copy this, as you will need it for the next step.
- Grant permissions to the System-assigned Managed Identity. You can use PowerShell, Postman, Graph Explorer or your own app for assigning the permissions to the app:
Content-Type: application/json {
"principalId": "ObjectID from the previous step",
"resourceId": "ResourceID of Graph API *1",
"appRoleId": "ID of the permission you want to assign *2"
}
*1 - can be found like this and in my case it's the one circled with red:
*2 - can be found here and should be for Application type, not Delegated
These permissions depend on the API call you want to perform. For example, if you want to use your Runbook to modify user's properties, then you would need to grant to your app some permissions like User.ReadWrite.All. Again, this is just an example, the permissions you will grant to your system assigned MI depend on what Graph API calls you will need to use in the automation.
Each Graph API call has a corresponding article in the documentation and the permissions required for that API call are mentioned in a table like this, at the beginning of the articles:
Keep in mind that for system assigned MIs you have to use Application type permissions.
PowerShell code:
# Ensures you do not inherit
Disable-AzContextAutosave -
# Connect to Azure with
$AzureContext = (Connect-
# Get the access token for
$GraphToken = Get-
$Identity = $GraphToken.token
$graphTokenConverted=($
Connect-MgGraph -AccessToken $
# Get the access token for
$AadToken = Get-AzAccessToken
# Connect to Azure AD using
Import-Module -Name AzureAD.
#get-az* cmdlets are working
Connect-AzureAD -AccountId $Az
#$users = Get-AzADGroup -
$users = Get-AzureADGroup -
$devices = $users | % {Get-
$list = $devices | % {Get-
#last logon not older than 6
$t = get-date
#as result DeviceId is
#$final = $list | % {if (($_.
$final = $list | % {if (($_.
$final = $final | Select-
# Get the device details from
$IsIntuneManaged = $final | %
$IsIntuneManagedTrue = $
$finalObjectId = $
$AAD_PROD_Intune_IIT_Devices =
$currentMembers = Get-
$newMembers = Compare-Object -
Write-Output "SHOWING NEW
if (($newMembers -ne $null) -
$newMembers | % {Add-
}
$sdtoolbox = "zzz"
$toRemove1 = Compare-Object -
#except sdtoolbox service
$toRemove2 = Compare-Object -
Write-Output "SHOWING OBJECTS
$toRemove2
if (($toRemove2) -ne $null -
$toRemove2 | % {Remove-
}
Write-Output "End of code"
Komentarze
Prześlij komentarz