gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:48:58 25 4
gpt4 key购买 nike

我正在尝试通过 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-CommandInstall-WindowsFeatureRemove-WindowsFeatureGet-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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com