Skip to main content

Use PowerShell to list all server roles and the services that they are using

This was a good question that came up in my Windows Server 2008 Fundamentals course.  The student needed to list all of the server roles on the local server and the services that each role utilizes for a collage class she was taking.  This was actually an easy one to resolve.

Import-Module ServerManager

Get-WindowsFeature -ErrorAction Stop |

 Where-Object {($_.installed -eq $true) -and ($_.SystemService -ne $Null)} |

 Select-Object -Property Name, SystemService

 

This code requires the ServerManager module.  Since it was being executed on the local Windows 2008 R2 server, there was not an issue.  What about executing this code against remote clients? At this point, the code is only somewhat reusable.  One of my students worked in an environment of over 3000 servers. How could it be usable for him? The help file for Get-WindowsFeature does not show any built in remoting technology.  For this reason, we need to enable PowerShell Remoting.

 

PowerShell remoting is turned on by default on Server 2012 and Windows 8.  From Server 20008 R2 and Windows 7, follow these instructions to turn on PowerShell remoting in your domain using Group Policy. Below is the code to all this information to be recovered for remote clients.

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

 

Function Get-RoleServiceUsage

{

[cmdletbinding()]

Param(

    [Parameter(Mandatory=$True,

               ValueFromPipeline=$True,

               Position=0)]

               $Name = "LocalHost"

)              

    Begin {}

   

    Process

    {

        ForEach ($Item in (Invoke-Command -ComputerName $Name -ErrorAction Continue `

            -ScriptBlock {

                Param ($Name)

                Try

                {

                    Import-Module ServerManager -ErrorAction Stop;

                    Get-WindowsFeature -ErrorAction Stop |

                    Where-Object {($_.installed -eq $true) -and ($_.SystemService -ne $Null)} |

                    Select-Object -Property Name, SystemService

                }

                Catch

                {

                    Write-Host "Error: No ServerManager on Server: " `

                    -NoNewline -ForegroundColor Red -BackgroundColor DarkRed

                    Write-HOst "$Name"

                       

                }

            } -args $Name ))

       

       

        {

            Write-Output $Item

        }

    }

   

    End {}

 

<#

.SYNOPSIS

Returns the services being used by server roles.

 

.DESCRIPTION

Returns an object representing each role installed on the server and the services

that are being used being used by each role.

 

.PARAMETER Name

The Name of the server to recover role and service information from.

 

.EXAMPLE

"PHX-DC1" | Get-RoleServiceUsage

 

Name               : ADDS-Domain-Controller

SystemService      : {ntds*, netlogon*, kdc*, ntfrs...}

PSComputerName     : PHX-dc1

RunspaceId         : cd61ea87-1899-484b-887f-29180f7134de

PSShowComputerName : True

 

Name               : DNS

SystemService      : {dns*}

PSComputerName     : PHX-dc1

RunspaceId         : cd61ea87-1899-484b-887f-29180f7134de

PSShowComputerName : True

 

Returns the roles and their respective services from a single client.

 

.EXAMPLE

PHX-DC1" | Get-RoleServiceUsage |

    ForEach -process {Write-Host $_.Name -BackgroundColor Gray;

     $_ |Select-Object -ExpandProperty SystemService}

    

Returns a list of both the roles and ther services, with the roles

highlighted.

 

.NOTES

Requires the ServerManager module on each client this cmdlet is

executed against.

 

#>

} # End Function: Get-RoleServiceUsage

 

Line 1 is the declaration of our function, and the name of the cmdlet that we are creating: Get-RoleServiceUsage.

Lines 2 and 82 encapsulate the code of the function.

Line 3 enables this function to become a cmdlet by allowing the usage of the PowerShell Default Parameters.  To read more about the CmdletBinding attribute, take a look at the PowerShell help file About_Functions_Advanced_Parameters.

Line 4 starts our parameter block that ends at line 9

Lines 5 – 8 declares our only parameter, $Name.  This will be the name of a server that we want to retrieve information from.  Notice that the parameter has a default value of “LocalHost” and can accept input via the PowerShell pipeline.  This means we can pipe a comma seperated list of server names into this cmdlet so we can reach out to many servers easily.

Line 10 holds an empty BEGIN block.  This block is not used in this code.

Lines 12 – 37 holds our PROCESS block.  The Process block will execute once for each name piped to our cmdlet.

Line 14 starts a very complex ForEach statement.  Essentially the collection of objects that we are recovering from the servers are gathered starting with (Invoke-Command in line 14 and ends on line 31.  The results are stored in $Item for later processing.  The Invoke-Command cmdlet is used because we can send code to other clients using the –ComputerName parameter.

Line 15 starts the –ScriptBlock portion of the Invoke-Command cmdlet.  Everything inside of the curly braces from line 15 to line 31 is sent to the remote client.

Line 16 allows for a parameter to be sent the the remote client.  This parameter is called $Name and the actual value is sent from the –Args parameter on line 31.

Line 17 sets up some error handling.  We are looking for a potential error on Lines 19 or 20.

Line 19 will load the ServerManager module on the remote client.  If this fails, the Catch block on line 24 will execute.

Lines 20-22 is one continuous PowerShell command.  The first cmdlet is Get-WindowsFeature.  This cmdlet came from the ServerManager module and it will return a list of objects representing all the roles and features that are available to the remote server.

Line 21 filters the list of objects se we only will be working with objects that are both installed, and use a service as part of their functionality.

Line 22 filters out extra properties and returns to us an object containing the role’s name and the list of services that particular role uses.  This is the information that is stored in the variable $Item.

Lines 24 – 30 will handle any errors that occur on lines 19 and 20.  This block will execute should you accidental include clients in the search which do not have access to the ServerManage module.  Should RSAT be installed, you will get results.  This is another avenue for you to look into for additional error handling.

Lines 34-36 execute for each object recovered in the variable $Item from line 14.  In this case, the object is simply placed in the PowerShell pipeline.

Line 39 is the END block of our function.  It is not used.

Lines 42 – 82 is the help file for this function.

Line 83 ends the function.

 

Once you load this function into memory, you can access the help file by executing:

Get-Help Get-RoleServiceUsage –Detailed

Take a look at the examples to learn how to use it.

Comments

Popular posts from this blog

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the mapped drive. If the