【发布时间】:2019-08-24 20:19:28
【问题描述】:
我尝试在 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# .net powershell namespaces