# rdsLicenseReport.ps1
$knownRdsComputers=$null # Options: (a) 'SERVER1','SERVER100' (b) Get-Content Path\To\RDServers.txt

function getTsLicenses{
  param(
    $knownRdsComputers,
    $domain=$env:userdnsdomain
    )
  try{
      import-module activedirectory
      $pdc=(get-addomain -server $domain).PdcEmulator
      $tsLicenseServers=(Get-ADGroupMember -server $pdc -Identity "Terminal Server License Servers"|?{$null -ne $_.distinguishedName}).Name
      if($knownRdsComputers){
        $tsLicenseServers=$tsLicenseServers+$knownRdsComputers|select-object Unique
      }
      $tsLicenses=@()
      foreach ($server in $tsLicenseServers){
          # Class source: https://learn.microsoft.com/en-us/windows/win32/termserv/win32-tslicensekeypack?redirectedfrom
          $tsLicense=Get-CIMInstance -computername $server Win32_TSLicenseKeyPack `
                      -filter "TotalLicenses!=0"|?{$_.TypeAndModel -ne 'Built-in TS Per Device CAL'}
          $tsLicenses+=$tsLicense|select -Property @{n='LicenseServer';e={$_.PSComputerName}},TypeAndModel,TotalLicenses,IssuedLicenses,AvailableLicenses,ProductVersion,ExpirationDate
      }
      return $tsLicenses
  }catch{
      write-warning $_
      return $false
  }
}

getTsLicenses $knownRdsComputers|Format-Table -autosize
PS C:\Windows\system32> getTsLicenses|Format-Table -autosize
LicenseServer TypeAndModel TotalLicenses IssuedLicenses AvailableLicenses ExpirationDate ProductVersion
------------- ------------ ------------- -------------- ----------------- -------------- --------------
NEW-RDS-LicServ RDS Per User CAL 1000 790 210 12/31/2037 5:00:00 PM Windows Server 2016
OLD-RDS1 TS or RDS Per User CAL 400 40 360 12/31/2037 5:00:00 PM Windows Server 2008 or Windows Server 2008 R2
OLD-RDS2 RDS Per User CAL 100 10 90 12/31/2037 5:00:00 PM Windows Server 2003
OLD-RDS3 RDS Per User CAL 50 00 50 12/31/2037 5:00:00 PM Windows Server 2000
# Quick 1-liner Snippet to Get Licensing Server Names
# Discover RDS Licensing Servers in Active Directory
(Get-ADGroupMember -Identity "Terminal Server License Servers"|?{$_.ObjectClass -eq 'computer'}).name