0

I have a powershell based http trigger azure function that uses the Az module to call get-azvm. It get's it's data from a logic-app POST. The output looks correct and it's type shows as string but the cmdlet does not like the variable. The function looks like this:

Write-Host "PowerShell HTTP trigger function processed a request."
$sub = $Request.Body.subject | Out-String
write-host "sub:" $sub
$split = $sub -split "[/]"
write-host "split:" $split
$avd = $split[8] | Out-string
Write-Host "avd" $avd

$rgName_avd = 'rg-azgroup'

Get-AzVM -Name $avd -ResourceGroupName $rgName_avd

The post input looks like:

/subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourcegroups/rg-azgroup/providers/Microsoft.Compute/virtualMachines/myvmname-0

The error in the logs starts as:

[Error] ERROR: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.Exception             :Type       : Newtonsoft.Json.JsonReaderExceptionTargetSite :Name          : ParseValueDeclaringType : Newtonsoft.Json.JsonTextReaderMemberType

It feels like it's an issue with the input type but using ConvertFrom-Json does not seem to work. Any ideas what I'm doing wrong? If I hard code the VM name or define a variable locally and use it the command executes.

The AZ module versions being used in requirements.psd1:

@{
    'Az.Accounts' = '2.7.6'
    'Az.Compute' = '4.26.0'
}
Skin
  • 9,085
  • 2
  • 13
  • 29
TyMac
  • 783
  • 2
  • 9
  • 32
  • Can I ask, on which line do you get the error? On `$sub = $Request.Body.subject | Out-String`, or on `Get-AzVM -Name $avd -ResourceGroupName $rgName_avd` ? – Vince Bowdren May 11 '22 at 14:35
  • The error occurs at: Get-AzVM -Name $avd -ResourceGroupName $rgName_avd (also worth noting that the same script works without error if an event grid subscription is used for the same input) – TyMac May 15 '22 at 18:21
  • In that case, I strongly suspect that something is going wrong with the way you're isolating the $avd value; if you add more detailed logging, you'll probably be able to figure out what is wrong with it. – Vince Bowdren May 16 '22 at 08:50

1 Answers1

0

Here are the few workarounds that you can try:

Solution 1:
You might not be passing JSON to DeserializeObject.
It looks like that type of tmpfile from File.WriteAllText(tmpfile,... is string that contain file path. JsonConvert.DeserializeObject takes JSON value rather than the file path, it fails trying to convert something like @"c:\temp\fooo" - which is not JSON.

Solution 2:
Check that is the file containing JSON string has BOM.
Once u remove BOM the problem may get resolved.

Solution 3:
For a Web API action that was binding to a string instead to an object or a JObject when JSON was correct, but the binder tried to obtain a string from the JSON structure and failed. So, instead of:

[HttpPost("[action]")]
public object Search([FromBody] string data)

Try this:

[HttpPost("[action]")]
public object Search([FromBody] JObject data)

References:
Unexpected character encountered while parsing value
Unexpected character encountered while parsing value ASP.NET Core and Newtonsoft