Get-AzureRmResource: Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JObject'
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
@ravbhatnagar Can you take a look at this issue?
If you guys need me to provide anything here just let me know.
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.
I'm having same issue here, eager for a fix on this.
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.
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.
: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.