We are implementing a Runbook which has to get all AzureAD Users - The code seems running successful and we are getting the right count of users (6453) - however, while getting the output in a JSON format, it throws the following error: the runbook job failed due to a job stream being larger than 1MB, the limit that is supported by an Azure Automation sandbox. See some common ways to resolve this at https://aka.ms/AAjobstreamlimit
We have tried multiple permutations & combination to get the right output but no luck...
Please note that the same code works fine in a local machine (Windows 10) using Powershell.
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
# Logging in to Azure AD with Service Principal
"Logging in to Azure AD..."
Connect-AzureAD -TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
try{
$AzureADUsers = Get-AzureADUser -All $true -Filter 'accountEnabled eq true' | select DisplayName,UserPrincipalName,Department
$AzureADUsers.Count;
try{
Write-Output ($AzureADUsers| ConvertTo-Json)
}
catch{
$AppException2 = $_.Exception
}
}
catch{
$AppException1 = $_.Exception
}