我有一组Azure对象(虚拟机,数据仓库或Analysis Services),我想循环遍历该集合,但也过滤掉具有特定标记的对象。
例如,让所有不的Analysis Services都有一个名为" Environment"的标签。价值"生产"。
如何指定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
}
答案 0 :(得分:3)
因此Tag
属性是哈希表。有多种方法可以访问哈希表的值,但是我会使用你已经尝试过的那个,但只是语法有点错误。以下是我将如何完成您正在寻找的内容。
$AnalysisServicesServers = Get-AzureRmAnalysisServicesServer | Where-Object {$_.Tag['Environment'] -ne "Production"}
如果你的结果没有“环境”,那么这可能会引发错误。标记在他们身上,但它会为您提供您正在寻找的结果。