gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:02:00 24 4
gpt4 key购买 nike

我有一个 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/

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