简体   繁体   English

PowerShell cmdlet Install-WindowsFeature从C#错误调用,未列在命令集合中

[英]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#. 我正在尝试通过PowerShell和C#在Windows Server 2012系统上安装新功能。 I am getting this error back when trying to call the Install-WindowsFeature cmdlet: 我在尝试调用Install-WindowsFeature cmdlet时收到此错误:

The term 'Install-WindowsFeature' is not recognized as the name of a cmdlet, function, script file, or operable program... 术语“Install-WindowsFeature”不被识别为cmdlet,函数,脚本文件或可操作程序的名称......

I can call other cmdlets though C# and even some of the other new ones. 我可以通过C#调用其他cmdlet,甚至可以调用其他一些新cmdlet。 I did a Get-Command and Install-WindowsFeature , Remove-WindowsFeature , and Get-WindowsFeatures are not listed...but about 980 others are. 我没有列出Get-CommandInstall-WindowsFeatureRemove-WindowsFeatureGet-WindowsFeatures ......但是大约有980个。

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? 为什么我不能从我的C#程序中调用这个cmdlet,但是当我转到PowerShell时,它调用它就好了?

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: 在代码中尝试使用此命令而不是Install-WindowsFeature:

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

Try compiling in x64. 尝试在x64中编译。 In Visual Studio go to menu ProjectPropertiesBuild - Platform Target = x64. 在Visual Studio中,转到菜单项目属性构建 - 平台目标 = x64。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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