如何通过foreach循环和变量堆栈从azure订阅中检索所有数据库

时间:2017-12-04 13:55:06

标签: powershell azure powershell-v5.0

我有以下工作可以很好地提取没有RESOURCE GROUP的所有数据库的列表并存储到名为$ resources的变量

$resources = Get-AzureRmResource | ?{ $_.ResourceGroupName -eq "resourceGROUPnameHERE" -and $_.kind -eq "v12.0,user"  } | select resourcename,resourceid

和输出如下所示(每个resourcename nad resourceID两列)

ResourceName              ResourceId
------------              ----------
georgidbserver1/georgiDB1 /subscriptions/aaaaaa-bbbb-486a-96cf-6c68361d8000/resourceGroups/georgigroup/providers/Microsoft.Sql/servers/georgidbserver1/databases/georgiDB1
georgidbserver1/georgiDB2 /subscriptions/aaaaaa-bbbb-486a-96cf-6c68361d8000/resourceGroups/georgigroup/providers/Microsoft.Sql/servers/georgidbserver1/databases/georgiDB2

我希望实现的目标是获得相同的输出并以相同的方式将其存储到变量中,但不是针对单个追索组,而是我希望它从我的订阅中检查所有资源组并从中拉出所有数据库他们在名单中。

我创建了以下代码,准确输出了PowerShell窗口中我需要的内容

CODE是

$rg = Get-AzureRmResourceGroup | ?{ $_.ProvisioningState -eq "succeeded" } | select -expandproperty resourcegroupname
foreach ($resourcegroup in $rg) {Get-AzureRmResource | ?{ $_.ResourceGroupName -eq $resourcegroup -and $_.kind -eq "v12.0,user"  } | select resourcename,resourceid}

输出看起来像这样(我确切地想要它)

PS C:\ Users> \ small.ps1

ResourceName              ResourceId
------------              ----------
georgidbserver2/georgiDB3 /subscriptions/17e1cc9e-4eb3-486a-96cf-6c68361d8000/resourceGroups/dbgroup/providers/Microsoft.Sql/servers/georgidbserver2/databases/georgiDB3
georgidbserver2/georgiDB4 /subscriptions/17e1cc9e-4eb3-486a-96cf-6c68361d8000/resourceGroups/dbgroup/providers/Microsoft.Sql/servers/georgidbserver2/databases/georgiDB4
georgidbserver1/georgiDB1 /subscriptions/17e1cc9e-4eb3-486a-96cf-6c68361d8000/resourceGroups/georgigroup/providers/Microsoft.Sql/servers/georgidbserver1/databases/georgiDB1
georgidbserver1/georgiDB2 /subscriptions/17e1cc9e-4eb3-486a-96cf-6c68361d8000/resourceGroups/georgigroup/providers/Microsoft.Sql/servers/georgidbserver1/databases/georgiDB2

我的问题是如何将第二个代码的输出存储到一个变量中,就像它在$ resources的第一个例子中一样。

我希望以后能够循环遍历它并为订阅中的所有数据库创建数据库警报。我已经为每个资源组的所有数据库创建了警报创建代码。我希望实现相同但是对所有人来说订阅中的数据库

下面,作为参考,是我用于为每个资源组创建数据库警报的完整代码

<#create CPU,DTU and Storage alerts for as many databases as you have in the resource group provided by keyboard input#>

#define variable for resource group name by requesting keyboard input

$rg = Read-Host 'Please, input resource group name here (exactly as it is in Azure)'

<#create the array containing databases where alerts are required. The value of v12.0,user corresponds to the kind of resource as to include only the SQL DBs and not the SQL servers#>

$resources = Get-AzureRmResource | ?{ $_.ResourceGroupName -eq $rg -and $_.kind -eq "v12.0,user"  } | select resourcename,resourceid

#loop through the array and create the DTU alert rule for each DB

foreach($resource in $resources){$alertname=$resource.resourcename.Substring($resource.resourcename.IndexOf('/')+1)+"-DTU-Alert";Add-AzureRMMetricAlertRule -ResourceGroup $rg -location "centralus" -targetresourceid $resource.resourceid -Name $alertname -MetricName "dtu_consumption_percent" -Operator "GreaterThan" -Threshold 90 -WindowSize $([TimeSpan]::Parse("00:15:00")) -TimeAggregationOperator "Average" -verbose -Actions $(New-AzureRmAlertRuleEmail -SendToServiceOwners -CustomEmails "Client-email@here")}

#loop through the array and create the CPU alert rule for each DB

foreach($resource in $resources){$alertname=$resource.resourcename.Substring($resource.resourcename.IndexOf('/')+1)+"-CPU-Alert";Add-AzureRMMetricAlertRule -ResourceGroup $rg -location "centralus" -targetresourceid $resource.resourceid -Name $alertname -MetricName "cpu_percent" -Operator "GreaterThan" -Threshold 90 -WindowSize $([TimeSpan]::Parse("00:15:00")) -TimeAggregationOperator "Average" -verbose -Actions $(New-AzureRmAlertRuleEmail -SendToServiceOwners -CustomEmails "Client-email@here")}

#loop through the array and create the STORAGE alert rule for each DB

foreach($resource in $resources){$alertname=$resource.resourcename.Substring($resource.resourcename.IndexOf('/')+1)+"-STORAGE-Alert";Add-AzureRMMetricAlertRule -ResourceGroup $rg -location "centralus" -targetresourceid $resource.resourceid -Name $alertname -MetricName "storage_percent" -Operator "GreaterThan" -Threshold 90 -WindowSize $([TimeSpan]::Parse("00:15:00")) -TimeAggregationOperator "Average" -verbose -Actions $(New-AzureRmAlertRuleEmail -SendToServiceOwners -CustomEmails "Client-email@here")}

1 个答案:

答案 0 :(得分:0)

您可以使用嵌套循环。以下脚本适合我。

$rg = Get-AzureRmResourceGroup | ?{ $_.ProvisioningState -eq "succeeded" } | select -expandproperty resourcegroupname
foreach ($resourcegroup in $rg) 
{
    $resources = Get-AzureRmResource | ?{ $_.ResourceGroupName -eq $resourcegroup -and $_.kind -eq "v12.0,user"  } | select resourcename,resourceid,location
    foreach ($resource in $resources)
    {
        ##create the DTU alert rule for each DB
        $alertname = $resource.resourcename.Substring($resource.resourcename.IndexOf('/')+1)+"-DTU-Alert"
        Add-AzureRMMetricAlertRule -ResourceGroupName $resourcegroup  -Location $resource.location -targetresourceid $resource.resourceid -Name $alertname -MetricName "dtu_consumption_percent" -Operator "GreaterThan" -Threshold 90 -WindowSize $([TimeSpan]::Parse("00:15:00")) -TimeAggregationOperator "Average" -verbose -Actions $(New-AzureRmAlertRuleEmail -SendToServiceOwners -CustomEmails "test@hotmail.com")

        ##create the CPU alert rule for each DB
        $alertname=$resource.resourcename.Substring($resource.resourcename.IndexOf('/')+1)+"-CPU-Alert"
        Add-AzureRMMetricAlertRule -ResourceGroupName $resourcegroup  -Location $resource.location -targetresourceid $resource.resourceid -Name $alertname -MetricName "cpu_percent" -Operator "GreaterThan" -Threshold 90 -WindowSize $([TimeSpan]::Parse("00:15:00")) -TimeAggregationOperator "Average" -verbose -Actions $(New-AzureRmAlertRuleEmail -SendToServiceOwners -CustomEmails "test@hotmail.com")

        ##create the STORAGE alert rule for each DB
        $alertname=$resource.resourcename.Substring($resource.resourcename.IndexOf('/')+1)+"-STORAGE-Alert"
        Add-AzureRMMetricAlertRule -ResourceGroupName $resourcegroup  -Location $resource.location -targetresourceid $resource.resourceid -Name $alertname -MetricName "storage_percent" -Operator "GreaterThan" -Threshold 90 -WindowSize $([TimeSpan]::Parse("00:15:00")) -TimeAggregationOperator "Average" -verbose -Actions $(New-AzureRmAlertRuleEmail -SendToServiceOwners -CustomEmails "test@hotmail.com")
    }
}