1

I have a logic app that is passing input parameters to a PowerShell Azure Automation job the job keeps failing. The input parameters are in this format:


Input Parameters

The PowerShell script I have is as follows:

 param (
 [Parameter(Mandatory=$true)]
 [string]$azureArcJson,
 [Parameter(Mandatory=$true)]
 [string]$crowdstrikeJson
 )
 
 try {
 Print the received JSON strings for debugging
 Write-Output "Received Azure Arc JSON string: $azureArcJson"
 Write-Output "Received Crowdstrike JSON string: $crowdstrikeJson"
 
 #Parse the input JSON strings
     $azureArcEntities = $azureArcJson | ConvertFrom-Json
     $crowdstrikeEntities = $crowdstrikeJson | ConvertFrom-Json
 
 #Extract hostnames
     $azureArcHostnames = $azureArcEntities | ForEach-Object { $_.Hostname }
     $crowdstrikeHostnames = $crowdstrikeEntities | ForEach-Object { $_.Hostname }
 
 Find missing hostnames
     $azureArcMissing = $crowdstrikeHostnames | Where-Object { $_ -notin $azureArcHostnames }
     $crowdstrikeMissing = $azureArcHostnames | Where-Object { $_ -notin $crowdstrikeHostnames }
 
 #Format the output for discrepancies
     $output = "Discrepancies between Azure Arc and Crowdstrike Hostnames:`n"
     $output += "Hostnames in Crowdstrike but not in Azure Arc:`n"
     $output += ($azureArcMissing -join "`n")
     $output += "`n`nHostnames in Azure Arc but not in Crowdstrike:`n"
     $output += ($crowdstrikeMissing -join "`n")
 
 Write-Output $output
 }
 catch {
 Write-Error "An error occurred: $_"
 }
 

The job continues to fail with the following Exception: Cannot process argument transformation on parameter 'azureArcJson'. Cannot convert value to type System.String. (Cannot convert value to type System.String. (Cannot convert value to type System.String.))

I am unsure what to do to get this script to work. Can anyone help me?

5
  • The issue is the json $azureArcJson you are passing to the script. Is the json a string or something else? The error says the json cannot be converted to a string. Commented Jun 14, 2024 at 17:26
  • Please format your post properly. Commented Jun 14, 2024 at 20:14
  • 1
    Purely as an investigation step, you could change the parameter type to object and then write some debug output containing the value type (write-host $azureArcJson.GetType().FullName) and it’s json representation (write-host ($azureArcJson | convert to-json -Deprh 2)) which won’t solve the problem but will give you a bit more information about what is being passed in, and that might help… Commented Jun 16, 2024 at 22:11
  • What does azureArcJson contains. Can you share it here. @SentinelAzure Commented Jun 17, 2024 at 4:14
  • Is it resolved or you still facing the same issue? @SentinelAzure Commented Jun 25, 2024 at 4:54

1 Answer 1

0

'azureArcJson'. Cannot convert value to type System.String:

Thanks @mclayton for pointed out in the right direction. When you pass input parameters $azureArcJson & $crowdstrikeJson as Json, the azure automation is considering them as objects but not strings. The expected input is strings that is why the error is being thrown.

To resolve it, use ConvertTo-Json PowerShell command as follows.

Add below lines of code in your script by keeping remaining script as same as you did.

$azureArcjsonc = $azureArcJson | ConvertTo-Json -Depth 3
$crowdstrikejsonc = $crowdstrikeJson | ConvertTo-Json -Depth 3

Complete script:

param (
 [Parameter(Mandatory=$true)]
 [string]$azureArcJson,
 [Parameter(Mandatory=$true)]
 [string]$crowdstrikeJson
 )
 
 try {
 Print the received JSON strings for debugging
 Write-Output "Received Azure Arc JSON string: $azureArcJson"
 Write-Output "Received Crowdstrike JSON string: $crowdstrikeJson"
 
     $azureArcjsonc = $azureArcJson | ConvertTo-Json -Depth 3
     $crowdstrikejsonc = $crowdstrikeJson | ConvertTo-Json -Depth 3
 

     $azureArcHostnames = $azureArcjsonc | ForEach-Object { $_.Hostname }
     $crowdstrikeHostnames = $crowdstrikejsonc | ForEach-Object { $_.Hostname }
 

     $azureArcMissing = $crowdstrikeHostnames | Where-Object { $_ -notin $azureArcHostnames }
     $crowdstrikeMissing = $azureArcHostnames | Where-Object { $_ -notin $crowdstrikeHostnames }
 

     $output = "Discrepancies between Azure Arc and Crowdstrike Hostnames:`n"
     $output += "Hostnames in Crowdstrike but not in Azure Arc:`n"
     $output += ($azureArcMissing -join "`n")
     $output += "`n`nHostnames in Azure Arc but not in Crowdstrike:`n"
     $output += ($crowdstrikeMissing -join "`n")
 
 Write-Output $output
 }
 catch {
 Write-Error "An error occurred: $_"
 }

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

This is unlikely to be the correct explanation, because PowerShell binds instance of any type to a [string] parameter, with the exception of arrays. E.g., & { [CmdletBinding()] param([string] $foo) "[$foo]" } ([pscustomobject] @{ bar = 1 }) works just fine.
Yes, I was mentioning about how the Azure Automation considering the variables with PowerShell stack runtime.
I'm not familiar with Azure, but your code cannot work as written: Even if the JSON input is converted to custom objects during parameter binding, your code still defines the parameters as [string], which is what caused the error to begin with. Passing a string to ConvertTo-Json is pointless - all you get is an "..."-enclosed, potentially escaped version of the same string. Furthermore, because the result is also a [string], the only property it has is .Length; something like $_.Hostname cannot work.
I agree with you that works in few scenarios. If that is the case, we need to ensure that the Json is correctly parsed into an object and then access the properties with their values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.