I’m not sure if this is the right forum for this since this is not exclusively Powershell but also AAD, but I thought I’d give it a go.
I have a runbook in Azure AD which is supposed to grab the active licensed users in the AD and export a number of CSVs to an Azure AD blob storage.
However, the runbook now consistently ends up in the suspended state with the exception:
The runbook job failed due to sandbox running out of memory. Each Azure Automation sandbox is allocated 400 MB of memory. The job was attempted 3 times before it was suspended. See some common ways to resolve this at https://aka.ms/AAMemoryLimit
The query which seems to be the problem is the following:
#Report for Active Users
Write-Output 'Getting all active non-student users'
$Export = Get-MgUser -All -Filter 'accountEnabled eq true' |
Where-Object '$_.UserPrincipalName -match "[NON STUDENT DOMAIN]"' |
Select-Object -Property DisplayName,UserPrincipalName,onPremisesSamAccountName,@{n="License";e={$_.assignedLicenses.skuId}},Department, CompanyName, accountEnabled,UserType
Write-Output $Export.Count
Write-Output 'Got all active non-student users'
Looking at the Output tab for the suspended jobs I find the following:
Environments
------------
{[AzureChinaCloud, AzureChinaCloud], [AzureCloud, AzureCloud], [AzureGermanCloud, AzureGermanCloud], [AzureUSGovernme...
Connected to Azure
Welcome To Microsoft Graph!
Getting all active non-student users
Environments
------------
{[AzureChinaCloud, AzureChinaCloud], [AzureCloud, AzureCloud], [AzureGermanCloud, AzureGermanCloud], [AzureUSGovernme...
Connected to Azure
Welcome To Microsoft Graph!
Getting all active non-student users
Environments
------------
{[AzureChinaCloud, AzureChinaCloud], [AzureCloud, AzureCloud], [AzureGermanCloud, AzureGermanCloud], [AzureUSGovernme...
Connected to Azure
Welcome To Microsoft Graph!
Getting all active non-student users
So it seems that the query somehow fails or times out and then retries three times before being put in the suspended state.
I have run the same query directly from a Powershell prompt and it’s not exactly fast but it works.
I have a similar runbook for inactive users which was previously integrated directly into this runbook and it works without a hitch. Naturally the number of inactive users is miniscule compared to the number of active users.
I’ve already modified the initial query a number of times from this (+12k users in two separate variables):
$Result = Get-MgUser -All -Filter 'accountEnabled eq true'
$Export = $Result | select DisplayName,UserPrincipalName,onPremisesSamAccountName,@{n="License";e={$_.assignedLicenses.skuId}},Department, CompanyName, accountEnabled,UserType
Through (+12k users in one variable):
$Export = Get-MgUser -All -Filter 'accountEnabled eq true' |
Select-Object DisplayName,UserPrincipalName,onPremisesSamAccountName,@{n="License";e={$_.assignedLicenses.skuId}},Department, CompanyName, accountEnabled,UserType
To the one listed above (+4k users in one variable):
$Export = Get-MgUser -All -Filter 'accountEnabled eq true' |
Where-Object '$_.UserPrincipalName -match "[NON STUDENT DOMAIN]"' |
Select-Object -Property DisplayName,UserPrincipalName,onPremisesSamAccountName,@{n="License";e={$_.assignedLicenses.skuId}},Department, CompanyName, accountEnabled,UserType
Nonetheless the script is still suspended before moving onto the CSV exports.
What baffles me is that it has run successfully previously, but for some reason it consistently fails now.

