首页 > 解决方案 > .NET 中 PowerShell 的参考是什么?

问题描述

我尝试在 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();
}

标签: c#.netpowershellnamespaces

解决方案


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

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


推荐阅读