简体   繁体   English

检测 Windows Server 2012 上安装了哪些服务器角色

[英]Detecting what Server Roles are installed on Windows Server 2012

In Windows Server 2008 you could programmatically detect server Features and Roles using WMI and the Win32_ServerFeature class.在 Windows Server 2008 中,您可以使用 WMI 和 Win32_ServerFeature 类以编程方式检测服务器功能和角色。

In Windows Server 2012 the Win32_ServerFeature class has been deprecated and does not include features and roles new to 2012.在 Windows Server 2012 中,Win32_ServerFeature 类已被弃用,并且不包括 2012 的新功能和角色。

As far as i can tell Win32_ServerFeature class has been replace by Server Manager Deployment and there are no examples of how to use it.据我所知Win32_ServerFeature类已被服务器管理器部署取代,并且没有关于如何使用它的示例。

I have search online an can't find any info on it other than the docs that are no help.我在网上搜索过,除了没有帮助的文档外,找不到任何关于它的信息。

Any assistance would be appreciated, i am developing in c# in a 4.5 Dot Net Framework Application.任何帮助将不胜感激,我正在 4.5 点网络框架应用程序中使用 c# 进行开发。

The way i would consider doing it is by using a piece of PowerShell script and then 'playing' with the output in C#.我会考虑这样做的方法是使用一段 PowerShell 脚本,然后在 C# 中“播放”输出。

If you add a reference to the following item you will be able to interact with PowerShell scripts in C# :如果您添加对以下项目的引用,您将能够在 C# 中与 PowerShell 脚本进行交互:

System.Management.Automation系统.管理.自动化

Then use the following using statements to delve into and interact with the features of this :然后使用以下using语句来深入研究 this 的功能并与之交互:

using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces

The following script will create a nice sub that will take a PowerShell command and return a readable string, with each item (in this case, a role) added as a new line :以下脚本将创建一个不错的子程序,它将采用 PowerShell 命令并返回一个可读字符串,并将每个项目(在本例中为一个角色)添加为一个新行:

private string RunScript(string scriptText)
{
// create a Powershell runspace then open it

Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();

// create a pipeline and add it to the text of the script

Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);

// format the output into a readable string, rather than using Get-Process
// and returning the system.diagnostic.process

pipeline.Commands.Add("Out-String");

// execute the script and close the runspace

Collection<psobject /> results = pipeline.Invoke();
runspace.Close();

// convert the script result into a single string

StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}

return stringBuilder.ToString();
}

Then you can pass the following PowerShell command to the script and receieve the output like so :然后,您可以将以下 PowerShell 命令传递给脚本并像这样接收输出:

RunScript("Import-module servermanager | get-windowsfeature");

Alternatively you could just run this PowerShell command from a C# script and then read the output text file from C# when the script has finished processing :或者,您可以从 C# 脚本运行此 PowerShell 命令,然后在脚本完成处理后从 C# 读取输出文本文件:

import-module servermanager | get-windowsfeature > C:\output.txt

Hope this helps!希望这可以帮助!

I would rather recommend you to check Windows Registry.我宁愿建议您检查 Windows 注册表。 For example for components of IIS you can check HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\InetStp\\Components folder例如对于 IIS 的组件,您可以检查HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\InetStp\\Components文件夹

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

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