azure Powershell(7.2.7)前向对象并行-多线程问题

fnvucqvd  于 2023-03-03  发布在  Shell
关注(0)|答案(1)|浏览(126)

我正在尝试从数千个订阅收集Azure SQL Server/Database(PaaS)信息。以下是我的脚本。

$SQLserver = [System.Collections.Concurrent.ConcurrentBag[psobject]]::new()
$subscriptions | Where-Object {$_.state -ne "Disabled" -and $_.State -ne "Warned"} | Foreach-Object -Parallel {
    $sub = $_
    $localSqlserver = $using:SQLserver
    $localSqlserver.Add($(
        Get-AzSqlServer -DefaultProfile (Set-AzContext -SubscriptionId $PSItem) | ForEach-Object -process {
        $sqlServerObj = $_
        $sqlServerObj | ForEach-Object -process {
            Get-AzSqlDatabase -ResourceGroupName $sqlServerObj.ResourceGroupName -ServerName $sqlServerObj.ServerName | ForEach-Object -process {
            $DBObj = $_
            $DBObj | Select-Object @{N='SubscriptionName'; E = {$sub.Name}},
            @{N='ResourceGroupName';E={$sqlServerObj.ResourceGroupName}},
            @{N='SQLServerName';E={$sqlServerObj.ServerName}},
            @{N='FQDN';E={$sqlServerObj.FullyQualifiedDomainName}},
            @{N='sqlServerResourceId'; E = {$sqlServerObj.ResourceId}},
            @{N='Location';E={$sqlServerObj.Location}},
            @{N='SqlAdministratorLogin';E={$sqlServerObj.SqlAdministratorLogin}},
            @{N='ServerVersion';E={$sqlServerObj.ServerVersion}},
            @{N='Identity';E={$sqlServerObj.Identity}},
            @{N='MinimalTlsVersion';E={$sqlServerObj.MinimalTlsVersion}},
            @{N='PublicNetworkAccess';E={$sqlServerObj.PublicNetworkAccess}},
            @{N='RestrictOutboundNetworkAccess';E={$sqlServerObj.RestrictOutboundNetworkAccess}},
            @{N='KeyId';E={$sqlServerObj.KeyId}},
            @{N='DBName';E={$DBObj.DatabaseName}},
            @{N='DBId';E={$DBObj.DatabaseId}},
            @{N='Edition';E={$DBObj.Edition}},
            @{N='CollationName';E={$DBObj.CollationName}},
            @{N='MaxSizeBytes';E={$DBObj.MaxSizeBytes}},
            @{N='Status';E={$DBObj.Status}},
            @{N='CurrentSize';E={$DBObj.CurrentServiceObjectiveName}},
            @{N='ElasticPoolName';E={$DBObj.ElasticPoolName}},
            @{N='ReadScale';E={$DBObj.ReadScale}},
            @{N='ZoneRedundant';E={$DBObj.ZoneRedundant}},
            @{N='Capacity';E={$DBObj.Capacity}},
            @{N='Family';E={$DBObj.Family}},
            @{N='SkuName';E={$DBObj.SkuName}},
            @{N='LicenseType';E={$DBObj.LicenseType}},
            @{N='CurrentBackupStorageRedundancy';E={$DBObj.CurrentBackupStorageRedundancy}},
            @{N='MaintenanceConfigurationId';E={$DBObj.MaintenanceConfigurationId}},
            @{N='EnableLedger';E={$DBObj.EnableLedger}}
            }}
        }
    ))
} -UseNewRunspace

虽然$SQLserver有对象集合,我仍然收到下面的错误消息,但我真的不明白真正的问题是什么,由于这个错误,它仍然不包含所有的信息。但对单个订阅运行脚本(从第6行开始)确实有效。
x一个一个一个一个x一个一个二个x
有人觉得剧本的逻辑有什么问题吗?

cnjp1d6j

cnjp1d6j1#

错误**"找不到资源组"**清楚地表明指定的资源组中不存在数据库/服务器,或者给定的资源组不正确。
脚本的另一个问题是,它在并行循环内使用using scope modifier访问$SQLserver集合。这可能会导致并行线程之间的同步问题。$SQLserver集合应作为参数传递给ForEach -Object cmdlet。

我修改了您的脚本,如下所示,它按预期运行。

  • 我尝试只检索几个字段以验证它是否正常工作。您可以更改它以满足您的要求。*
$SQLserver = [System.Collections.Concurrent.ConcurrentBag[psobject]]::new()
ForEach-Object  {
    $sub = $_
    $sqlServers = Get-AzSqlServer -DefaultProfile (Set-AzContext -SubscriptionId $subscription)
    $sqlServers | ForEach-Object {
        $sqlServerObj = $_
        $databases = Get-AzSqlDatabase -ResourceGroupName $sqlServerObj.ResourceGroupName -ServerName $sqlServerObj.ServerName
        $databases | ForEach-Object {
            $DBObj = $_
            $SQLserver.Add([pscustomobject]@{
                SubscriptionName = $sub.Name
                ResourceGroupName = $sqlServerObj.ResourceGroupName
                SQLServerName = $sqlServerObj.ServerName
                sqlServerResourceId = $sqlServerObj.ResourceId
                Location = $sqlServerObj.Location
                SqlAdministratorLogin = $sqlServerObj.SqlAdministratorLogin
                ServerVersion = $sqlServerObj.ServerVersion
                Identity = $sqlServerObj.Identity
                MinimalTlsVersion = $sqlServerObj.MinimalTlsVersion
                PublicNetworkAccess = $sqlServerObj.PublicNetworkAccess
              })
        }
    }
}

输出:

相关问题