循环访问 Azure 对象并筛选标记

Joo*_*ost 2 powershell azure azure-powershell

我有一个 Azure 对象(虚拟机、数据仓库或分析服务)的集合,我想遍历该集合,但也过滤掉具有特定标签的对象。

在此处输入图片说明

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

如何指定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
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

The*_*ian 5

所以该Tag属性是一个哈希表。有多种方法可以访问哈希表的值,但我将使用您已经尝试过的方法,只是语法有点错误。这是我将如何完成您正在寻找的内容。

$AnalysisServicesServers = Get-AzureRmAnalysisServicesServer | Where-Object {$_.Tag['Environment'] -ne "Production"}
Run Code Online (Sandbox Code Playgroud)

如果您的结果上没有“环境”标签,这可能会引发错误,但它会为您提供您正在寻找的结果。