Azure Batch Needs to be Updated to PowerShell Version 7+

Viewed 144

I am running a Custom Activity in Azure Data Factory. We are using the Test-Json command to validate JSON data against a schema. This command works perfectly find in PowerShell version 7+ but will fail in version 5.1.

For testing purposes, if I run the below command (directly from the Microsoft documentation) it fails when using the Custom Activity in Data Factory.

"{'name': 'Ashley', 'age': 25}" | Test-Json

If I run the below command, it show that we are using PowerShell version 5.1.

$PSVersionTable

Is there anyway to upgrade to version 7.1 so these batch file will process successfully?

1 Answers

Two potential options:

Modify your task to use ConvertFrom-Json with a try-catch block instead of Test-Json, as described here.

As you've already pointed out, Test-Json comes with PowerShell 7.1 (part of PowerShell Core), but it doesn't come with PowerShell 5.1 (part of Windows PowerShell), which many Windows images have installed by default. ConvertFrom-Json, however, does come with PowerShell 5.1 and can provide the same functionality as Test-Json with a few tweaks.

Install PowerShell Core 7.1 as a start task, and then invoke your command as normal.

The start task syntax to complete this installation would be cmd /c "powershell.exe -c "iex ""&" { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI -Quiet""", based off the one-liner described here and modified to fit the Batch syntax described here. This will install the latest version of PowerShell Core, which will have Test-Json included. When creating tasks, be sure to invoke PowerShell Core using pwsh.exe, and not powershell.exe, which is for Windows PowerShell.

Related