获取 azure powershell 上其标签以(或包含)特定字符串开头的所有资源 - Azure Powershell

get All resources on azure powershell whose Tags start with (or include) a particular String - Azure Powershell

我在 Azure 中有将近 20 个资源,其中 4 个已被赋予标签@

{"Office1work"="work"}
{"Office2practice"="Practice"}
{"Office3practice"="Practice"}
{"Office4practice"="Practice"}

现在我想获取Tag名称以关键字"Office"开头的资源。 我知道通过 TagName 获取资源,例如 "hello",我只需使用以下命令,

get-azureRmResource -TagName "Hello"

如何使用 get-azurermresource 的 -Tagname 属性 为我提供标签以关键字 "Office" 开头的所有资源?

或者有没有其他好的方法来获取所有标签以特定字符串开头的资源?

谢谢:)

您可以使用此代码段:

$resources = Get-AzureRmResources
$resources.foreach{ if ($PSItem.tags.keys -match '^Office') { $PSItem } }

首先获取订阅中的所有资源,然后过滤掉所有标签不匹配'Office' "expression".

的资源

正如@LotPings 指出的那样,过滤而不保存到临时变量可能更有意义:

$resources = Get-AzureRmResources|Where-Object {$_.tags.keys -match "^Office"}

此外,我没有注意到您要求 starts with 过滤器,因此您应该使用 ^Office 作为更严格的过滤器(如果需要)。