By using app registration you can assign granular API permissions. It should start from
And to allow your automation to e.g. read users from Azure AD you need to grant: User.Read and User.Read.All
Then on Certificates & secretes (visible on above screenshot) you create new secret (copy it as it is shown only once) which is valid for 1 year.
On specific azure automation account go to variables now:
And create new variable – mark it as encrypted and in the value of it (string) paste copied secret. Now in a runbook created under this automation account you can use:
$cSec = Get-
To get value of this secret. Below code is an example how to authenticate. Client_ID within Body is the application ID of app registration
Function Get-GraphAccessToken{
$Body = @{
Grant_Type = "
Scope = "https
client_Id = "xxx"
Client_Secret = $cSec
}
$authResult = ConvertFrom-
$TokenRenewalTime = (Get-
# Put token in to a nice
if ($authResult.access_
$private:authHeader =
'Content-Type' =
'Authorization' =
'ExpiresIn' =
'TimeToRenew' =
}
# Return token
return $authHeader
}
else {
return "ErrorAPIToken:
}
}
$token = Get-GraphAccessToken
$UserInfo = 'https://graph.
Invoke-RestMethod -Method Get
Komentarze
Prześlij komentarz