Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and WMI to display Windows activation status.
Hey, Scripting Guy! The previous network administrator at my company was not attentive to his work. I am not sure what he actually did for a living, but it certainly was not network administration. I think he was a professional Internet surfer. I suspect that many of our desktop operating systems are not activated properly. I want to produce a list of desktop systems, get the names and versions of the operating systems, and find out if the system is activated properly. Can I do this with Windows PowerShell?
—DB
Hello DB,
Microsoft Scripting Guy, Ed Wilson, is here. This morning I am still sore from my session with my new personal trainer. In fact, I am pretty sure that I am sore in muscles that I did not know that I had. I guess that means it is working…we will see. I am sipping a cup of Oolong tea this morning. Normally, I would have a black tea in the morning, but this morning, for some reason, I thought that Oolong would do the trick.
I brought back some very nice Oolong tea from Leipzi, Germany earlier this year, and I have been saving it for special occasions. I feel like this morning is a special occasion. With Oolong, especially a very nice Oolong, I do not add anything to the tea. No cinnamon stick, no lemon, no milk, and especially no sugar. Only tea. The Oolong has a very complex flavor, and I can close my eyes and taste the natural sweetness of the tea. There is no need to mess around with something that is already perfect.
Windows Management Instrumentation
There are numerous ways to access Windows Management Instrumentation (WMI) information by using Windows PowerShell. The best way is to use the CIM cmdlets. The CIM cmdlets shipped in Windows PowerShell 3.0 (with Windows 8 and Windows Server 2012), so they have been around for a while. The WMI team wrote a great article about CIM cmdlets on the Windows PowerShell Blog that you may want to read: Introduction to CIM Cmdlets.
To find information about current versions of Windows, I can use the SoftwareLicensingProduct WMI class. (For versions earlier than Windows 7, other WMI classes are required). The SoftwareLicensingProduct class is documented on MSDN, and it contains numerous methods that permit network administrators to completely manage Windows licensing. Two of the more useful methods (for me anyway) are the Activeand the UninstallProductKeymethods, both of which are pretty much self-explanatory as to what they do.
One reason for using the Get-CimInstance cmdlet is that I can create a CIM session pretty much automatically. This is because it relies on WINRM for remoting. It uses the same remoting technology as other Windows PowerShell remoting. This means that it is automatically activated and it simply works in Windows Server 2012 R2 and Windows Server 2012.
For desktop operating systems, WinRM needs to be enabled, but that is easy enough to do via Group Policy (in larger environments) or via a logon script (for smaller environments). If you have been using Windows PowerShell for a while to manage your network, chances are good that you already have WinRM enabled.
I can easily query Active Directory Domain Services (AD DS) by using the Get-Computer cmdlet to retrieve a list of the computers I want to check. I can then feed that list to the New-CimSession cmdlet to create CIM sessions, and then I can use Get-CimInstance to retrieve my information.
That is how I might modify things to run against all of the computers on my network. For output, I would probably pipe the results to Export-CSV to create a nice file that I can open in Microsoft Excel for my documentation.
Getting licensing status
I am going to use the Get-CimInstance cmdlet to query the SoftwareLicensingProduct WMI class, and to find the ApplicationID for my version of Windows. This is sort of a magic number, and it takes a bit of searching on the Internet to find the information (it is not in the MSDN article for the WMI class).
I use the Get-CimInstance Windows PowerShell cmdlet, specify the WMI class, and pick up the ComputerName value from the environmental variable. I specify the ApplicationID property value for my filter, and I look for a LicenseStatus of 1 (this means it is activated, and this enumeration value does come from the MSDN article).
Then I select the name and description of the product, and I create a custom property that includes the name of the computer. For this example, I throw it to Format-List so I can see my results. Here is the script:
Get-CimInstance SoftwareLicensingProduct -ComputerName $env:computername `
-Filter "ApplicationID = '55c92734-d682-4d71-983e-d6ec3f16059f'" |
where licensestatus -eq 1 |
Select name, description, @{Label='computer';
Expression = {$_.PscomputerName}} |
Format-List name, description, computer
The use of the script and the associated output are shown in the following image:
DB, that is all there is to using Windows PowerShell to detect product activation status. WMI Week will continue tomorrow when I will talk about more cool stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy