我需要在我对excel工作表的订阅中获得一个标记名“ SquId”及其所有资源组的值。我尝试了下面的代码,它给了我所有可用的标签。但是我只需要获取特定标签即可使用excel。一旦我将所有内容都添加至excel中即可。
Select-AzureRmSubscription -SubscriptionName $SubscriptionName
$results = @()
$TagsAsString = ""
$datetime = Get-Date -Format "dd-MM-yy-hh-mm"
#Getting all Azure Resource
$resources = Get-AzureRmResource
foreach ($resource in $resources) {
#Fetching Tags
$Tags = $resource.Tags
#Checkign if tags is null or have value
if ($Tags -ne $null) {
$Tags.GetEnumerator() | % { $TagsAsString += $_.Key + ":" + $_.Value + ";" }
}
else {
$TagsAsString = "NULL"
}
#Adding to Results
$details = @{
ResourceGroup = $resource.ResourceGroupName
Resource = $resource.Name
Tags = $TagsAsString
}
$results += New-Object PSObject -Property $details
#Clearing Variable
$TagsAsString = ""
}
$OutputPathWithFileName = $OutputCSVFilePath + "\Tags-" + $SubscriptionName + "-" + $datetime + ".csv"
$results | Select-Object -Property ResourceGroup, Resource, Tags | export-csv -Path $OutputPathWithFileName -NoTypeInformation
}