用于获取资源组中的资源的 Azure Powershell 命令

问题描述 投票:0回答:5

在Azure Powershell 0.8和0.9版本中,有命令

Get-AzureResource -ResourceGroupName "RGName" -OutputObjectFormat New

并且,它返回上述Azure资源组中的资源。需要azure模式为ARM模式。

但是,在 Azure PowerShell 版本 1.2 及更高版本中

Get-AzureRMResource -ResourceGroupName "RGName" 

无法提供资源组中存在的资源。它需要更多参数,例如“ResourceID”或“ResourceName”,这使得它特定于资源。

我需要的是,它应该返回资源组中的所有资源。 是新版本的错误还是我遗漏了一些东西! 建议

azure-powershell azure-resource-manager
5个回答
8
投票

您可以使用Find-AzureRmResource:

Find-AzureRmResource -ResourceGroupNameContains "RGName"

4
投票

Get-AzureRMResource
PowerShell 命令是通过 REST API 实现的。如果您检查列出资源组中的资源的 REST API。你会看到类似这样的东西。

https://management.azure.com/subscriptions/<Subscription ID>/resourceGroups/<resource group name>/resources?api-version=2015-01-01

并且,如果您将

-debug
选项添加到
Get-AzureRMResource -ResourceId <the resource id>
,您将找到它正在使用的 REST API。

https://management.azure.com<the resource id>?api-version=2015-01-01

比较这两个 REST API,您将看到以下 PowerShell 命令将列出资源组中的资源。

Get-AzureRmResource -ResourceId "/subscriptions/<Subscription ID>/resourceGroups/<resource group name>/resources"

我知道这很棘手,但它确实有效。


4
投票

尝试

Get-AzureRmResource | where {$_.ResourceGroupName -eq "RG"}

3
投票

获取对象中的资源组

$groups = Get-AzureRmResourceGroup -Name $RG_Name 

获取变量中资源组的所有资源

$t=(Find-AzureRmResource -ResourceGroupNameEquals 
$groups.ResourceGroupName).ResourceName

0
投票

您可以使用一个非常简单的命令

Get-AzResource -ResourceGroupName $rg
来获取资源组内的所有资源,然后可以循环执行某些任务。

© www.soinside.com 2019 - 2024. All rights reserved.