Azure powershell 图形资源管理器查询无法处理参数,因为参数“名称”的值无效

问题描述 投票:0回答:1

我们有 1000 多个 Azure 订阅,有些订阅有 1000 多个资源。我们正在从自动化帐户运行 powershell 脚本,以使用 graph explorer 模块收集有关每个订阅中所有资源的信息。有一个默认限制,powershell 只能从 1000 个订阅和 100 个资源中收集数据,为了克服这个限制,我们收集了以下脚本,但它给了我们一个错误。我相信问题出在 for loop 的某个地方。


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 线 | 61 | ... eryResult = Search-AzGraph -Query $query -SkipToken $skipToken -subsc ... | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~ |无法处理参数,因为参数“名称”的值不是 |有效的。更改“name”参数的值并运行操作 |再次。

azure powershell ksqldb graph-explorer
1个回答
0
投票

要解决问题,您可以尝试在代码中添加一些调试语句,以帮助确定错误发生的位置。例如,您可以添加一个语句来输出循环中 $batch 变量的值,该循环将订阅分组为批次。这可以帮助您确定是否正确填充了变量。

另一种方法是简化代码以隔离问题。例如,您可以尝试使用单个订阅和一小组资源运行 Search-AzGraph cmdlet,以查看问题是否仍然存在。如果错误没有发生,您可以逐渐向脚本添加更多订阅和资源,直到错误再次发生。这可以帮助您确定脚本的哪一部分导致了问题。

© www.soinside.com 2019 - 2024. All rights reserved.