Verifying Windows Licenses

While working on a recent project where I was taking over a new business that was detaching from it's parent company, I had to remove KMS licensing from a number of workstations.

Verifying Windows Licenses

While working on a recent project where I was taking over a new business that was detaching from it's parent company, I had to remove KMS licensing from a number of workstations. Over the course of removing the licensing, I found some handy scripts to monitor Windows licensing on workstations and even generate alerts if licenses aren't working for whatever reason.

The Windows Software Licensing Management Tool

slmgr is a command line tool that you can use to perform a bunch of tasks on Windows licensing. It can actually be used for other volume licensing like Office, too.

Getting the current license status is simple enough:

slmgr /dli

When you just run slmgr like an executable it will open dialog boxes with the response. The other way to run it is with the cscript tool:

>  cscript C:\Windows\System32\slmgr.vbs /dli
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.


Name: Windows(R), Professional edition
Description: Windows(R) Operating System, OEM_DM channel
Partial Product Key: ****
License Status: Licensed

The only line we care about here is the last one: License Status: Licensed. That means that the OS is showing as licensed. All done. If Windows is not licensed, you'll usually see License Status: Notification indicating that there's some problem. Sometimes it'll tell you what the problem is below the status, sometimes it won't. There are plenty of blog posts on the web about how to use slmgr to license and activate Windows so I won't cover that here.

Creating Alerts

Now that we know how to get the license status, it's time to create alerts when Windows isn't licensed. All we need to do is capture and parse the output from the slmgr command to create an alert in our RMM (in my case Atera) when a computer isn't properly licensed:

$slmgr = Join-Path -Path $env:Windir -ChildPath "System32\slmgr.vbs"

$LicenseDetails = & cscript $slmgr /dli

# Using the results of slmgr which are in the form of an array, gran the License Status line
$LicenseStatusLine = $LicenseDetails | Where-Object { $_ -Like "License Status:*" }

# If you didn't know, list index -1 is the last element in the list.
$LicenseStatus = $LicenseStatusLine.Split(": ")[-1]

# For anything but 
if ($LicenseStatus -ne "Licensed") {
  Set-AteraAPIKey -APIKey "***YOUR API KEY***"
  $Agent = Get-AteraAgent
  
  # Check for existing open alerts about this issue
  $Alerts = Get-AteraAlerts -Open -Snoozed | Where-Object { $_.DeviceGuid -eq $Agent.DeviceGuid -and $_.Title -eq "Windows may not be licensed" }
  if ($Alerts -ne $nil) { return }
  
  # Create the new alert if there isn't one with the license details in the alert message
  New-AteraAlert -DeviceGuid $Agent.DeviceGuid -CustomerID $Agent.CustomerID -Title "Windows may not be licensed" -Severity Warning -AlertCategoryID General -MessageTemplate ($LicenseDetails -Join "`n")
}

One fun tip about PowerShell lists that you can see in the script is that you can index into a list in reverse using negative indexes.