Automating RDS Reports for Windows 2016 and 2019

Any windows server administrator running a Remote Desktop Services Server or farm in their environment at some point has wonder if there is an easy way to create RDS usage reports.

The standard manual way of creating this report using the RD licensing Manager’s Create Report function is fine for one off cases, but for most large companies that want to monitor these licences to forecast the need for extra licences in advance, creating these reports manually is not acceptable.

Luckily there is a solution.

The easy solution for this can be found using Powershell. Taking advantage of the psdrive for RDS, you can find all the currently issued licences. The second part is just to convert this info to a readable format like a csv file.

The following script can be ran from the RDS Licence server.

<#
This script will generate a RDS report for Microsoft Servers 2016 and 2019 and above.
This script should be ran on the server running the licence service
#>

try{
    Import-Module RemoteDesktopServices -ErrorAction Stop
}
catch{
    Write-Host "Failed to import module"
}

try{
    Set-Location -Path 'rds:' -ErrorAction Stop
    Remove-Item RDS:\LicenseServer\IssuedLicenses\PerUserLicenseReports\* -Recurse
}
catch{
    Write-Host "Could not access the RDS drive" -ForegroundColor Red
}

#Sets the export path and gets the filename of the license report
$path = “C:\scripts\CAL_Reports\RDS-CAL-Report.csv”
$fileName = (Invoke-WmiMethod Win32_TSLicenseReport -Name GenerateReportEx).FileName

#fetches all entries from the report for the attachment
$fileEntries = (Get-WmiObject Win32_TSLicenseReport | Where-Object FileName -eq $fileName).FetchReportEntries(0,0).ReportEntries

#Converts the data into readable formats
$objArray = @()
foreach($entry in $fileEntries){
    $objArray += $entry | select User, ProductVersion, CALType, ExpirationDate
    $objArray[-1].User = $objArray[-1].User.Split('\') | select -Last 1
    $time = $objArray[-1].ExpirationDate.Split('.') | select -first 1
    $objArray[-1].ExpirationDate = [datetime]::ParseExact($time, "yyyyMMddHHmmss", $null)
}

#Creates the CSV from the formatted report entries object array, the path will be used later to reference the file
$objArray | Export-Csv -Path $path -Delimiter ',' -NoTypeInformation

The results

This script can also be made into a scheduled task that sends attached to an email to make the creating of the report a simple automated task.

https://github.com/philpug/Powershell_scripts/blob/master/RDS/RDS_Report.ps1

You May Also Like

About the Author: Phil

5 Comments

  1. Hi,
    I find the script a good solution but I have problems running the script. I have created the export path exactly as mentioned above. THe PS version is 5.1.
    Could you tell me what I am doing wrong? Do I need any special right?
    Invoke-WmiMethod : Not found
    At line:1 char:14

    Get-WmiObject : Invalid class “Win32_TSLicenseReport”
    At line:2 char:20

    You cannot call a method on a null-valued expression.
    At line:26 char:1

  2. In my case, I’m running it on a Server 2019 with Powershell 5.1.

    I’ve only seen that error when it’s ran from a workstation or different server.

    Please note, the script needs to be ran as an admin on the server with the RDS licencing feature installed.

  3. Hi, any idea on how to retrieve the summary from the report?
    I find this script useful in automating the generation of RDS CAL reports, but it would be more useful to retrieve the summary part where it tells you the totals of available CALs, Issued CALs, etc, instead of the actual licenses issued.

Leave a Reply

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