gpt4 book ai didi

azure - Powershell (7.2.7) Foreach-Object -Parallel - 多线程问题

转载 作者:行者123 更新时间:2023-12-03 03:26:36 27 4
gpt4 key购买 nike

我正在尝试从数千个订阅中收集 Azure SQL Server/数据库 (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 行开始)确实有效。

Get-AzSqlDatabase:
Line |
8 | Get-AzSqlDatabase -ResourceGroupName $sqlServerObj.Resour …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Resource group '911f5f6be3ab-rg' could not be found.
PSMessageDetails      :
Exception : Microsoft.Rest.Azure.CloudException: Resource group '911f5f6be3ab-rg' could not be found.
at Microsoft.Azure.Management.Sql.DatabasesOperations.ListByServerWithHttpMessagesAsync(String resourceGroupName, String serverName, String skipToken, Dictionary`2 customHeaders, CancellationToken cancellationToken)
at Microsoft.Azure.Management.Sql.DatabasesOperationsExtensions.ListByServerAsync(IDatabasesOperations operations, String resourceGroupName, String serverName, String skipToken, CancellationToken cancellationToken)
at Microsoft.Azure.Commands.Sql.Database.Services.AzureSqlDatabaseCommunicator.List(String resourceGroupName, String serverName)
at Microsoft.Azure.Commands.Sql.Database.Services.AzureSqlDatabaseAdapter.ListDatabases(String resourceGroupName, String serverName)
at Microsoft.Azure.Commands.Sql.Database.Cmdlet.GetAzureSqlDatabase.GetEntity()
at Microsoft.Azure.Commands.Sql.Common.AzureSqlCmdletBase`2.ExecuteCmdlet()
at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
TargetObject :
CategoryInfo : CloseError: (:) [Get-AzSqlDatabase], CloudException
FullyQualifiedErrorId : Microsoft.Azure.Commands.Sql.Database.Cmdlet.GetAzureSqlDatabase
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, <No file>: line 8
at <ScriptBlock>, <No file>: line 7
at <ScriptBlock>, <No file>: line 5
PipelineIterationInfo : {0, 1, 0}

有人发现脚本逻辑有问题吗?

最佳答案

错误“找不到资源组”清楚地表明指定的资源组中不存在数据库/服务器,或者给定的资源组不正确。

该脚本的另一个问题是它在并行循环内使用 using 作用域修饰符 来访问 $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
})
}
}
}

输出:

enter image description here

关于azure - Powershell (7.2.7) Foreach-Object -Parallel - 多线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75514750/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com