繁体   English   中英

.NET中PowerShell的参考是什么?

[英]What is the reference for PowerShell in .NET?

我试图在C#中运行PowerShell脚本。 但这会引发错误:

PowerShell doesn't available in current context. 

我在谷歌搜索,但没有得到解决方案,请帮忙。 这是我的代码:

string text = System.IO.File.ReadAllText(@"C:\Program Files (x86)\Backup Reporter\Required\edit_website.ps1");    
using (PowerShell PowerShellInstance = PowerShell.Create())
{
    // use "AddScript" to add the contents of a script file to the end of the execution pipeline.
    // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
    PowerShellInstance.AddScript(text);

    if (PowerShellInstance.Streams.Error.Count > 0)
    {
        Console.Write("Error");
    }
    Console.ReadKey();
}

您需要下载System.Management.Automation并将其附加到您的项目中:

https://www.nuget.org/profiles/PowerShellTeam

从VS Nuget Manager下载System.Management.AutomationSystem.Management.Automation.Runspaces包并将其添加到您的项目。

https://powershell.org/forums/topic/executing-powershell-from-c/

Windows PowerShell SDK 1.1.0中存在PowerShell类,您可以使用此NuGet包将其添加到项目中: https : //www.nuget.org/packages/System.Management.Automation/

PM> Install-Package System.Management.Automation -Version 6.2.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM