Powershell Get-AzureADAuditSignInLogs 限制

Powershell Get-AzureADAuditSignInLogs Limits

我正在使用 AzureADPreview 模块中的 Get-AzureADAuditSignInLogs。 此 cmdlet 将其输出限制为 1000 行。 我想做一个解决方法并通过 User/Date 过滤器询问它。 它适用于我的情况,但后来我遇到了这样的 http429 错误。

Error occurred while executing GetAuditSignInLogs 
Code: UnknownError
Message: Too Many Requests
InnerError:
  RequestId: fc532b20-baea-4f62-8b5a-f4714f86f0a9
  DateTimeStamp: Wed, 10 Nov 2021 14:40:35 GMT
HttpStatusCode: 429
HttpStatusDescription: Too Many Requests
HttpResponseStatus: Completed

我看过有关如何查询在给定时间内您仍然可以发出的请求数量的技术,但这只是您启动 http-requests 时的全部内容。我想模块正在为我们屏蔽它。

有人知道如何使用此模块执行此操作的技术吗?

这是代码

Write-Output ("--- Get All AAD SignOns ---")
$filterDate = "{0:yyyy-MM-dd}" -f (get-date).AddDays(-30)
$TempFileName = $TempDir + $storageblobpath + $FileName + "AADSignOn.csv"
$usrCount     = 0
$logCount     = 0

ForEach ($user in $users) {
    $usrCount++
    $signons = Get-AzureADAuditSignInLogs -Filter "createdDateTime ge $filterDate and userPrincipalName eq '$user.UserPrincipalName'" |
                Select-Object Id,CreatedDateTime,UserId,AppId,AppDisplayName,IpAddress,ClientAppUsed,IsInteractive,TokenIssuerType,ProcessingTimeInMilliseconds,
                    @{name='DeviceId';expression={$_.DeviceDetail.DeviceId} },
                    @{name='DeviceDisplayName';expression={$_.DeviceDetail.DisplayName} },
                    @{name='DeviceOperatingSystem';expression={$_.DeviceDetail.OperatingSystem} },
                    @{name='DeviceBrowser';expression={$_.DeviceDetail.Browser} },
                    @{name='DeviceIsCompliant';expression={$_.DeviceDetail.IsCompliant} },
                    @{name='DeviceIsManaged';expression={$_.DeviceDetail.IsManaged} },
                    @{name='DeviceTrustType';expression={$_.DeviceDetail.TrustType} },
                    @{name='LocationCity';expression={$_.Location.City} },
                    @{name='LocationState';expression={$_.Location.State} },
                    @{name='LocationCountryOrRegion';expression={$_.Location.CountryOrRegion} }
    $signons | Export-Csv -Path $TempFileName -NoTypeInformation -Append

    $logCount += $signons.Count                   
    if ($usrCount % 200 -eq 0) {
        Write-Output ("Users {0}, {1} Logs so far" -f $usrCount, $logCount)
    }
}

Kr, 哈利

我在我的环境中使用 lasted AzureADPreview 模块进行了测试,得到的错误与您遇到的错误相同。

对于最新的模块,我发现此错误仅针对某些日志条目出现。例如,过滤特定用户的 UPN。

$signons = Get-AzureADAuditSignInLogs -Filter "createdDateTime ge $filterDate and userPrincipalName eq '$user.UserPrincipalName'"

尝试使用较旧的模块版本,如 2.0.2.85、2.0.2.89、2.0.2.105,但出现相同的错误。

注意:这些 PowerShell cmdlet 目前仅适用于 Azure AD 预览模块。请注意,不建议将预览模块用于生产。

请检查here

中提到的内容

我已经在 userDisplayName 上尝试了特定的用户过滤器,因为它有效。

 $signons = Get-AzureADAuditSignInLogs -Filter "createdDateTime gt 2021-10-18 and userDisplayName eq 'MOD Administrator'" |
               Select-Object Id,CreatedDateTime,UserId,AppId,AppDisplayName,IpAddress,ClientAppUsed,IsInteractive,TokenIssuerType,ProcessingTimeInMilliseconds
                  
Write-Output ("--- All Okay ---")
    $signons | Export-Csv -Path 'C:\AzureAD\AADSignOn.txt' -NoTypeInformation -Append 

输出--

但 Same 不是 运行 的 UPN 并且出现错误。

循环没有为所有用户完成。

许多用户报告了这个 github discussion as well but didn’t get the proper solution would suggest you to please reach out to MS support 团队遇到的同样问题。

我在循环访问我环境中的一组用户时遇到了同样的错误。现在我还没有对此进行广泛的测试,但至少在我的情况下,为每个请求添加延迟似乎可以绕过这个错误。

我只是在每个请求的末尾添加了 Start-Sleep -Milliseconds 500。当然,我的 user-base 没有那么大,所以这可能会为您的循环增加大量时间,但之后没有发生错误。

编辑:运行 一个 try/catch 语句,我发现只有在出错时才延迟,这比持续延迟更快。

    try {
        $UPN = $entry.Trim()
        Get-AzureADAuditSignInLogs -Filter "UserPrincipalName eq '$UPN'" | select UserPrincipalName,CreatedDateTime -Last 1 | Out-File -FilePath $login_file -Append
    }
    catch {
        $UPN = $entry.Trim()
        Start-Sleep -Seconds 20
        Get-AzureADAuditSignInLogs -Filter "UserPrincipalName eq '$UPN'" | select UserPrincipalName,CreatedDateTime -Last 1 | Out-File -FilePath $login_file -Append
    }