2017-12-28 7 views
0

私はAzureオブジェクト(仮想マシン、データウェアハウスまたはAnalysis Services)のコレクションを持っており、そのコレクションをループするだけでなく、特定のタグを持つオブジェクトもフィルタリングします。Azureオブジェクトをループしてタグにフィルターをかける

enter image description here

例えばが値「生産」と「環境」というタグを持っていない行うことを、すべてのAnalysis Servicesを取得します。私は(成功せず、いくつかの方法を試してみました)ここで、オブジェクトフィルタ

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

答えて

2

を指定するにはどうすればよい

だからTagプロパティは、ハッシュテーブルです。ハッシュテーブルの値にアクセスするには複数の方法がありますが、すでに試みたものを使用しますが、構文がちょっと間違っています。ここで私があなたが探しているものを達成する方法です。

あなたがそれらの上に「環境」のタグを持っていない結果を持っている場合は、エラーがスローされることがあり、それはあなたが探している結果が得られます

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

+0

魅力的な作品です! – Joost

関連する問題