{"id":3871,"date":"2013-04-06T00:01:00","date_gmt":"2013-04-06T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/04\/06\/testing-license-state-and-setting-the-license-key\/"},"modified":"2013-04-06T00:01:00","modified_gmt":"2013-04-06T00:01:00","slug":"testing-license-state-and-setting-the-license-key","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/testing-license-state-and-setting-the-license-key\/","title":{"rendered":"Testing License State and Setting the License Key"},"content":{"rendered":"<p><strong style=\"font-size: small\">Summary<\/strong><span style=\"font-size: small\">: Microsoft Windows PowerShell MVP, Richard Siddaway, talks about using Windows PowerShell and WMI to work with operating system licensing.<\/span><\/p>\n<p><span style=\"font-size: small\">Microsoft Scripting Guy, Ed Wilson, is here. This week we will not have our usual <strong>PowerTip<\/strong>. Instead we have excerpts from seven books from Manning Press. In addition, each blog will have a special code for 50% off the book being excerpted that day. Remember that the code is valid only for the day the excerpt is posted. The coupon code is also valid for a second book from the Manning collection.<\/span><\/p>\n<p class=\"Body\" align=\"left\"><span style=\"font-size: small\">This excerpt is from <a href=\"http:\/\/www.manning.com\/siddaway2\/\" target=\"_blank\">PowerShell and WMI<br \/><\/a><\/span><span style=\"font-size: small\">&nbsp; By Richard Siddaway<\/span><\/p>\n<p class=\"Body\" align=\"left\"><span style=\"font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7801.wes-4-6-13-1.jpg\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Photo of book cover\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7801.wes-4-6-13-1.jpg\" alt=\"Photo of book cover\" \/><\/a><\/span><\/p>\n<p class=\"Body\" align=\"left\"><span style=\"font-size: small\">Product activation for Windows servers may seem to be a pain, but it&rsquo;s a fact of life. You have to do it for two reasons:<\/span><\/p>\n<ul>\n<li><span style=\"font-size: small\">To ensure the software is properly licensed and you remain legal<\/span><\/li>\n<li><span style=\"font-size: small\">To keep the servers working<\/span><\/li>\n<\/ul>\n<p class=\"Body\" align=\"left\"><span style=\"font-size: small\">In this blog, which is based on Chapter 13 of <a href=\"http:\/\/www.manning.com\/siddaway2\/\" target=\"_blank\">PowerShell and WMI<\/a>, I will explain how to test license state and set the license key. How can you ensure software is properly licensed in the most efficient manner? My friend James O&rsquo;Neill answered this in a blog post. In it, he references two WMI classes:<\/span><\/p>\n<ul>\n<li><span style=\"font-size: small\">SoftwareLicensingProduct<\/span><\/li>\n<li><span style=\"font-size: small\">SoftwareLicensingService<\/span><\/li>\n<\/ul>\n<p class=\"Callout\" style=\"padding-left: 30px\" align=\"left\"><span style=\"font-size: small\"><strong>Note<\/strong> &nbsp;These classes are new in Windows 7 and Windows Server 2008 R2. They&rsquo;re not available on earlier versions of Windows.<\/span><\/p>\n<p class=\"Body\" align=\"left\"><span style=\"font-size: small\">These tips are derived from James&rsquo; post. You can test the license status of Windows like this:<\/span><\/p>\n<p class=\"Body\" style=\"padding-left: 30px\" align=\"left\"><span style=\"font-size: small\">Get-WmiObject SoftwareLicensingProduct |<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">select Name, LicenseStatus<\/span><\/p>\n<p class=\"Code\"><strong style=\"font-size: small\">LicenseStatus <\/strong><span style=\"font-size: small\">will return an integer value, where 0 = Unlicensed and 1 = Licensed. A number of results that represent the various ways Windows can be licensed or activated are returned. The important result is the one with a partial product key:<\/span><\/p>\n<p class=\"Body\" style=\"padding-left: 30px\" align=\"left\"><span style=\"font-size: small\">Get-WmiObject SoftwareLicensingProduct |<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">where {$_.PartialProductKey} |<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">ft&nbsp; Name, ApplicationId, LicenseStatus &ndash;a<\/span><\/p>\n<p class=\"Code\"><span style=\"font-size: small\">This indicates the licensing situation you&rsquo;re dealing with. It would be nice, though, if you could get a little bit more information about the licensing state of your system.<\/span><\/p>\n<h2>Testing license state<\/h2>\n<p class=\"Body1\" align=\"left\"><span style=\"font-size: small\">Has your IT environment ever been audited? Can you prove that all of your servers are properly activated? This section will help you answer the second question. In addition to being a useful test while you&rsquo;re building a new server, you can use it to test the setup of your whole estate.<\/span><\/p>\n<h3>Problem<\/h3>\n<p class=\"Body1\" align=\"left\"><span style=\"font-size: small\">You need to test the activation and license state of your servers for auditing purposes. Some of the servers are in remote locations and you don&rsquo;t have the time or resources to physically visit them all.<\/span><\/p>\n<h3>Solution<\/h3>\n<p class=\"Body1\" align=\"left\"><span style=\"font-size: small\">You&rsquo;ve seen that the license status information is available through the <strong>SoftwareLicensingProduct<\/strong> class. The following listing shows how you can use that class to generate a meaningful statement about the license status of your server.<\/span><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p class=\"Body1\" align=\"left\"><span style=\"font-size: small\"><strong>Listing 1: Test license status<\/strong><\/span><strong style=\"font-size: small\">&nbsp;<\/strong><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">$lstat = DATA {<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">ConvertFrom-StringData -StringData @&#8217;<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">0 = Unlicensed<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">1 = Licensed<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">2 = OOB Grace<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">3 = OOT Grace<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">4 = Non-Genuine Grace<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">5 = Notification<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">6 = Extended Grace<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">&#8216;@<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">}<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">function get-licensestatus {<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">param (<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">[parameter(ValueFromPipeline=$true,<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">&nbsp;&nbsp; ValueFromPipelineByPropertyName=$true)]<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">&nbsp; [string]$computername=&#8221;$env:COMPUTERNAME&#8221;<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">)<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">PROCESS {<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">&nbsp;Get-WmiObject SoftwareLicensingProduct -ComputerName $computername |<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">&nbsp;where {$_.PartialProductKey} |<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">&nbsp;select Name, ApplicationId,<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">&nbsp;@{N=&#8221;LicenseStatus&#8221;; E={$lstat[&#8220;$($_.LicenseStatus)&#8221;]} }<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">}}<\/span><\/p>\n<p class=\"Body1\" align=\"left\"><span style=\"font-size: small\">A hash table, <strong>$lstat<\/strong>, is defined at the beginning of the script. You can then call the <strong>SoftwareLicensingProduct<\/strong> class&nbsp;against the computer, passed as a parameter to the function. The results are filtered on the <strong>PartialproductKey<\/strong> property&nbsp;to ensure you only get the results you need. There are three pieces of data you need:<\/span><\/p>\n<ul>\n<li><span style=\"font-size: small\">The name of the product<\/span><\/li>\n<li><span style=\"font-size: small\">The <strong>ApplicationId<\/strong>, which is a GUID<\/span><\/li>\n<li><span style=\"font-size: small\">The decoded license status<\/span><\/li>\n<\/ul>\n<p class=\"Body\" align=\"left\"><span style=\"font-size: small\">The decoding of the license status is managed by the calculated field in the <strong>Select-Object<\/strong> statement.<\/span><\/p>\n<h3>Discussion<\/h3>\n<p class=\"Body1\" align=\"left\"><span style=\"font-size: small\">The following image shows the results of running the function. The <strong>ApplicationId<\/strong> is fixed for versions of Windows. You should get the same result returned on all versions.<\/span><\/p>\n<p class=\"FigureCaption\"><span style=\"font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3618.wes-4-6-13-2.png\"><img decoding=\"async\" style=\"border: 0px currentColor\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3618.wes-4-6-13-2.png\" alt=\"Image of command output\" \/><\/a><\/span><\/p>\n<p class=\"Body1\" align=\"left\"><span style=\"font-size: small\">The results in the previous image show that you&rsquo;re still in the grace period after installation of the operating system. You need to set the license key before you can activate the server.<\/span><\/p>\n<h2>Setting the license key<\/h2>\n<p class=\"Body1\" align=\"left\"><span style=\"font-size: small\">A Windows license key consists of five groups of five alphanumeric characters. A valid license key is required for each instance of Windows. The key is usually found with the media. Keys are specific to the version of Windows and the source of the media. For instance, you can&rsquo;t use an MSDN key on a commercial version of Windows.<\/span><\/p>\n<h3>Problem<\/h3>\n<p class=\"Body1\" align=\"left\"><span style=\"font-size: small\">The license key needs to be set before you can activate the system. You need to perform this act remotely and ensure that the license key is in the correct format.<\/span><\/p>\n<h3>Solution<\/h3>\n<p class=\"Body1\" align=\"left\"><span style=\"font-size: small\">Windows 7 and Windows Server 2008 R2 have a WMI class, <strong>SoftwareLicensingService<\/strong>, which you can use to solve this issue. This is shown in the following listing. The license key and computer name are mandatory parameters. This removes the need for default values. The license key pattern is evaluated by using a regular expression and the <strong>ValidatePattern<\/strong><strong> <\/strong>method. This won&rsquo;t guarantee that the key is correct, but it will ensure that it&rsquo;s in the right format.<\/span><\/p>\n<p class=\"Body1\" align=\"left\"><span style=\"font-size: small\"><strong>Listing 2: Set license key<\/strong><\/span><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">function set-licensekey {<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">param (<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">[parameter(Mandatory=$true)]<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">[string]<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">[ValidatePattern(&#8220;^\\S{5}-\\S{5}-\\S{5}-\\S{5}-\\S{5}&#8221;)]<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">$Productkey,<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">[parameter(Mandatory=$true)]<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">[string]$computername=&#8221;$env:COMPUTERNAME&#8221;<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">)<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\">&nbsp;<\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">&nbsp;$product = Get-WmiObject -Class SoftwareLicensingService `<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">-computername $computername<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">&nbsp;$product.InstallProductKey($ProductKey)<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">&nbsp;$product.RefreshLicenseStatus()<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">}<\/span><\/p>\n<p class=\"Body\" align=\"left\"><span style=\"font-size: small\">You use the <\/span><strong style=\"font-size: small\">SoftwareLicensingService<\/strong><span style=\"font-size: small\"> class&nbsp;to create a WMI object. You can use the <\/span><strong style=\"font-size: small\">InstallProductKey<\/strong><span style=\"font-size: small\"> method&nbsp;with the license key as an argument. The last line of the function refreshes the license status information.<\/span><\/p>\n<h3>Discussion<\/h3>\n<p class=\"Body1\" align=\"left\"><span style=\"font-size: small\">The function is used as follows:<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">set-licensekey -Productkey &#8220;XXXXX-XXXXX-XXXXX-XXXXX-XXXXX&#8221;&nbsp; `<\/span><\/p>\n<p class=\"Code\" style=\"padding-left: 30px\"><span style=\"font-size: small\">-computername &#8220;10.10.54.118&#8221;<\/span><\/p>\n<p class=\"Code\"><span style=\"font-size: small\">The &#8220;XXXXX-XXXXX-XXXXX-XXXXX-XXXXX&#8221; represents the license key. You didn&rsquo;t really think I&rsquo;d use my real key? The computer on which you&rsquo;re installing the key can be designated by IP address as here or by its name.<\/span><\/p>\n<p class=\"Body1\" align=\"left\"><span style=\"font-size: small\">Configuring a new server is a task that occurs on a regular basis in most organizations. There are a number of steps to be completed after the operating system is installed:<\/span><\/p>\n<ul>\n<li><span style=\"font-size: small\">Rename the server to something more meaningful.<\/span><\/li>\n<li><span style=\"font-size: small\">Stop and restart the server as required.<\/span><\/li>\n<li><span style=\"font-size: small\">Set the IP address and DNS servers.<\/span><\/li>\n<li><span style=\"font-size: small\">Rename the network connection.<\/span><\/li>\n<li><span style=\"font-size: small\">Join the server to the domain.<\/span><\/li>\n<li><span style=\"font-size: small\">Install the license key.<\/span><\/li>\n<li><span style=\"font-size: small\">Activate the server.<\/span><\/li>\n<li><span style=\"font-size: small\">Set the power plan.<\/span><\/li>\n<\/ul>\n<p class=\"Body\" align=\"left\"><span style=\"font-size: small\">All of these activities take time. You can use Windows PowerShell functions to perform these tasks remotely so you don&rsquo;t need to spend time accessing the server directly.<\/span><\/p>\n<p class=\"Body\" align=\"left\">~Richard<\/p>\n<p><strong>Here is the code for the discount offer today at <\/strong><a href=\"http:\/\/www.manning.com\/\" target=\"_blank\">www.manning.com<\/a>: <strong>scriptw6<\/strong><br \/> Valid for 50% off <a href=\"http:\/\/www.manning.com\/siddaway2\/\" target=\"_blank\">PowerShell and WMI<\/a> and <a href=\"http:\/\/www.manning.com\/wicklund\/\" target=\"_blank\">SharePoint 2010 Workflows in Action<\/a> <br \/> Offer valid from April 6, 2013 12:01 AM until April 7, midnight (EST)<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Windows PowerShell MVP, Richard Siddaway, talks about using Windows PowerShell and WMI to work with operating system licensing. Microsoft Scripting Guy, Ed Wilson, is here. This week we will not have our usual PowerTip. Instead we have excerpts from seven books from Manning Press. In addition, each blog will have a special code [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,31,189,3,45,6],"class_list":["post-3871","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-operating-system","tag-richard-siddaway","tag-scripting-guy","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Windows PowerShell MVP, Richard Siddaway, talks about using Windows PowerShell and WMI to work with operating system licensing. Microsoft Scripting Guy, Ed Wilson, is here. This week we will not have our usual PowerTip. Instead we have excerpts from seven books from Manning Press. In addition, each blog will have a special code [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3871"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3871\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=3871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}