azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Get-AzureRmResource: Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JObject'

Open JaDevGuy opened this issue 8 years ago • 8 comments

The Azure Resource Explorer shows this command should give me my admin token for my azure function

Get-AzureRmResource -ResourceGroupName contosoFunc -ResourceType "Microsoft.Web/sites/functions/token" -ResourceName "demofunct1/admin" -ApiVersion 2016-08-01

However running this cmdlet gives the error "Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JObject'."

This is my current setup:

PS C:\WINDOWS\system32> get-module AzureRM -ListAvailable
    Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     5.4.1      AzureRM

PS C:\WINDOWS\system32> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.16299.248
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.16299.248
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


Message        : Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JObject'.
StackTrace     :    at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)
                    at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions.JsonExtensions.FromJson[T](Stream
                 stream)
                    at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions.HttpMessageExtensions.<ReadContentAs
                 JsonAsync>d__0`1.MoveNext()
                 --- End of stack trace from previous location where exception was thrown ---
                    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                    at Microsoft.Azure.Commands.ResourceManager.Cmdlets.RestClients.ResourceManagerRestClientBase.<Send
                 RequestAsync>d__7`1.MoveNext()
                 --- End of stack trace from previous location where exception was thrown ---
                    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                    at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzureResourceCmdlet.<GetResou
                 rce>d__66.MoveNext()
                 --- End of stack trace from previous location where exception was thrown ---
                    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                    at Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.GetAzureResourceCmdlet.<GetResou
                 rces>d__65.MoveNext()
Exception      : System.InvalidCastException
InvocationInfo : {Get-AzureRmResource}
Line           : Get-AzureRmResource -ResourceGroupName contosoFunc -ResourceType "Microsoft.Web/sites/functions/token" -ResourceName "demofunct1/admin" -ApiVersion 2016-08-01
Position       : At line:1 char:1
                 + Get-AzureRmResource -ResourceGroupName contosoFunc -ResourceType Micro ...
                 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 2

JaDevGuy avatar Mar 01 '18 22:03 JaDevGuy

@ravbhatnagar Can you take a look at this issue?

maddieclayton avatar Mar 05 '18 19:03 maddieclayton

If you guys need me to provide anything here just let me know.

JaDevGuy avatar Mar 19 '18 16:03 JaDevGuy

I am seeing this exact same issue. We have no clean way to retrieve the master and default keys from a function app service. I am really trying to avoid needing to make REST calls directly to get it.

brushwood24 avatar Jun 07 '18 19:06 brushwood24

I'm having same issue here, eager for a fix on this.

mguirao avatar Dec 13 '18 17:12 mguirao

This still needs to be fixed but in the interim, you can call the AzureRM API directly to get the default function key. This page describes how http://blog.octavie.nl/index.php/2017/04/20/get-the-default-azure-function-key-with-powershell. Obviously, this is way harder than it should be.

brushwood24 avatar Dec 13 '18 17:12 brushwood24

Yeah, this is what i ended up doing. I'm used to this by now, not the first bug i'm facing. But same as you, REST is always the final solution.

mguirao avatar Dec 17 '18 13:12 mguirao

:bell: Routing this issue to @act-identity-squad.

The AzureRM module (including Get-AzureRmResource) is retired. The supported path for the reporter's scenario (fetching function-app admin keys) on current Az modules is Invoke-AzRestMethod. Closing as obsolete against the retired cmdlet. If you're still hitting an InvalidCastException on against current Az.Resources, please open a fresh issue with a current repro.

MaddyMicrosoft avatar Aug 16 '26 22:08 MaddyMicrosoft