2012-10-22 12 views
3

我試圖通過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

回答

0

試試這個命令,而不是在你的代碼安裝-WindowsFeature:

調用表達式 「Powershell的-Command {導入模塊ServerManager的;安裝-WindowsFeature XXXX}」

0

嘗試在x64中編譯。在VS中,轉到Project> Properties> Build - Platform Target = x64。