PowerShell: Get Terminal Service (Remote Desktop Service) Licenses Report

# rdsLicenseReport.ps1

function getTsLicenses($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
        $tsLicenses=@()
        foreach ($server in $tsLicenseServers){
            # Class source: https://docs.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,ExpirationDate,ProductVersion
        }
        return $tsLicenses
    }catch{
        write-warning $_
        return $false
    }
}

getTsLicenses|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

Leave a Reply

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