powershell - 如何从 Get-AzureRmResourceGroup 标签检索键值对

标签 powershell azure azure-resource-manager

我有一个使用具有键值对的标记来计划的资源组:“IncludeInSchedule”:“true”

当我执行 Get-AzureRmResourceGroup -Name MyResourceGroup 时,我看到:

ResourceGroupName : MyResourceGroup
Location          : northcentralus
ProvisioningState : Succeeded 
Tags              :
                    Name               Value
                    =================  ======
                    IncludeInSchedule  True
ResourceId        : /subscriptions/ea904806-082f-4ce5-9b66-288afd61f83e/resourceGroups/MyResourceGroup

当我尝试将标签中的值读入变量时,我遇到了困难。它看起来像一个哈希表,但是 Get-AzureRmResourceGroup -Name MyResourceGroup | Get-Member Tags建议它是一个哈希表数组,我没看错吗?

Name MemberType Definition                 
---- ---------- ----------                 
Tags Property   hashtable[] Tags {get;set;}

如果我将 Get-AzureRmResourceGroup 的输出通过管道传输到 Select-Object 并展开我得到的标签属性:

Name  Value
===== =====
Value True      
Name  IncludeInSchedule

这不是我期望看到的,我期望看到的是:

IncludeInSchedule  True

此外,当我尝试将标签分配给变量以便提取 IncludeInSchedule 值时,我没有看到任何值。

如何从中提取值(value)?

最佳答案

根据微软官方文档:Displaying Hash Tables

以下应该有效:

> (Get-AzureRmResourceGroup -Name MyResourceGroup).Tags.Value
True

您可以通过以下方式获取所有标签的 key :

> (Get-AzureRmResourceGroup -Name MyResourceGroup).Tags.Keys
Value
Name

例如,我使用这种方式来访问虚拟机的正常运行时间,如下所示:

> $vm = Get-AzureRmVM -ResourceGroupName $rgName -Name $vmName
> $vm.Tags.UptimeMonday
24h

如果您的命名与 Microsoft 的命名发生任何冲突,他们进一步声明:

If the key name collides with one of the property names of the HashTable type, you can use PSBase to access those properties. For example, if the key name is keys and you want to return the collection of Keys, use this syntax

$hashtable.PSBase.Keys

关于powershell - 如何从 Get-AzureRmResourceGroup 标签检索键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35046271/

相关文章:

azure - 嵌套 ARM 模板并依赖于另一个资源组

powershell - 我可以将 Powershell 4 与 Powershell 3 并排安装吗?

azure - 如何在 Pulumi 上使用 "GetComponnectOutput"访问 Azure App Insights 输出

azure - 在 Azure 云服务上启用 OPTIONS 方法(以启用 CORS)

azure - 如何将属性从 Azure AD 登录映射到 B2C 身份?

azure-devops - 重新部署Azure Functions ARM模板会删除我发布的函数

reactjs - 从Powershell在Appveyor上运行Jest测试

python - 如何在 Qualtrics 中循环数百个图像 - 实现代码所需的帮助

c# - CAKE 在 TeamCity 构建期间抛出异常,但无法在本地重现

Azure 服务主体无法访问服务管理 API