Search code examples
powershellazureazure-resource-managerazure-resource-group

Why ForEach-Object doesn't work for AzureRM cmdlet


I am trying to execute a very simple cmdlet but it doesn't work

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

$resources

The error message being thrown is as follows

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


Solution

  • -ResourceGroupName is part of a parameter set which means that if used it must be included along with the other mandatory parameters that belong to its set.

    Documentation here shows two parameter sets with that parameter is used.

    #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>]
    

    Based on the parameters used in your script it cannot determine which set to use.

    Either Add more parameters to your command to narrow down which set to use.

    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"
    

    OR

    use a set that takes parameters where the scope is narrower

    For example:

    Get-AzureRmResourceGroup returns an object like this

    PS C:\> Get-AzureRmResourceGroup
    
    ResourceGroupName : xxxxxxxxxxxxxxxxx
    Location          : xxxxxxxxxxxx
    ProvisioningState : Succeeded
    Tags              : 
    ResourceId        : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxxxxxxxxxxxxxxxxx
    

    You could try passing the ResourceId to the next cmdlet.

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

    You command would then look like this:

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