问题描述
我们正在实现一个必须获取所有AzureAD用户的Runbook-代码似乎运行成功,并且我们获得了正确的用户数(6453)-但是,当以JSON格式获取输出时,它将引发以下错误:由于作业流大于1MB(Azure自动化沙箱支持的限制),因此Runbook作业失败。在https://aka.ms/AAjobstreamlimit
中查看解决此问题的一些常用方法我们尝试了多种排列组合以获得正确的输出,但是没有运气...
请注意,相同的代码在使用Powershell的本地计算机(Windows 10)中可以正常工作。
$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
}
解决方法
我在运行本上测试了您的代码,效果很好(我的租户中只有251
个用户)。
此scenario中的原因仅仅是可能的原因,并非每个此错误都是由此问题引起的。
从下面的几行中,显然我们可以知道1 MB
的限制是在runbook作业输出流上的,try catch
块只是防止消息写入作业输出流中,租户中有6453
个用户,我认为即使没有错误写入输出流,它也会达到限制。
似乎沙箱流限制为1 MB,并且有老用户投票赞成增加沙箱流限制为1 MB。也许值得投票。 https://feedback.azure.com/forums/246290-automation/suggestions/15024291-change-behavior-when-sandbox-runs-out-of-memory-1
因此解决方案是按以下方式拆分查询:
Get-AzureADUser -All $true | where-Object { $_.DisplayName -cmatch '^[a-j,A-J]'} | select DisplayName,UserPrincipalName,Department
Get-AzureADUser -All $true | where-Object { $_.DisplayName -cmatch '^[k-t,K-T]'} | select DisplayName,Department
Get-AzureADUser -All $true | where-Object { $_.DisplayName -cmatch '^[u-z,U-Z]'} | select DisplayName,Department