0

I'm trying to run a PS script from my C# project with target framework = .Net Core 2.1

I tried this:

        PowerShell ps = PowerShell.Create();
        ps.AddScript(File.ReadAllText(Properties.PATHTOSCRIPT)).Invoke();

with the <PackageReference Include="Cake.Powershell" Version="1.0.1" /> but this resulted in an error:

System.InvalidProgramException: 'Common Language Runtime detected an invalid program.'

Any suggestions how to run a PS script in C#?

mhu
  • 17,720
  • 10
  • 62
  • 93
0brine
  • 450
  • 5
  • 18
  • `Cake.Powershell` is an extension to the cake build system. This package is not intended for what you use it. – PMF Jul 15 '21 at 13:02
  • There's a package named `Microsoft.PowerShell.SDK` that's probably more suitable for your needs (I haven't tried it, though) – PMF Jul 15 '21 at 13:04
  • @PMF i removed the cake package and installed the other one but there is no Powershell type `The type or namespace name 'PowerShell' could not be found (are you missing a using directive or an assembly reference?)` – 0brine Jul 15 '21 at 13:12
  • As said, I haven't tested it. Maybe you start reading about it here: https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md#where-do-i-get-the-powershell-core-sdk-package – PMF Jul 15 '21 at 14:50
  • There is a really good post around here somewhere (I think from mklement0) that explains the different runtimes needed for different version of .NET. In your case, for .Net Core 2.1, you can install Microsoft.PowerShell.SDK ver 6.2.7 `` – Daniel Jul 15 '21 at 19:02
  • Found it - https://stackoverflow.com/a/58211901/11954025 (P.S., I don't know why VS suggests installing Cake instead of the actual runtimes...) – Daniel Jul 15 '21 at 19:07

0 Answers0