【发布时间】:2017-03-31 01:20:01
【问题描述】:
我尝试从我的 c# 应用程序中执行以下 PowerShell 命令。
$check = Get-WindowsOptionalFeature -Online -FeatureName WCF-HTTP-Activation45
if ($check.Installed -eq 'False') {
Enable-WindowsOptionalFeature -Online -FeatureName WCF-HTTP-Activation45
}
但是当我运行它时,PowerShell 会引发以下异常:
在模块“Dism”中找到“Get-WindowsOptionalFeature”命令,但无法加载该模块。有关更多信息,请运行“Import-Module Dism”
但是,当我从 PowerShell 窗口运行相同的脚本时,它工作得很好。我的问题的根源是什么?我的操作系统是 Windows 10。
PowerShell 版本:5 - 1 - 14393 - 953
C#代码:
const string psScript = @"$check = Get-WindowsOptionalFeature -Online -FeatureName WCF-HTTP-Activation45
if ($check.Installed -eq 'False') {
Enable-WindowsOptionalFeature -Online -FeatureName WCF-HTTP-Activation45
}";
using (var PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript(psScript);
var PSOutput = PowerShellInstance.Invoke();
if (PowerShellInstance.Streams.Error.Count > 0)
{
foreach (var err in PowerShellInstance.Streams.Error)
logger.LogError(err.ToString());
}
foreach (var outputItem in PSOutput)
{
if (outputItem != null)
logger.LogInfo(outputItem.ToString());
}
}
【问题讨论】:
-
你有解决办法吗?
标签: c# powershell