PowerShell: Find Windows RDS Roles and Their Licensing Servers

# Get TS Licensing Servers (Enterprise or AD Registered)
$termLicenseServers=Get-ADGroupMember -Identity "Terminal Server License Servers"
$termLicenseServers|Select-Object -Property @{label='computername';expression={[regex]::match($_.DistinguishedName,'([\w\d-]+)(?=,OU=)').Value}}

# Get TS Connection Points with Licensing Role Installed (may or may not be registered on the domain)
$termServers=Get-ADObject -Filter {objectClass -eq 'serviceConnectionPoint' -and Name -eq 'TermServLicensing'}
$termServers|Select-Object -Property @{label='computername';expression={[regex]::match($_.DistinguishedName,'([\w\d-]+)(?=,OU=)').Value}}

# Check localhost for TS Licensing Servers
$tsSettings=gwmi -namespace "Root/CIMV2/TerminalServices" Win32_TerminalServiceSetting 
$tsSettings.GetSpecifiedLicenseServerList().SpecifiedLSList

# Find RDS Servers and their Licensing Configs
$servers=Get-ADComputer -Filter {OperatingSystem -Like '*Windows Server*' -and Enabled -eq $true}
$results=@()
$noResults=@()
$initCount=0
$serversCount=$servers.Count
write-host "There are $serversCount servers to check..."
foreach($server in $servers.DNSHostName){
	$initCount++
	write-host "$initCount of $serversCount`: $server"
	$rdsRole=try{Get-WindowsFeature RDS-RD-Server -ComputerName $server -EA Ignore}catch{$null}	
	if($rdsRole){
		$rdsRoleInstalled=$rdsRole.InstallState -eq 'Installed'
		if($rdsRoleInstalled){
		$rdsLicenseServer=try{		
			$tsSettings=gwmi -namespace "Root/CIMV2/TerminalServices" Win32_TerminalServiceSetting -computername $server
			$tsSettings.GetSpecifiedLicenseServerList().SpecifiedLSList
		}catch{
			'not configured'
		}
		$result=@{
			computername=$server
			rdsLicenseServer=$rdsLicenseServer
			licensingType=$tsSettings.LicensingName
		}
		write-host "$server has RDS-RD-Server role installed with licensing server(s) $rdsLicenseServer"
		$results+=$result
		}else{	
			write-host "$server doesn't have RDS-RD-Server role installed"
		}
	}else{
		write-warning "$env:computername cannot connect to $server"
		$noResults+=@{
			computername=$server
			connectionProblem=$True			
		}
	}
	#pause
}

$results|%{write-host "$($_.computername)`: $($_.rdsLicenseServer) | $($_.LicensingType)"}
$noResults|%{write-host "$($_.computername)"}

Leave a Reply

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