English 中文(简体)
所有资源团体提供标签信息的权力说明
原标题:Powershell script to get tag information from all Resource Groups

我需要找回与我们租户的所有子典相关的“所有”标签。 我似乎正在得到租户没有 t的记录。 Maybe 我会处理这一错误,但我认为,我会通过所有订阅而感到振奋,然后通过所有资源小组,然后通过标签信息。 大多数资源小组的记录相同。 谁会发现问题?

# Connect-AzAccount
$subs = Get-AzSubscription
$count = 1
foreach ($sub in $subs) {
    Set-AzContext -SubscriptionId $sub.SubscriptionId
    Write-Host $count. $sub.Name
    $count ++
    $resource_groups = Get-AzResourceGroup
    foreach ($rg in $resource_groups) {
        Write-Host           $rg.ResourceGroupName
        $rgTags = Get-AzTag -Name  owner 
        if ($rgTags -ne $null) {
            foreach ($tag in $rgTags.Values) {
                Write-Host                  $tag.Name
            }
        }
    }
}
问题回答

I would recommend using Search-AzGraph to query the Resource Manager API, it s much easier and faster with KQL than it will ever be with the individual cmdlets.

The query can be read as:

Get all resourcecontainers where their type is microsoft.resources/subscriptions/resourcegroups and their owner Tag is not null or empty and, for each, project the properties subscriptionId, resourceGroup, ownertag.

Search-AzGraph -Query @ 
    resourcecontainers
    | where type =~  microsoft.resources/subscriptions/resourcegroups 
    | extend ownertag = tags.owner
    | where isnotempty(ownertag)
    | project subscriptionId, resourceGroup, ownertag
 @




相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签