How To: Find all RDS session hosts

If you’re working in a large environment without a lot of management consoles to see the roles deployed in the environment you can do this easy with Active Directory and the magic of script blocks. The below script will loop through all the Windows servers found in AD and give a simple CSV displaying all the servers with the Remote Desktop Session Host role installed. This can be very helpful for System engineers wanting to get a better overview of where their RDS CALs are being used.

# Import the Active Directory module
Import-Module ActiveDirectory

# Define the Remote Desktop Session Host role name
$RDSSessionHostRoleName = "RDS-RD-Server"

# Get a list of all servers in Active Directory
$Servers = Get-ADComputer -Filter {OperatingSystem -Like "Windows server*"} -Properties Description

# Define the script block to run on the remote servers
$ScriptBlock = {
    param($RDSSessionHostRoleName)

    $roles = Get-WindowsFeature -Name $RDSSessionHostRoleName | Where-Object {$_.Installed}

    if ($roles) {
        # Return the server name and description
        [PSCustomObject]@{
            ServerName = $env:COMPUTERNAME
            Description = (Get-WmiObject -Class Win32_OperatingSystem).Description
        }
    }
}

# Run the script block on each remote server using Invoke-Command
$Results = Invoke-Command -ComputerName $Servers.Name -ScriptBlock $ScriptBlock -ArgumentList $RDSSessionHostRoleName

# Export the results to a CSV file
$Results | Export-Csv -Path "C:\Path\To\Output\File.csv" -NoTypeInformation

You May Also Like

About the Author: Phil

Leave a Reply

Your email address will not be published. Required fields are marked *