Running Powershell script in c# -ConvertToJson error

Since ConvertFrom-Json was introduced in Powershell 3.0, ensure that the runspace that you are creating is also Powershell 3.0+. You can do this by executing $Host.Version in the context of your C# code and looking at the Version object returned. It should have a Major Version of 3 or 4.

If it is 3.0+, since ConvertFrom-Json is included via the module Microsoft.PowerShell.Utility, make sure that the module is loaded by doing Import-Module Microsoft.PowerShell.Utility before your ConvertFrom-Json. The module is probably getting loaded via your normal session initialization scripts, but when being executed from code, it may not be executing those.

Tags:

C#

Powershell