简体   繁体   English

使用C#调用Powershell服务器Commandlet到Enable-WindowsOptionalFeature netfx3

[英]Using C# to invoke powershell server commandlets to Enable-WindowsOptionalFeature netfx3

I am working on a WiX installer that uses Burn and therefore has a .Net requirement. 我正在使用Wi-Fi安装程序的WiX安装程序,因此具有.Net要求。 Bundling the prerequisite version of .Net does not work on server OS's as they require the role manager to be used. 捆绑必备版本的.Net在服务器操作系统上不起作用,因为它们要求使用角色管理器。 The fact that it's part of a WiX install is not really of overwhelming importance it's there in case there are other methods to help me accomplish my task. 它确实是WiX安装的一部分,但并不是很重要,因为如果有其他方法可以帮助我完成任务,那么它就存在。 I am running/testing the code below as a standalone .Net Console application. 我正在作为独立的.Net Console应用程序运行/测试以下代码。 After failing using this method in order to work around this I wrote the following use powershell and "Enable-WindowsOptionalFeature". 为了解决此问题而无法使用此方法后 ,我编写了以下use powershell和“ Enable-WindowsOptionalFeature”。

    static void Main(string[] args)
    {

        PowerShell ps = PowerShell.Create();
        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();

        Pipeline pipeline = runspace.CreatePipeline();

        Command addDotNet = new Command("Enable-WindowsOptionalFeature");
        addDotNet.Parameters.Add("-FeatureName netfx3 -Online -All");
        pipeline.Commands.Add(addDotNet);
        Collection<PSObject> log = pipeline.Invoke();

} }

When I run it this window pops up. 当我运行它时,将弹出该窗口。
Windows Server roles and features cannot be automatically installed or uninstalled via the Windows Features Control Panel. Windows Server角色和功能无法通过Windows功能控制面板自动安装或卸载。 在此处输入图片说明

Seeing as how I figured I was already using the equivalent of "Server Manager cmdlets" I'm not sure what it's actually asking for. 就像我认为的那样,我已经在使用等效的“ Server Manager cmdlet”,所以我不确定它的实际要求。 So the two part question is 1) What is the proper way to work around this issue. 因此,两部分的问题是:1)解决此问题的正确方法是什么? 2)How do I keep it quiet during install. 2)如何在安装过程中保持安静。

PS: If this question really belongs to Server Fault let me know. PS:如果这个问题确实属于服务器故障,请告诉我。

Enable-WindowsOptionalFeature is part of the DISM, it would be used to create a windows image to deploy to a new system Enable-WindowsOptionalFeature是DISM的一部分,它将用于创建Windows映像以部署到新系统

Add-WindowsFeature is the one you are looking for, it adds a feature to the current instance of Windows Server. Add-WindowsFeature是您要寻找的一项,它将功能添加到Windows Server的当前实例。 It is in the ServerManager powershell module, which is only available on Windows Server (not on Windows 7) 它在ServerManager powershell模块中,仅在Windows Server上可用(不适用于Windows 7)

All I'm going to say is based on common sense, rather than on prior experience, so sorry if this is not relevant. 我要说的只是基于常识,而不是基于先前的经验,如果这不相关,请抱歉。

You seem to be able to be on Windows Server 2012 as only this version has Windows ADK that you are using. 您似乎可以在Windows Server 2012上运行,因为只有此版本具有要使用的Windows ADK。 Windows 8 (client os )nd Windows Server 2012 (server os) have differen ways of enabling feature. Windows 8(客户端操作系统)和Windows Server 2012(服务器操作系统)具有不同的启用功能方式。 Those surfaced as Optional Features in Windows 8 but as Server Roles/Features in the server. 这些在Windows 8中作为“可选功能”出现,但在服务器中作为“服务器角色/功能”出现。

Note, that contrary to what you say, the bundle does not work not because it's a server OS it does not work because it's included with the OS and need to be enabled. 请注意,与您所说的相反,该捆绑软件不能运行,不是因为它是服务器操作系统,而是因为它包含在OS中并且需要启用,因此它不起作用。 This applies both to server OS (Win 2012) and client OS (Windows 8). 这适用于服务器操作系统(Win 2012)和客户端操作系统(Windows 8)。

It's just that you need to use different methods for Windows 8 and Win 2012. 只是您需要针对Windows 8和Win 2012使用不同的方法。

For Windows 8 your method could work. 对于Windows 8,您的方法可以使用。 For Windows 2012 the help you are seeing suggest that you use Add-WindowsFeature cmdlet. 对于Windows 2012,您所看到的帮助建议您使用Add-WindowsFeature cmdlet。

Basically, the command fails, because the component you need to install is not present in the component store. 基本上,该命令会失败,因为您需要安装的组件不在组件存储中。 The administrator needs to insert the install media. 管理员需要插入安装媒体。 Perhaps you could include the respective file(s) with your installer to avoid that manual step, but I've never tried that and it might also pose a licensing problem. 也许您可以在安装程序中包含相应的文件,以免执行手动操作,但我从未尝试过这样做,它可能还会引起许可问题。

For Server 2012 Microsoft decided to remove the .NET framework 3.5 from the component store by default. 对于Server 2012,Microsoft决定默认情况下从组件存储中删除.NET Framework 3.5。 It's still present on the install media, just not spooled onto the disk during installation. 它仍然存在于安装介质上,只是在安装过程中未假脱机到磁盘上。 I wrote a rant about this in my blog a while ago. 不久前,我在博客中写了一篇关于这件事的言论

I added some additional logging/writeline statements while trying to figure out where things were broken in the server 2012 instance. 在尝试找出服务器2012实例中发生问题的位置时,我添加了一些其他日志记录/写入语句。 I realized that those debug lines were not output to the console at all. 我意识到那些调试行根本没有输出到控制台。

It turns out that I was not READING the pop-up closely enough. 事实证明,我没有足够仔细地阅读弹出窗口。 The complaint was not about using the PS commandlets, but a complaint about trying to install .Net 3.5 to run my app. 投诉不是关于使用PS Commandlet的投诉,而是关于试图安装.Net 3.5来运行我的应用程序的投诉。 My app was targeting .Net 3.5 so the message was about the attempt to automatically install .Net 3.5 我的应用程序以.Net 3.5为目标,因此该消息是有关尝试自动安装.Net 3.5的消息

May this question be more than a testament to my momentary blindness and stupidity, but also serve as commentary on the importance of paying attention to detail and reading the error understanding why it's occurring. 这个问题不仅可以证明我一时的失明和愚蠢,还可以作为评论着重于细节并阅读错误的原因的重要性的注释。 :) :)

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

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