Get-AzureRmResource command does not fail/error out for random Resource Names
Cmdlet(s)
Get-AzureRmResourceProvider
PowerShell Version
4.0
Module Version
2.0.1 (Azure) 3.0.1 (AzureRM.Resources)
OS Version
6.3.9600.17400
Description
Am trying to use Get-AzureRmResource command to get information on a particular resource.
Get-AzureRmResource -ResourceType Microsoft.Devices -ResourceName "Anything"
ResourceId ResourceName ResourceType SubscriptionId
/subscriptions/-0d45... Microsoft.Devices/ -0d45-4183-9f36-0b0...
Get-AzureRmResource -ResourceType Microsoft.Devices -ResourceName "Something"
ResourceId ResourceName ResourceType SubscriptionId
/subscriptions/-0d45... Microsoft.Devices/ -0d45-4183-9f36-0b0...
The parameter -ResourceName just accepts any random value but returns the same result. And i think it is specific for the "-ResourceType Microsoft.Devices"
Thanks, Pradebban Raja
It seems to me at least that this is an upstream concern and likely can't be fixed in the powershell module without significant effort. I can reproduce this for any of the 'collection' style queries. It looks like even if you put in a resource provider that doesn't exist, it still returns a 200 response that's simply empty. I assume the logic is that it was a valid query and it was able to process it fine, it's just that there are no results. It seems like there could be more helpful messages though indicating that the resource type you're looking for doesn't exist for instance.
PS X:\> Get-AzureRmResource -ResourceGroupName $workspaceRG -ResourceType "Microsoft.thisistotallynotanactualresourceprovider" -Debug
DEBUG: 10:48:24 AM - GetAzureResourceCmdlet begin processing with ParameterSet 'Get resource collection'.
...snip...
DEBUG: ============================ HTTP REQUEST ============================
HTTP Method:
GET
Absolute Uri:
https://management.azure.com/subscriptions/subid/resourceGroups/rgname/resources?$filter=resourceType EQ 'Microsoft.thisistotallynotanactualresourceprovider'&api-
version=2016-09-01
Headers:
User-Agent : AzurePowershell/v3.1.0.0,PSVersion/v5.1.14393.223
ParameterSetName : Get resource collection
CommandName : Get-AzureRmResource
Body:
DEBUG: ============================ HTTP RESPONSE ============================
Status Code:
OK
Headers:
Pragma : no-cache
x-ms-ratelimit-remaining-subscription-reads: 14991
x-ms-request-id : 0a605597-357d-459e-94c0-e69ed8db7f96
x-ms-correlation-request-id : 0a605597-357d-459e-94c0-e69ed8db7f96
x-ms-routing-request-id : NORTHCENTRALUS:20161104T154825Z:0a605597-357d-459e-94c0-e69ed8db7f96
Strict-Transport-Security : max-age=31536000; includeSubDomains
Cache-Control : no-cache
Date : Fri, 04 Nov 2016 15:48:25 GMT
Body:
{
"value": []
}
DEBUG: 10:48:25 AM - GetAzureResourceCmdlet end processing.
DEBUG: 10:48:25 AM - GetAzureResourceCmdlet end processing.
This is the accepted powershell behavior you should return nothing if there are no query results.
If there is a scenario that is blocked by this behavior, please open an issue that describes that scenario and we will considewr this.
@markcowl, I disagree with terming this as accepted powershell behavior,
Few examples where the results is not empty (and has meaningful error) when the query fetched nothing.
1. Get-AzureRmResource -ResourceType microsoft.web/sites -ResourceName "12345"
Get-AzureRmResource : ResourceNotFound : The Resource 'microsoft.web/sites/12345' under resource group '
2. Get-AzureRmResource -ResourceType microsoft.datafactory/datafactories -ResourceName "12345"
Get-AzureRmResource : ResourceNotFound : The Resource 'microsoft.datafactory/dataFactories/12345' under resource group'
3. Get-AzureRmResource -ResourceType microsoft.sql/servers -ResourceName "12345"
Get-AzureRmResource : ResourceNotFound : The Resource 'microsoft.sql/servers/12345' under resource group '
@twitchax there are couple issues very similar. Let's discuss if we can come to a resolution on this one. Will add another issue that is very similar.
@ravbhatnagar Hey Gaurav, should this be a change in ARM behavior? PowerShell is just reflecting what ARM is returning in this case.
The Get-AzureRmResource cmdlet was updated to use the ARM SDK rather than a generic HttpClient, and returns the following results:
Invalid Resource Group
PS C:\> Get-AzureRmResource -ResourceGroupName 12345
Get-AzureRmResource : Resource group '12345' could not be found.
Invalid Resource Group with Random Resource Name
PS C:\> Get-AzureRmResource -ResourceGroupName 12345 -Name 67890
Get-AzureRmResource : Resource group '12345' could not be found.
Valid Resource Group with Random Resource Name
PS C:\> Get-AzureRmResource -ResourceGroupName AzureFunctions-WestUS -Name 67890
PS C:\>
I think this is the appropriate combination of errors and empty results -- since the object we are trying to query is a resource, if an RG that we are looking for resources in does not exist, then it's appropriate to throw, but if the RG does exist and contains no resources, it's appropriate to return an empty result. I believe this is the correct way to handle this scenario. Please let me know if you feel otherwise and we can discuss adding better error messaging to this cmdlet.
I am also having an issue with Get-AzureRmResource. I am expecting the Get command to return false since the resource doesn't exist and i want to use that check to see weather i want to create the resource or not. Event though I have the ErrorAction set to "Silently Continue", I keep getting the error (Resource not found), and it stops processing, instead of it setting the $resourcevariable and letting me use that as a check further below. How can i get past this?
$resource = Get-AzureRmResource -ResourceName 'AppInsights' -ResourceType 'Microsoft.Insights/components' -ResourceGroupName 'AppInsights_RG' -ErrorAction SilentlyContinue Get-AzureRmResource : ResourceNotFound : The Resource 'Microsoft.Insights/componentsAppInsights' under resource group 'AppInsights_RG' was not found. At line:1 char:13
- $resource = Get-AzureRmResource -ResourceName 'INSTECDevCQS2AppInsigh ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~