Running Powershell script in C # -ConvertToJson error

I would like to run shell scripts in C #.

Using a tutorial on running powershell scripts in C # from CodeProject ( http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C )

I can run the "most" ps scripts (they work fine on PS), but the one I need, I get an error when I try to use the -ConvertFrom-Json command.

Error in the script: the term "ConvertFrom-Json" is not recognized as the name of the cmdlet, function, script file or executable program. Check the spelling of the name or if the path was included, check the path and try again.

I'm new to this, so not sure if its outdated libraries or other dependencies are necessary for it to work?

+6
c # powershell
source share
1 answer

Since ConvertFrom-Json was introduced in Powershell 3.0, make sure your environment is also Powershell 3.0+. You can do this by running $Host.Version in the context of your C # code and looking at the returned Version object. It must have a major version of 3 or 4.

If it is 3.0+ because ConvertFrom-Json is enabled through the Microsoft.PowerShell.Utility module, make sure the module is loaded by running the Import-Module Microsoft.PowerShell.Utility in front of your ConvertFrom-Json. The module is probably loaded via normal session initialization scripts, but when executed from code, it may not be executed.

+6
source share

All Articles