SentinelAzure
SentinelAzure

Reputation: 23

Powershell Script in Azure Automation issue

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?

Upvotes: 1

Views: 165

Answers (1)

Jahnavi
Jahnavi

Reputation: 8018

'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

Upvotes: 0

Related Questions