简体   繁体   中英

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

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();

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM