Azure PowerShell调用Search-AzGraph报错:参数'name'无效
问题:Azure Resource Graph 查询报错:参数"name"无效
我们有1000+ Azure订阅,部分订阅包含1000+资源。通过自动化账户运行PowerShell脚本,借助Az.ResourceGraph模块收集所有订阅的资源信息。为突破PowerShell默认的1000订阅、100资源限制编写了脚本,但运行时出现错误:Cannot process argument because the value of argument "name" is not valid,怀疑问题出在循环中。
原脚本代码
Import-Module Az.Accounts Import-Module Az.Automation Import-Module Az.Storage Import-Module Az.ResourceGraph $resourceGroup = "rg-xxxxx" $storageAccount = "stxxxxxxxxxx" $subscriptionid = "xxxx-xxxx-xxxx" $storageAccountContainer = "azure" $connectionName = "AzureRunAsConnection" # Run using Run As account try { # Get the connection "AzureRunAsConnection " $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName "Logging in to Azure..." $connectionResult = Connect-AzAccount -Tenant $servicePrincipalConnection.TenantID ` -ApplicationId $servicePrincipalConnection.ApplicationID ` -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint ` -ServicePrincipal "Logged in." } catch { if (!$servicePrincipalConnection) { $ErrorMessage = "Connection $connectionName not found." throw $ErrorMessage } else{ Write-Error -Message $_.Exception throw $_.Exception } } $date = get-date -format dd-MM-yyyy $query = Search-AzGraph -Query 'Resources' $subscriptions = Get-AzSubscription $SubscriptionIds = $subscriptions.Id $counter = [PSCustomObject] @{ Value = 0 } $batchSize = 1000 $response = @() $data = @() $subscriptionsBatch = $subscriptionIds | Group -Property { [math]::Floor($counter.Value++ / $batchSize) } foreach ($batch in $subscriptionsBatch){ $skipToken = $null; $queryResult = $null; do { if ($null -eq $skipToken){ $queryResult = Search-Azgraph -Query $query -first 1000 -subscription $batch.Group; $data = $data + $queryResult; } else{ $queryResult = Search-AzGraph -Query $query -SkipToken $skipToken -subscription $batch.Group; $data = $data + $queryResult; } $skipToken = $queryResult.SkipToken; } while ($null -ne $skipToken); } $data | Export-Csv "$Env:temp/Azure-temp-totalresources.csv" -notypeinformation Set-AzContext -SubscriptionId $subscriptionid Set-AzCurrentStorageAccount -StorageAccountName $storageAccount -ResourceGroupName $resourceGroup Remove-AzStorageBlob -Blob 'Azure-Azure-totalresources.csv' -Container $storageAccountContainer Set-AzStorageBlobContent -Container $storageAccountContainer -file "$Env:temp/Azure-temp-totalresources.csv" -Blob "Azure-totalresources.csv" -force
错误信息
Search-AzGraph: C:\Temp\z11pylt2.z2k\8a832791-6abe-4a38-b4b5-0c4eea1a215d.ps1:61 Line | 61 | … eryResult = Search-AzGraph -Query $query -SkipToken $skipToken -subsc … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Cannot process argument because the value of argument "name" is not | valid. Change the value of the "name" argument and run the operation | again.
解决方案
错误根源
脚本中$query = Search-AzGraph -Query 'Resources'这行错误地将查询结果对象赋值给了$query,但后续调用Search-AzGraph时,-Query参数需要的是查询字符串,而非查询结果。这种参数类型不匹配导致了"name"参数无效的错误。
修正步骤
- 修正$query的定义:将$query改为纯查询字符串,而非执行查询的结果
- 优化订阅批次验证:确保每个批次的订阅ID有效,避免空值
- 提升数据拼接性能:用
ArrayList替代数组拼接,减少内存开销(可选)
修正后的完整脚本
Import-Module Az.Accounts Import-Module Az.Automation Import-Module Az.Storage Import-Module Az.ResourceGraph $resourceGroup = "rg-xxxxx" $storageAccount = "stxxxxxxxxxx" $subscriptionid = "xxxx-xxxx-xxxx" $storageAccountContainer = "azure" $connectionName = "AzureRunAsConnection" # Run using Run As account try { $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName "Logging in to Azure..." $connectionResult = Connect-AzAccount -Tenant $servicePrincipalConnection.TenantID ` -ApplicationId $servicePrincipalConnection.ApplicationID ` -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint ` -ServicePrincipal "Logged in." } catch { if (!$servicePrincipalConnection) { $ErrorMessage = "Connection $connectionName not found." throw $ErrorMessage } else{ Write-Error -Message $_.Exception throw $_.Exception } } $date = Get-Date -Format dd-MM-yyyy # 修正:将$query定义为查询字符串,而非查询结果 $query = 'Resources' $subscriptions = Get-AzSubscription $SubscriptionIds = $subscriptions.Id $counter = [PSCustomObject] @{ Value = 0 } $batchSize = 1000 # 改用ArrayList提升拼接性能 $data = New-Object System.Collections.ArrayList $subscriptionsBatch = $SubscriptionIds | Group-Object -Property { [math]::Floor($counter.Value++ / $batchSize) } foreach ($batch in $subscriptionsBatch) { # 跳过空批次 if (-not $batch.Group) { continue } $skipToken = $null $queryResult = $null do { if ($null -eq $skipToken) { $queryResult = Search-AzGraph -Query $query -First 1000 -Subscription $batch.Group [void]$data.AddRange($queryResult) } else { $queryResult = Search-AzGraph -Query $query -SkipToken $skipToken -Subscription $batch.Group [void]$data.AddRange($queryResult) } $skipToken = $queryResult.SkipToken } while ($null -ne $skipToken) } $data | Export-Csv "$Env:temp/Azure-temp-totalresources.csv" -NoTypeInformation Set-AzContext -SubscriptionId $subscriptionid Set-AzCurrentStorageAccount -StorageAccountName $storageAccount -ResourceGroupName $resourceGroup # 先检查Blob是否存在再删除,避免报错 $existingBlob = Get-AzStorageBlob -Container $storageAccountContainer -Blob "Azure-totalresources.csv" -ErrorAction SilentlyContinue if ($existingBlob) { Remove-AzStorageBlob -Blob "Azure-totalresources.csv" -Container $storageAccountContainer } Set-AzStorageBlobContent -Container $storageAccountContainer -File "$Env:temp/Azure-temp-totalresources.csv" -Blob "Azure-totalresources.csv" -Force
内容的提问来源于stack exchange,提问作者A A
相关产品推荐
相关产品推荐

