检测哪些服务器角色都安装在Windows Server 2012上角色、服务器、安装在、Server

2023-09-03 03:28:52 作者:熬夜等死

在Windows Server 2008中,你可以编程方式检测使用WMI和Win32_ServerFeature类服务器功能和角色。

In Windows Server 2008 you could programmatically detect server Features and Roles using WMI and the Win32_ServerFeature class.

在Windows Server 2012中的Win32_ServerFeature类已经去precated,不包括功能和角色的新的2012年。

In Windows Server 2012 the Win32_ServerFeature class has been deprecated and does not include features and roles new to 2012.

据我可以告诉Win32_ServerFeature类已由服务器管理器部署取代和有如何使用它没有例子

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.

我在网上搜索了找不到比这是没有帮助的文档之外的任何信息。

I have search online an can't find any info on it other than the docs that are no help.

任何援助将AP preciated,我正在开发中C#4.5点NET框架的应用。

Any assistance would be appreciated, i am developing in c# in a 4.5 Dot Net Framework Application.

推荐答案

我会考虑这样做的方法是使用了一块PowerShell脚本,然后在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# :

System.Management.Automation

System.Management.Automation

然后使用下面的使用的语句来深入探讨,并与此功能交互:

Then use the following using statements to delve into and interact with the features of this :

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

下面的脚本将创建一个漂亮子,将采取一个PowerShell命令,并返回一个可读的字符串,每个项目(在此情况下,角色)加入作为新行:

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 :

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();
}

然后你可以通过下面的PowerShell命令的脚本,receieve输出像这样:

Then you can pass the following PowerShell command to the script and receieve the output like so :

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

另外,您可以只从一个C#脚本运行此PowerShell命令,然后读取C#中的输出文本文件,在脚本完成处理:

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 :

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

希望这有助于!

Hope this helps!

 
精彩推荐
图片推荐