为什么 ForEach-Object 不适用于 AzureRM cmdlet

Why ForEach-Object doesn't work for AzureRM cmdlet

我正在尝试执行一个非常简单的 cmdlet,但它不起作用

$resources = Get-AzureRMResourceGroup | % { Get-AzureRMResource -ResourceGroupName $_.ResourceGroupName}

$resources

抛出的错误信息如下

Get-AzureRmResource : Parameter set cannot be resolved using the
specified named parameters. At line:1 char:45
+ ... Group | % { Get-AzureRMResource -ResourceGroupName $_.ResourceGroupNa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-AzureRmResource], Para meterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.Azure.Commands.R
esourceManager.Cmdlets.Implementation.GetAzureResourceCmdlet

-ResourceGroupName 是参数集的一部分,这意味着如果使用它,它必须与属于其集的其他强制参数一起包含。

文档here 显示使用该参数的两个参数集。

#Parameter Set: Get resource by name and group
Get-AzureRmResource [-ApiVersion <System.String> ] [-ExpandProperties] [-ExtensionResourceName <System.String> ] [-ExtensionResourceType <System.String> ] [-IsCollection] [-ODataQuery <System.String> ] [-Pre] [-ResourceGroupName <System.String> ] [-ResourceName <System.String> ] [ <CommonParameters>]

#Parameter Set: Get resource by name, group and type
Get-AzureRmResource -ResourceGroupName <System.String> -ResourceName <System.String> -ResourceType <System.String> [-ApiVersion <System.String> ] [-ExpandProperties] [-ExtensionResourceName <System.String> ] [-ExtensionResourceType <System.String> ] [-ODataQuery <System.String> ] [-Pre] [ <CommonParameters>]

根据脚本中使用的参数,它无法确定要使用的集合。

在您的命令中添加更多参数以缩小要使用的集合的范围。

Example 1: Get a resource

This command gets a resource of the type microsoft.web/sites, named ContosoWebsite under ResourceGroup11.

Windows PowerShell

PS C:\> Get-AzureRmResource -ResourceType "microsoft.web/sites" -ResourceGroupName "ResourceGroup11" -ResourceName "ContosoWebsite"

使用范围较窄的参数集

例如:

Get-AzureRmResourceGroupreturns像这样的对象

PS C:\> Get-AzureRmResourceGroup

ResourceGroupName : xxxxxxxxxxxxxxxxx
Location          : xxxxxxxxxxxx
ProvisioningState : Succeeded
Tags              : 
ResourceId        : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxxxxxxxxxxxxxxxx

您可以尝试将 ResourceId 传递给下一个 cmdlet。

#Parameter Set: Get a single resource by its Id.
Get-AzureRmResource -ResourceId <String> [-ApiVersion <System.String> ] [-ExpandProperties] [-ODataQuery <System.String> ] [-Pre] [ <CommonParameters>]

您的命令将如下所示:

$resources = Get-AzureRMResourceGroup | % { Get-AzureRMResource -ResourceId $_.ResourceId}

$resources