powershell - 循环遍历 Azure 对象并筛选标签

标签 powershell azure azure-powershell

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

enter image description here

例如,获取具有名为“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
}

enter image description here

最佳答案

因此,Tag 属性是一个哈希表。有多种方法可以访问哈希表的值,但我将使用您已经尝试过的方法,但语法有点错误。以下是我将如何实现您正在寻找的目标。

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

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

关于powershell - 循环遍历 Azure 对象并筛选标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48014163/

相关文章:

powershell - 如何解析来自Invoke-WebRequest的响应

用于从文件夹列表中省略遍历访问被拒绝的文件夹的 Powershell 脚本

azure - 执行 Azure 函数 cmdlet Get-AzADAppCredential 的权限不足

macos - 无法从 Mac 连接到 Azure

regex - 使用 -match 从字符串中提取美元符号和金额

powershell - 在数组中查找字符串 Powershell V2.0

azure - 使用 Terraform 的 Azure 中的集群访问问题

entity-framework - Azure Web 角色中的 Entity Framework DbContext

azure - 如何从 SQL Azure V11 升级到 V12?

azure - Azure CLI和PowerShell之间的区别