我创建了一个 Azure 函数,尝试获取所有 AAD 组成员。该计时器作业需要每天运行一次。当我进行测试运行时,我在日志记录中看到它在 nextLink 分页中迭代了几次。但随后就卡住了,没有出现错误。我已经将超时时间增加到 10 分钟,但我仍然遇到同样的问题。
当我在我的机器上本地运行此脚本时,它工作正常。我预计它应该迭代 127 次并获得 12757 个项目。
using namespace System.Net
# Input bindings are passed in via param block.
param($Timer)
"START"
$fileName = "Azure - ADGroupMembers.csv"
Write-Output "Connect to AAD"
Connect-AzAccount -identity
try {
#region auth
Write-Output "Get items from MS Graph"
$token = (Get-AzAccessToken -ResourceUrl "https://graph.microsoft.com/").Token
$authHeader = @{Authorization = "Bearer $token"}
# #endregion
# #region main proces
$allPages = @()
$items = (Invoke-RestMethod -Method 'Get' -Uri 'https://graph.microsoft.com/v1.0/groups?$top=999&$expand=members' -Headers $authHeader -ContentType 'Application/Json')
$allPages += $items.value
$index = 0
if ($items.'@odata.nextLink') {
do {
$index++
"Index counter: $index"
$token = (Get-AzAccessToken -ResourceUrl "https://graph.microsoft.com/").Token
$authHeader = @{Authorization = "Bearer $token"}
$items = (Invoke-RestMethod -Method 'Get' -Uri $items.'@odata.nextLink' -Headers $authHeader -ContentType 'Application/Json')
$allPages += $items.value
} until (
!$items.'@odata.nextLink'
)
}
$items = $allPages
"Count items: " + $items.Count
$filePath = "D:\home\data\$($fileName)"
Write-Host "Convert to csv to path '$($filePath)'" -ForegroundColor green
$items | Export-Csv -NoTypeInformation -Path $filePath
#endregion
}
catch {
write-output $_.Exception.Message
}
"FINISH"
尝试使用高级或专用计划为函数中的专用磁盘存储创建 Azure 函数应用程序,因为消费应用程序只会在函数运行时创建函数实例。
基于功能应用程序消耗的计划也有默认时间为 5 分钟。请参阅此MS Document以获取相同的信息:-
我对您的代码进行了一些更改,并利用客户端凭据流程和一些 try catch 错误块来获取日志记录并且代码成功,请参阅下文:-
run.ps1:-
# Input bindings are passed in via param block.
param($Timer)
Write-Output "START"
# Function to fetch AAD group members and handle pagination
function Get-AADGroupMembers {
param (
[string]$Uri,
[hashtable]$Headers
)
$allPages = @()
do {
$response = Invoke-RestMethod -Method 'Get' -Uri $Uri -Headers $Headers -ContentType 'Application/Json'
$allPages += $response.value
if ($response.'@odata.nextLink') {
# Add a delay to handle rate limiting (adjust the duration as needed)
Start-Sleep -Seconds 2
$Uri = $response.'@odata.nextLink'
}
else {
$Uri = $null
}
} while ($Uri)
return $allPages
}
try {
# Connect to Azure AD
Write-Output "Connect to AAD"
$SecurePassword = ConvertTo-SecureString -String "xxxxxxxa~N63RzmH34duL" -AsPlainText -Force
$TenantId = '8xxxxxx9ed-afxxxxx5'
$ApplicationId = 'cxxx45b5-b8xxxx1435cb'
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ApplicationId, $SecurePassword
Connect-AzAccount -ServicePrincipal -TenantId $TenantId -Credential $Credential
# Get access token
$token = (Get-AzAccessToken -ResourceUrl "https://graph.microsoft.com/").Token
$authHeader = @{ Authorization = "Bearer $token" }
# Get AAD group members
$graphUri = 'https://graph.microsoft.com/v1.0/groups?$top=999&$expand=members'
$groupMembers = Get-AADGroupMembers -Uri $graphUri -Headers $authHeader
Write-Output "Count of items: $($groupMembers.Count)"
$filePath = "C:\home\data\Azure-ADGroupMembers.csv"
Write-Output "Convert to CSV to path '$($filePath)'" -ForegroundColor green
$groupMembers | Export-Csv -NoTypeInformation -Path $filePath
Write-Output "FINISH"
}
catch {
# Log detailed error information
Write-Error "Error: $_"
Write-Output $_.Exception.Message
Write-Output $_.Exception.StackTrace
}
此外,我将文件路径更改为
C:\
而不是 D:\
,因为那是我的 Azure 函数文件的存储位置,请参阅下面的 Kudu 图像:-
输出:-