Get-AzureRmResource unable to handle empty array response
Cmdlet(s)
Get-AzureRmResource
PowerShell Version
5.1.14393.223
Module Version
3.1.0
OS Version
10.0.14393.223
Description
When trying to use Get-AzureRmResource, it appears that if the service returns a 200 response with an empty array, the cmdlet doesn't know how to handle it and errors out. I received this error:
Get-AzureRmResource : Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'Newtonsoft.Json.Linq.JObject'. when accessing this resourceid:
/subscriptions/$subscriptionId/resourceGroups/$workspaceRG/providers/Microsoft.OperationalInsights/workspaces/$WorkspaceName/savedSearches/$id/schedules
as there are no schedules for that saved search, but trying to access similar resources that return arrays works, e.g.:
/subscriptions/$subscriptionId/resourceGroups/$workspaceRG/providers/Microsoft.OperationalInsights/workspaces/$WorkspaceName/savedSearches
Debug Output
DEBUG: ============================ HTTP REQUEST ============================
HTTP Method:
GET
Absolute Uri:
https://management.azure.com/subscriptions/redactedsubid/resourceGroups/rsgname/providers/Microsoft.OperationalInsights/workspaces/workspacename/savedSearches/savedsearchname/schedules?api-version=2015-03-20
Headers:
User-Agent : AzurePowershell/v3.1.0.0,PSVersion/v5.1.14393.223
ParameterSetName : Get a single resource by its Id.
CommandName : Get-AzureRmResource
Body:
DEBUG: ============================ HTTP RESPONSE ============================
Status Code:
OK
Headers:
Pragma : no-cache
Strict-Transport-Security : max-age=31536000; includeSubDomains
X-Content-Type-Options : nosniff
x-ms-ratelimit-remaining-subscription-reads: 14987
x-ms-request-id : f49f384f-2d90-4862-b2c3-eeb32501ae8c
x-ms-correlation-request-id : f49f384f-2d90-4862-b2c3-eeb32501ae8c
x-ms-routing-request-id : SOUTHCENTRALUS:20161104T152844Z:f49f384f-2d90-4862-b2c3-eeb32501ae8c
Cache-Control : no-cache
Date : Fri, 04 Nov 2016 15:28:44 GMT
Server : Microsoft-IIS/8.5
X-Powered-By : ASP.NET
Body:
[]
get-AzureRmResource : Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'Newtonsoft.Json.Linq.JObject'.
At line:1 char:1
+ get-AzureRmResource -ResourceId "/subscriptions/$subscriptionId/resou ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-AzureRmResource], InvalidCastException
+ FullyQualifiedErrorId : Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'Newtonsoft.Json.Linq.JObject'.,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzureRe
sourceCmdlet
Script/Steps for Reproduction
Create a saved search in OMS but without a schedule, and run this with the appropriate vars assigned
$subscriptionId = ""
$workspaceRG = ""
$workspaceName = ""
$savedsearch = ""
get-AzureRmResource -ResourceId "/subscriptions/$subscriptionId/resourceGroups/$workspaceRG/providers/Microsoft.OperationalInsights/workspaces/$WorkspaceName/savedSearches/$savedsearch/schedules" -ApiVersion 2015-03-20 -Debug
trying do a
get-AzureRmResource -ResourceId "/subscriptions/$subscriptionId/resourceGroups/$workspaceRG/providers/Microsoft.OperationalInsights/workspaces/$WorkspaceName/savedSearches/$savedsearch
should work though.
For the record I'm getting the same error
Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'Newtonsoft.Json.Linq.JObject'. At line:47 char:3
-
throw $_.Exception; -
~~~~~~~~~~~~~~~~~~- CategoryInfo : OperationStopped: (:) [], InvalidCastException
- FullyQualifiedErrorId : Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'Newtonsoft.Json.Linq.JObject'.
But I see a response in the debug output
Body: [{"id":"subscriptions/bce7ec4f-86ce-4a55-9253-7de908a98ed0/resourceGroups/uks-rsg-all-prd/providers/Microsoft.OperationalInsights/workspaces/819631-OMS/savedSearches/raxunexpectedshutdown/schedules/raxunexpectedshutdown","etag":"W/"datetime'2016-10-19T00%3A42%3A27.3128 973Z'"","properties":{"Interval":5,"QueryTimeSpan":5,"Enabled":true,"NearRealTime":false}}]
What is interesting, is I was playing around to see about getting schedules/ with no savedsearch id, if there are multiple schedules (which I don't think is possible just testing) my get uri
https://management.azure.com/subscriptions/bce7ec4f-86ce-4a55-9253-7de908a98ed0/resourceGroups/uks-rsg-all-prd/providers/Microsoft.OperationalInsights/workspaces/819631-OMS/savedSearches/raxunexpectedshutdown/schedules?api-version=2015-03-20
notice there is NOTHING following /schedules except the API, and yet in the body that is returned, you see the id of the saved search.
@ravbhatnagar can you take a look?
I have get the same error but when I use Invoke-Restmethod I just get the correct output returned.
` $SubscriptionURI = "https://management.azure.com/subscriptions/$SubscriptionID/resourceGroups/stsomsrg/providers/Microsoft.OperationalInsights/workspaces/$omsworkspace/savedSearches/7ebe4b31-3604-40c0-932f-053392005464/schedules" +'?api-version=2015-11-01-preview'
$params = @{ ContentType = 'application/x-www-form-urlencoded' Headers = @{ 'authorization'="Bearer $($Token.access_token)" } Method = 'Get' URI = $SubscriptionURI }
Invoke-RestMethod @params -OutVariable OMSSchedules
$OMSSchedules | fl *
id
subscriptions/[mysubscriptionid]/resourceGroups/[myresourcegroup]/providers/Microsoft.OperationalInsights/workspaces/[myworkspace]/savedSearches/7ebe4b31-36...
id : subscriptions/[mysubscriptionid]/resourceGroups/[myresourcegroup]/providers/Microsoft.OperationalInsights/workspaces/[myworkspace]/savedSearches/7 ebe4b31-3604-40c0-932f-053392005464/schedules/b9cb3487-f7db-420a-b845-403b4a962888 etag : W/"datetime'2016-09-23T12%3A00%3A12.094347Z'" properties : @{Interval=60; QueryTimeSpan=120; Enabled=True; NearRealTime=False} ` So it seems to be something related to using the 'Newtonsoft.Json.Linq.JArray' class in the Get-AzureRMResource cmdlet.
/Stefan
We can confirm that this is a bug in the Operational Insights resource provider.
We’ll be making a fix in the resource provider to remove this bug. After we make the fix the document returned in response to making a call to /schedules will change to comply with Azure Resource Manager requirements.
Example call
armclient get https://management.azure.com/subscriptions/******/resourceGroups/mms-eus/providers/Microsoft.OperationalInsights/workspaces/*****/savedSearches/*****/schedules?api-version=2014-11-10
Current behavior:
[
{
"id": "subscriptions/********/resourceGroups/mms-eus/providers/Microsoft.OperationalInsights/workspaces/*****/savedSearches/*****/schedules/********",
"etag": "W/\"datetime'2016-10-19T02%3A49%3A47.8123398Z'\"",
"properties": {
"Interval": 5,
"QueryTimeSpan": 5,
"Enabled": true,
"NearRealTime": false
}
}
]
Behavior with bug fixed (array is returned in a property called “value”):
{
“value”: [
{
"id": "subscriptions/********/resourceGroups/mms-eus/providers/Microsoft.OperationalInsights/workspaces/*****/savedSearches/*****/schedules/********",
"etag": "W/\"datetime'2016-10-19T02%3A49%3A47.8123398Z'\"",
"properties": {
"Interval": 5,
"QueryTimeSpan": 5,
"Enabled": true,
"NearRealTime": false
}
}
]
}
Once this bug is fixed you will be able to use Get-AzureRMResource to retrieve the schedules for an alert.
We deployed the fix last night and verified that Get-AzureRmResource now returns the correct results.
Since this was a change in the resource provider, and not to the PowerShell, it is not necessary to update the Azure PowerShell modules to get this fix.
I'm closing this issue since we've released a fix.