PowerShell cmdlet Install-WindowsFeature called from C# errors and is not listed in the collections of commands

北城余情 提交于 2021-01-27 07:16:57

问题


I'm trying to install new features on a Windows Server 2012 system through PowerShell and C#. I am getting this error back when trying to call the Install-WindowsFeature cmdlet:

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

I can call other cmdlets though C# and even some of the other new ones. I did a Get-Command and Install-WindowsFeature, Remove-WindowsFeature, and Get-WindowsFeatures are not listed...but about 980 others are.

Why wouldn't I be able to call this cmdlet from my C# program, but when I go to PowerShell it calls it just fine?

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

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

回答1:


Try this command instead of Install-WindowsFeature in your code:

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




回答2:


Try compiling in x64. In Visual Studio go to menu ProjectPropertiesBuild - Platform Target = x64.



来源:https://stackoverflow.com/questions/13021547/powershell-cmdlet-install-windowsfeature-called-from-c-sharp-errors-and-is-not-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!