Powershell cmdlet Install-WindowsFeature从C#错误调用并未列入命令集合

问题描述:

我试图通过Powershell和C#在Windows 2012系统上安装新功能。尝试调用Install-WindowsFeature cmdlet时,我收到此错误。Powershell cmdlet Install-WindowsFeature从C#错误调用并未列入命令集合

“安装-WindowsFeature”一词不被识别为一个 cmdlet,函数,脚本文件或可操作的程序的名称...

我可以打电话给其他cmdlet虽然C#,甚至其他一些新的。我做了Get-CommandInstall-WindowsFeature,Remove-WindowsFeature,和Get-WindowsFeatures没有列出...但约980其他人。

任何线索为什么我不能从我的C#程序中调用此cmdlet,但是当我转到Powershell时,它称它为好?

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

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

这是什么模块?也许它在一个模块中,默认情况下不会通过'CreateRunspace'的重载导入。您可以将ps模块添加到您传递给'CreateRunspace'的另一个重载的'InitialSessionState'对象。 –

+0

它位于ServerManager模块中。我从来没有使用InitialSessionState对象,我将不得不考虑这一点。 – Matronix

+0

@ mikez尝试了你的建议,仍然是不行。 string [] modules = {“ServerManager”}; InitialSessionState iss = InitialSessionState.CreateDefault(); iss.ImportPSModule(modules); runspace = RunspaceFactory.CreateRunspace(iss); – Matronix

试试这个命令,而不是在你的代码安装-WindowsFeature:

调用表达式 “Powershell的-Command {导入模块ServerManager的;安装-WindowsFeature XXXX}”

尝试在x64中编译。在VS中,转到Project> Properties> Build - Platform Target = x64。