0

I am working in Azure and trying to loop through all Subscriptions tags in my tenant and return all results in a list with a specific tag name + value. The idea is to auto alert certain people when platform changes are happening or incidents occur (sort of cheap mans ITSM).

$SubTags.Properties.TagsProperty

$ArrayOfTags = @()

ForEach($s in $SubTags) {
if ($SubTags.Properties.TagsProperty -contains "Environment"){
    $ArrayOfTags += $SubTags
}
else { 
        Write-Host "fail"
     }
}
Return $ArrayOfTags

All i get is:

fail
fail
fail
fail
fail
fail
fail
....

I have tried with some dummy code

$aa = @("aa","bb","cc", "cc")
[System.Collections.ArrayList]$bbb = @()

ForEach ($a in $aa){
if ($aa -eq "cc") {
$bbb.Add($_)  
    Write-Host "cool cool cool"
}
else {
    Write-Host ":("
}
}

Return $bbb

And i get:

0
cool cool cool
1
cool cool cool
2
cool cool cool
3
cool cool cool

Returned.

I appreciate that this might be a bit to vague, but I am new to ps, so appreciate any sort of help :-)

Good day

Solmyr
  • 11
  • 1
  • 2
    You should be using $s in your loop… not $subtags – Doug Maurer Sep 23 '21 at 07:04
  • I am pretty sure the object that stores the tag is a dictionary and it is per resource in the subscription, so if you have multiple dictionaries, you need to use your loop and test the individual dictionary (as Doug has said above, use `$s` in your loop) using the `$s.ContainsKey("name you wish to find")` method. If it exists, you can use the index property to get the value `$s["name you wish to find"]`. You may want to return [custom objects](https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject?view=powershell-7.1) with the resource name too. – Ash Sep 23 '21 at 07:27
  • Does this answer your question? [Powershell ArrayList magically inserts a zero](https://stackoverflow.com/questions/52641400/powershell-arraylist-magically-inserts-a-zero) or [A: Prevent ArrayList.Add() from returning the index](https://stackoverflow.com/a/2149205/1701026) – iRon Sep 23 '21 at 10:24
  • AS was said above you are referring to the wrong variable name in the loop - it should be $s – Hardoman Sep 23 '21 at 10:29
  • 1
    `foreach($thing in $collectionOfThings) { <# do something with $thing, not the whole collection #> }` – Theo Sep 23 '21 at 11:40
  • 1
    Ah of course - thank you all so much!! Highly appreciated. – Solmyr Sep 23 '21 at 11:55

0 Answers0