试图让 powershell 在 C#/Visual Studio 中工作

Trying to get powershell working within C#/Visual Studio

我正在尝试学习如何在 C# 中使用 powershell 我正在关注 this tutorial

然而,当我 运行 它时,我得到这个错误:

Unhandled exception. System.InvalidProgramException: Common Language Runtime detected an invalid program. at System.Management.Automation.PowerShell.Create() at desktopAdmin365.Program.Main(String[] args) in C:\Users\ncox\source\repos\desktopAdmin365\Program.cs:line 11

C:\Users\ncox\source\repos\desktopAdmin365\bin\Debug\netcoreapp3.0\desktopAdmin365.exe (process 17464) exited with code -532462766.

我确信在从解决方案资源管理器中包含 references/dependencies 时我遗漏了一些东西,但我没有任何运气 google 找出哪个

这是代码

using System;
using System.Management.Automation;
using System.Collections.ObjectModel;
namespace desktopAdmin365
{
    class Program
    {
        static void Main(string[] args)
        {
            using (PowerShell PowerShellInstance  = PowerShell.Create())
            { 
                PowerShellInstance.AddScript("param($param1) $d = get-date; $s = 'test string value'; " +
                "$d; $s; $param1; get-service");
            }
        }
    }
}

如果您查看项目中的依赖项,您可能会看到一条警告,指出正在使用 .NETFramework 恢复包并且可能不兼容。

您的项目是 .NET 核心,因此您可能需要安装 Powershell 核心。

或者,切换到 .net 框架解决方案,它会起作用。