Azure Runbook - [AzureAD] Get-AzureADUser -All

Mourtaza Moise Fazlehoussen 46 Reputation points MVP
2020-11-03T12:10:03.42+00:00

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  
}  

37192-image.png

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,256 questions
0 comments No comments
{count} vote

Accepted answer
  1. Andreas Baumgarten 111.1K Reputation points MVP
    2020-11-03T18:45:36.147+00:00

    JSON files compared to CSV files are about double in size.
    Maybe it's possible to generate a CSV format instead of a JSON format?

    The other option I see is to split the result. For instance in the first part all user with username starts with A to N and a second batch usernames with O -Z.

    There is an old user vote for increasing the sandbox stream limit of 1 MB. Maybe it's worth to vote.
    https://feedback.azure.com/forums/246290-automation/suggestions/15024291-change-behavior-when-sandbox-runs-out-of-memory-1


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.