我正在尝试通过 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-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();
最佳答案
在您的代码中尝试此命令而不是 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/