循环访问Azure对象并过滤标记

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

我有一组Azure对象(虚拟机,数据仓库或Analysis Services),我想循环遍历该集合,但也过滤掉具有特定标记的对象。

enter image description here

例如,获取所有没有名为“Environment”的标签的Analysis Services,其值为“Production”。

如何指定Where-Object过滤器(尝试了几种方法但没有成功)

cls

# Login to Azure
#Login-AzureRmAccount | Out-Null 

# Selecting the right subscription
#Select-AzureRmSubscription -SubscriptionName "MySubscription" | Out-Null 


# Get all my Analysis Services, but leave out those with the tag Environment=Production
$AnalysisServicesServers = Get-AzureRmAnalysisServicesServer | Where-Object {$_.Tag -notcontains @{Environment="Production";}}

# Loop Through the collection and to something
foreach ($AnalysisServicesServer in $AnalysisServicesServers)
{
    $AnalysisServicesServer                                                      # Show all properties
    Write-Host "1 Name $($AnalysisServicesServer.Name)"                          # Show Name
    Write-Host "2 Tags count [$($AnalysisServicesServer.Tag.Keys.Count)]"        # Show # of tags
    Write-Host "3 Tags Values [$($AnalysisServicesServer.Tag.Values)]"           # Show values
    Write-Host "4 Tags Keys [$($AnalysisServicesServer.Tag.Keys)]"               # Show keys
    Write-Host "5 Tags Keys [$($AnalysisServicesServer.Tag.Keys["Environment"])]" # NOT WORKING
}

enter image description here

powershell azure azure-powershell
1个回答
3
投票

所以Tag属性是一个哈希表。有多种方法可以访问哈希表的值,但是我会使用你已经尝试过的那个,但只是让语法有点错误。以下是我将如何完成您所寻找的目标。

$AnalysisServicesServers = Get-AzureRmAnalysisServicesServer | Where-Object {$_.Tag['Environment'] -ne "Production"}

如果您的结果没有“环境”标记,则可能会出错,但它会为您提供您正在寻找的结果。

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