c# - .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();
}
解决方案
您需要下载 System.Management.Automation 并将其附加到您的项目中:
推荐阅读
- asp.net-mvc-5 - How to set up IIS 10 for MVC 5?
- android - 发现多个文件具有操作系统独立路径“META-INF/sdk_release.kotlin_module”
- networking - 如何从 ns3 中的 QueueDiscItem 中提取 IP 地址?
- python - 如何使 Google AI Platform 的 BASIC_GPU 规模层上的 GPU 对我的 tensorflow 程序可见?
- xcode - 在终端中使用 codesign 命令签名后,使用 Unity 构建的 macOS 应用程序在启动时挂起
- css - 如何强制 material-ui 生成!重要的 css 样式以防止 WordPress/Gutenberg css 样式冲突?
- java - BiometricPrompt.AuthenticationResult 中的 CryptoObject 始终为 null
- java - Keycloak 示例 - 方法 getValue() 未为 UserCredentialModel 类型定义
- c# - 带有 SpecFlow 挂钩的 NuGet 包
- swift - 如何在 Swift4 中不使用 inputIntensity 参数控制 CIFilter 的强度?