It is now (has been for a while) possible to modify Azure AD via the Azure Automation. The example below uses the Run As Automation Account to first Connect to Azure AD and then run the appropriate commands. You can also create a dedicated Run As account if you want, as well as use a username and password (less secure).
Before you write your code make sure that you:
- Add the “AzureAD” module to the Automation Account
- Give the Azure Automation Run As account the appropriate permission as show at the end of this article
Automation Code example (list all the groups in AD):
$ErrorActionPreference = "Stop"
$connectionName = "AzureRunAsConnection"
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure Active Directory"
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
$ErrorMessage = "Connection $connectionName not found."
Write-Error -Message $_.Exception
Give the Azure Automation Run As account the appropriate permissions:
- Go to Azure Active Directory -> App registrations -> The Run Ass Account.
- Then go to the API access as show:
- Give the appropriate access, example below:
Don’t forget to click grant permissions!