我有一个使用具有键值对的标记来计划的资源组:“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/