简体   繁体   中英

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.

In Windows Server 2012 the Win32_ServerFeature class has been deprecated and does not include features and roles new to 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.

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.

The way i would consider doing it is by using a piece of PowerShell script and then 'playing' with the output in C#.

If you add a reference to the following item you will be able to interact with PowerShell scripts in C# :

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

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

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

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 :

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

Hope this helps!

I would rather recommend you to check Windows Registry. For example for components of IIS you can check HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\InetStp\\Components folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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