如何在 Windows 2008 R2 和 Windows 7 上以编程方式启用应用程序服务器角色

How to enable Application Server role programmatically on Windows 2008 R2 and Windows 7

作为安装过程的一部分,我需要启用应用程序服务器角色。我需要这样做才能获得应用程序所需的分布式事务功能。通过从 C# 应用程序执行 dism.exe,我可以在 Windows 2012 服务器上轻松完成此操作。这是执行的命令。

dism /Online /Enable-Feature /FeatureName:Application-Server /FeatureName:AS-NET-Framework /FeatureName:AS-Ent-Services /FeatureName:AS-Dist-Transaction /FeatureName:AS-Incoming-Trans /FeatureName:AS-Outgoing-Trans

但是当我尝试在 Windows 2008 R2 服务器或 Windows 7 上执行此操作时,它失败了,表明 DISM 无法识别这些功能。如果我在这些机器上使用 运行 命令列出功能 (Get-Features),它们确实不可用。即使我使用角色管理器手动启用应用程序服务器角色并列出 DISM 中可用的功能,它们也没有列出,或者任何其他名称都代表应用程序服务器角色。

如何使用 C# 以编程方式在 Windows 2008 R2 服务器和 Windows 7 上启用应用程序服务器角色?该解决方案不必使用 DISM。我正在寻找任何选项。

所以事实证明,即使服务器管理器在 Windows 2008 R2 上已贬值,您仍然可以使用它来启用角色。这是我用来启用它的命令:

ServerManagerCmd -install Application-Server AS-Ent-Services AS-Dist-Transaction AS-Incoming-Trans AS-Outgoing-Trans

您将收到以下警告,但它有效。

Servermanagercmd.exe is deprecated, and is not guaranteed to be supported in future releases of Windows. We recommend that you use the Windows PowerShell cmdlets that are available for Server Manager.

那么如何在 Window 7 上启用它?你不能,因为它是一个客户端操作系统,这些角色不存在。但最终安装此角色的原因是为了安装和配置分布式事务。结果 Windows 7 分布式事务协调器已经可用,您只需配置它。