Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 718
Dan Green Avatar asked Mar 23 '13 01:03

Dan Green


People also ask

How do I see what roles are installed on a server?

The PowerShell Get-WindowsFeature command—or, more properly, cmdlet—can retrieve a list of Windows features, including server roles, that are installed on a server or workstation running Windows, making it a handy tool for server admins.

How do I see installed roles and features?

The get-windowsfeature PowerShell command will get information about installed and available features and roles. As you can see in the screenshot above the command gets the display name, name, and the install state of services and roles on my local computer.

How do I check Windows roles?

In Service Manager, when you click Administration, expand Security, and then click User Roles, a User Roles pane displays a list of user roles.


1 Answers

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!

like image 131
chrismason954 Avatar answered Nov 08 '22 21:11

chrismason954