azurepowershellazure-functionsazureservicebus

Unable to print/use Azure Service Bus message output stored as System.Collections.Hashtable


I have a script that performs a task and, upon completion, sends a message to Azure Service Bus. I also have an Azure Function set up to monitor this queue for new messages. When a message is received, the function executes specific actions.

The message sent to the Service Bus includes two variables: filename and tenantId. However, I'm currently facing an issue where I am unable to parse, print, or utilize these variables from the received message.

The format of the message sent to the Service bus:

 $jsonMessage = @{
        "filename" = $fileName
        "tenantId" = $tenantId
    } | ConvertTo-Json -Depth 1

When looking at my output I see that the message is sent succesfully:

INFORMATION: message { "tenantId": "efas5-4aafs3-4as3-ebfaafsafasf1c", "filename": "202405231105.json" }

In the Azure Function waiting for the message I am trying to print it with the below code:

param([object] $mySbMsg, $TriggerMetadata)

Write-Host "PowerShell ServiceBus queue trigger function processed message: $mySbMsg"
write-host $mySbMsg
write-output $mySbMsg

$mySbMsg | Format-List | Out-String | Write-Output

# If you know the property names, access them directly
Write-Output "Filename: $($mySbMsg['filename'])"
Write-Output "Tenant ID: $($mySbMsg['tenantId'])"

$filename = $mySbMsg.filename
$tenantId = $mySbMsg.tenantId

I've tried various methods to print the message, but they all result in the following:

NFORMATION: PowerShell ServiceBus queue trigger function processed message: System.Collections.Hashtable
NFORMATION: System.Collections.Hashtable
OUTPUT: System.Collections.Hashtable
OUTPUT: Filename:
OUTPUT: Tenant ID:

The same thing happens when I manually try to send a message in JSON format.

enter image description here

How should I handle the received message in my Azure Function so I can use the output?


Solution

  • Unable to print/use Azure Service Bus message output stored as System.Collections.Hashtable:

    The issue is with the Azure Function is receiving the message as a Hash table object but not a proper JSON object. Use below given conversions and try retrieving the filename and tenantId property.

    param([object]$mySbMsg, $TriggerMetadata)
    
    Write-Host "PowerShell ServiceBus queue trigger function processed message: $mySbMsg"
    $jsonmsg1 = $mySbMsg | ConvertTo-Json -Depth 1
    $filename = $jsonmsg1 -match '"filename": "(.*?)",'
    if ($filename) {
      $filename = $matches[1]
      Write-Host "File: $filename"
    } else {
      Write-Host "Error: Could not find 'filename'."
    }
    

    Output:

    enter image description here

    You can follow the same for tenant Id as well which is given below.

    $jsonmsg2 = $mySbMsg | ConvertTo-Json -Depth 1
    $tenantId = $jsonmsg2 -match '"tenantId": "(.*?)",'
    if ($tenantId) {
      $tenantId = $matches[1]
      Write-Host "Tenant: $tenantId"
    } else {
      Write-Host "Error: Could not find 'tenantId'."
    }
    

    Refer matching operator -match in PowerShell for better understanding.