Determining which server roles are installed on Windows Server 2012

In Windows Server 2008, you can programmatically define server functions and roles using WMI and the Win32_ServerFeature class.

In Windows Server 2012, the Win32_ServerFeature class is deprecated and does not include the features and roles new in 2012.

As far as I can tell, the Win32_ServerFeature class has been replaced with a Server Manager Deployment , and there are no examples of how to use it.

I have a search on the Internet, and can not find any information about this, except for documents that do not help.

Any help would be appreciated, I am developing in C # in app 4.5 Dot Net Framework.

+7
source share
1 answer

How I could do this is to use a piece of PowerShell script and then β€œplay” with the output in C #.

If you add a link to the following element, you can interact with PowerShell scripts in C #:

System.Management.Automation

Then use the following instructions 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 good sub element that will take a PowerShell command and return a readable line, with each element (in this case, the role) added to 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 get the output like this:

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

Alternatively, you can simply run this PowerShell command with a C # script and then read the output text file from C # when you finish processing the script:

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

Hope this helps!

+8
source

All Articles