function Get-ActivationStatus { [CmdletBinding()] param( [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string]$DNSHostName = $Env:COMPUTERNAME ) process { try { $wpa = Get-WmiObject SoftwareLicensingProduct -ComputerName $DNSHostName ` -Filter "ApplicationID = '55c92734-d682-4d71-983e-d6ec3f16059f'" ` -Property LicenseStatus -ErrorAction Stop } catch { $status = New-Object ComponentModel.Win32Exception ($_.Exception.ErrorCode) $wpa = $null } $out = New-Object psobject -Property @{ ComputerName = $DNSHostName; Status = [string]::Empty; } if ($wpa) { :outer foreach($item in $wpa) { switch ($item.LicenseStatus) { 0 {$out.Status = "Unlicensed"} 1 {$out.Status = "Licensed"; break outer} 2 {$out.Status = "Out-Of-Box Grace Period"; break outer} 3 {$out.Status = "Out-Of-Tolerance Grace Period"; break outer} 4 {$out.Status = "Non-Genuine Grace Period"; break outer} 5 {$out.Status = "Notification"; break outer} 6 {$out.Status = "Extended Grace"; break outer} default {$out.Status = "Unknown value"} } } } else {$out.Status = $status.Message} $out } }
This function is designed to be compatible with Get-ADComputer cmdlet. You can pipe cmdlet output to a function. For example, if you want to get activation status for all computers in the domain, you can use the following command sequence: Get-ADComputer -Filter * | Get-ActivationStatus and here is example output:
PS C:\> Get-ADComputer -Filter * | Get-ActivationStatus ComputerName Status ------------ ------ Server Licensed TestPC1 Licensed TestPC2 The RPC server is unavailable TestPC3 Licensed TestPC4 Unlicensed PS C:\>
The RPC server is unavailable status means that WMI call was unsuccessfull (for example, remote computer is offline or unreachable due to network connectivity or firewall issues). Enterprise customers may use Volume Activation Management Tool 2.0.
Richard Mueller edited Revision 13. Comment: Replaced RGB values with color names in HTML to restore colors
Richard Mueller edited Revision 12. Comment: Removed (en-US) from title, added tags, modified title casing
Vadims Podans edited Revision 10. Comment: improved performance and added error handling
Vadims Podans edited Revision 9. Comment: fixed filter to get all licensing statuses. Probably, this filter is not necessary at all.
Richard Mueller edited Revision 8. Comment: Modifying the code made it appear all on one line, so it is unreadable (most of the code is not visible). Better to leave the code flawed.
Richard Mueller edited Revision 6. Comment: Changed filter to "LicenseStatus <= 1", so all values possible except 0
Richard Mueller edited Revision 7. Comment: Modifying the code made it appear all on one line, so it is unreadable (most of the code is not visible). Better to leave the code flawed.
Vadims Podans edited Revision 4. Comment: significantly improved script example and added new output format
Craig Lussier edited Revision 3. Comment: added en-US to tags and title
Useful! Good job, Bryan.
This code has a major flaw: $wpa is filtered by the where clause to only include licenseStatus = 1. Later, he uses a switch statement based on this status, which can only ever be 1.
I rewrote this article. New script version is much faster, because all filterings are made within a WMI call, thus WMI provider returns only required information without overheading netwrok bandwith with unnecessary data.
The code as written can only return either "Licensed" or "Not Available" for each computer, because of the filter. The only other example I can finding using this WMI class uses Where{$_.LicenseStatus -NotMatch "0"}. I tried to change the filter above to "LicenseStatus >=1", but this messed up the formating so the code appears on one line (so most of the code is not visible). I don't know why, but for each computer there are several objects for the same product with LicenseStatus = 0.
fixed.
Try this for Powershell V3 using the new CIM-Instance cmdlets
# set up the different possible types of licenses in an array:
#
$licenseStatus=@{0="Unlicensed"; 1="Licensed"; 2="OOBGrace"; 3="OOTGrace";
4="NonGenuineGrace"; 5="Notification"; 6="ExtendedGrace"}
# Now get the license details and assign the object to $r
$r=Get-CimInstance -Class SoftwareLicensingProduct `
|Where {$_.ApplicationID -eq "55c92734-d682-4d71-983e-d6ec3f16059f" -AND $_.PartialProductKey -ne $null}
# Now apply the value of $r.LicenseStatus to the $licenseStatus array
Write-Host $licenseStatus[[int]$r.LicenseStatus]
# You could equally well return this as a value from a function...