我試圖通過Powershell和C#在Windows 2012系統上安裝新功能。嘗試調用Install-WindowsFeature
cmdlet時,我收到此錯誤。Powershell cmdlet Install-WindowsFeature從C#錯誤調用並未列入命令集合
「安裝-WindowsFeature」一詞不被識別爲一個 cmdlet,函數,腳本文件或可操作的程序的名稱...
我可以打電話給其他cmdlet雖然C#,甚至其他一些新的。我做了Get-Command
和Install-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();
這是什麼模塊?也許它在一個模塊中,默認情況下不會通過'CreateRunspace'的重載導入。您可以將ps模塊添加到您傳遞給'CreateRunspace'的另一個重載的'InitialSessionState'對象。 –
它位於ServerManager模塊中。我從來沒有使用InitialSessionState對象,我將不得不考慮這一點。 – Matronix
@ mikez嘗試了你的建議,仍然是不行。 string [] modules = {「ServerManager」}; InitialSessionState iss = InitialSessionState.CreateDefault(); iss.ImportPSModule(modules); runspace = RunspaceFactory.CreateRunspace(iss); – Matronix