试图让 powershell 在 C#/Visual Studio 中工作 [英] Trying to get powershell working within C#/Visual Studio
问题描述
我试图学习如何在 C# 中使用 powershell我正在关注 本教程一个>
I was trying to learn how to use powershell within C# I was following this tutorial
但是,当我运行它时,出现此错误:
However when I run it I get this error:
未处理的异常.System.InvalidProgramException:公共语言运行时检测到无效程序.在 System.Management.Automation.PowerShell.Create()在 desktopAdmin365.Program.Main(String[] args) 在 C:\Users\ncox\source\repos\desktopAdmin365\Program.cs:line 11
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(进程 17464)退出,代码为 -532462766.
C:\Users\ncox\source\repos\desktopAdmin365\bin\Debug\netcoreapp3.0\desktopAdmin365.exe (process 17464) exited with code -532462766.
我确定在包含解决方案资源管理器中的引用/依赖项时我遗漏了一些东西,但我没有运气谷歌找到哪个
I am sure I'm missing something when including references/dependencies from the solutions explorer but I am not having any luck with google finding out which one
这是代码
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 还原的包并且可能不兼容的警告.
If you look at your dependencies in the project, you'll likely see a warning about the package being restored using .NETFramework and possibly not being compatible.
>
您的项目是 .NET Core,因此您可能需要安装 Powershell Core.
Your project is .NET core, so you might need to install Powershell Core instead.
或者,切换到 .net 框架解决方案,它会起作用.
Alternatively, switch to a .net framework solution and it will work.
这篇关于试图让 powershell 在 C#/Visual Studio 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!