I'm interested in querying an Azure subscription to provide a Tagging report which indicates the VM Name, Azure Region, and the Tags and Tag Value for each VM.
My current script provides me with the all the data with the exception of the Tag Value for each respective Tag. My output is displaying a result as if a Tag doesn't exist.
Does anyone have any advice on my script? Thanks!
param( [Parameter(Mandatory = $true)] [String] $subscriptionName )
Select-AzSubscription -Subscription $subscriptionName
$allTags = Get-AzTag
$allVMs = Get-AzVM
$vmInformation = @() foreach ($vm in $allVMs) {
$vmInformationObject = New-Object PSObject $vmName = $vm.Name $vmRegion = $vm.Location $vmInformationObject | Add-Member -MemberType NoteProperty -Name "VM_Name" -Value $vmName $vmInformationObject | Add-Member -MemberType NoteProperty -Name "VM_Region" -Value $vmRegion $vm_tags = $vm.tags foreach ($tag in $allTags) { $IfTagExists = $false foreach ($vmtag in $vm_tags) { if ($tag.name -like $vmtag.keys) { $IfTagExists = $true $vmInformationObject | Add-Member -MemberType NoteProperty -Name $tag.Name -Value $vmtag.$($tag.Name) break } } if ($IfTagExists -eq $false) { $vmInformationObject | Add-Member -MemberType NoteProperty -Name $tag.Name -Value "--" } } $vmInformation += $vmInformationObject }
$vmInformation | Export-Csv "path.csv" -NoTypeInformation -Force
$tag.name -like $vmtag.keys
should use a different operator -->$tag.name -in $vmtag.keys
as an example. You are comparing a single value to a list of values. You, therefore, need an operator that compares items in a list. – AdminOfThings