c# - 从 C# 错误调用的 PowerShell cmdlet Install-WindowsFeature 未在命令集合中列出

标签 c# powershell visual-studio-2012 windows-server-2012

我正在尝试通过 PowerShell 和 C# 在 Windows Server 2012 系统上安装新功能。尝试调用 Install-WindowsFeature cmdlet 时出现此错误:

The term 'Install-WindowsFeature' is not recognized as the name of a cmdlet, function, script file, or operable program...

我可以通过 C# 调用其他 cmdlet,甚至可以调用其他一些新的 cmdlet。我做了一个 Get-CommandInstall-WindowsFeatureRemove-WindowsFeatureGet-WindowsFeatures 没有列出...但还有大约 980 人。

为什么我不能从我的 C# 程序中调用此 cmdlet,但当我转到 PowerShell 时它调用它就可以了?

runspace = RunspaceFactory.CreateRunspace();
runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Install-WindowsFeature");
Collection<PSObject> output = pipeline.Invoke();

最佳答案

在您的代码中尝试此命令而不是 Install-WindowsFeature:

Invoke-Expression "Powershell -Command {Import-Module ServerManager ; Install-WindowsFeature XXXX}"

关于c# - 从 C# 错误调用的 PowerShell cmdlet Install-WindowsFeature 未在命令集合中列出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13021547/

相关文章:

c# - 如何使用 ASP.NET 访问 FaceBook Ads API

powershell - Azure PowerShell 将无法连接

opencv - 带有 OpenCV 2.4.10 版 DLL 的 Visual Studio 2012 缺少错误

powershell - 是否可以在 PowerShell session 之间传递 com 对象的实时实例

powershell - PowerShell-使用WebDAV下载文件

powershell - 使用 PowerShell 自定义 T4 脚手架

c# - 哪个本地数据库适合Windows 8 Store Apps?

c# - 如何从另一个表中的另一列等于我的参数值的表中选择数据?

c# - 如何验证字段长度

c# - 读取 Ctrl + 直到从键盘中找到非数字