Search results matching tags 'PowerShell', 'hyper-v', 'VBScript', and 'automation'http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&tag=PowerShell,hyper-v,VBScript,automation&orTags=0Search results matching tags 'PowerShell', 'hyper-v', 'VBScript', and 'automation'en-USCommunityServer 2.1 SP2 (Build: 61129.1)[OT] : Windows Activation, en massehttp://sqlblog.com/blogs/aaron_bertrand/archive/2011/03/13/ot-windows-activation-en-masse.aspxSun, 13 Mar 2011 20:07:00 GMT21093a07-8b3d-42db-8cbf-3350fcbf5496:34104AaronBertrand<p>This weekend I discovered a minor issue in one of my virtual environments. I had built out 100 VMs based on a Hyper-V template, but I forgot to activate the original source before creating the template, so all of the machines were suddenly out of compliance. While easy enough on a one- or two-machine basis to just log into the machine and activate manually, there was no way I was even going to dream of repeating that process on 100 machines.</p>
<p><font size="4"><br>My First Reaction : PowerShell</font></p>
<p>Whenever I do anything with this number of machines, I assume there's an easy way to do it in PowerShell. My searching throughout the 'toobz led to many promising scripts; unfortunately, most dealt with the old WMI class used prior to Windows 7 and Windows Server 2008 R2: <a href="http://msdn.microsoft.com/en-us/library/aa394520%28VS.85%29.aspx" title="http://msdn.microsoft.com/en-us/library/aa394520%28VS.85%29.aspx" target="_blank">Win32_WindowsProductActivation</a>. In the latest versions of the operating system, trying to utilize this class leads to generic "Invalid Class" errors; the methods and properties associated with this class have been split out into two new classes: <a href="http://msdn.microsoft.com/en-us/library/cc534596%28VS.85%29.aspx" title="http://msdn.microsoft.com/en-us/library/cc534596%28VS.85%29.aspx" target="_blank">SoftwareLicensingProduct</a> and <a href="http://msdn.microsoft.com/en-us/library/cc534597%28VS.85%29.aspx" title="http://msdn.microsoft.com/en-us/library/cc534597%28VS.85%29.aspx" target="_blank">SoftwareLicensingService</a> (did I mention I'm really glad they're moving away from the Win32_ prefix?).&nbsp; The only PowerShell script I found that dealt with these new classes was <a href="http://blogs.technet.com/b/jamesone/archive/2009/07/22/how-to-activate-windows-from-a-script-even-remotely.aspx" title="http://blogs.technet.com/b/jamesone/archive/2009/07/22/how-to-activate-windows-from-a-script-even-remotely.aspx" target="_blank">a function created by James O'Neill</a>; sadly, I could not get his version to work. I spent about an hour trying to tweak the script to make it work, but couldn't get past various errors. By default, the error I received was as follows:</p>
<blockquote>
<table bgcolor="#eeeeee" cellpadding="0" cellspacing="0">
<tr>
<td>
<div style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;color:red;">Register-Computer : Cannot process argument transformation on parameter 'Productkey'. Cannot convert value to type System.String.<br>At line:30 char:18<br>+ Register-Computer &lt;&lt;&lt;&lt;&nbsp; "&lt;product key&gt;","&lt;computer&gt;"<br>&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : InvalidData: (:) [Register-Computer], ParameterBindin...mationException<br>&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : ParameterArgumentTransformationError,Register-Computer</div></td>
</tr>
</table>
</blockquote>
<p>Not sure if this is an issue with the RegEx, but after removing the validation on the parameter and subsequently going through various other iterations, seeing many different error messages come out of the script, I gave up. A man only has so much patience, especially on the weekend.<br></p>
<p><br><font size="4">Going Back to Basic(s) </font></p>
<p>I don't want to say anything bad about PowerShell at this point, except that it's not the only tool in the toolbox, and it's not the magical cure for all that ails you. One thing I knew I would have little trouble getting to work would be VBScript. Assuming that I have 100 machines named VM-001, VM-002, ... , VM-099, VM-100, I could use the following script to activate them all in one swoop (sorry it's not color-coded, but it is fairly short):</p>
<blockquote>
<table bgcolor="#eeeeee" cellpadding="0" cellspacing="0">
<tr>
<td>
<pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;">For i = 1 to 100<br><br>&nbsp;&nbsp;&nbsp; srv = "VM-" &amp; right("000" &amp; i, 3)<br>&nbsp;&nbsp;&nbsp; wmi = "winmgmts:{impersonationLevel=impersonate}!\\" &amp; srv &amp; "\root\cimv2"<br><br>&nbsp;&nbsp;&nbsp; For Each o in GetObject(wmi).InstancesOf("SoftwareLicensingProduct")<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If&nbsp; o.ProductKeyID &amp; "" &gt; "" Then o.Activate()<br>&nbsp;&nbsp;&nbsp; Next<br><br>&nbsp;&nbsp;&nbsp; For Each o in GetObject(wmi).InstancesOf("SoftwareLicensingService")<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; o.RefreshLicenseStatus()&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; Next <br><br>Next</pre></td></tr></table></blockquote>
<p>Now, I don't handle all of the little nuances that James was concerned about, but it certainly did the job, and allowed me to move on with my Sunday. Once I had the script nicely formatted, it took about 6 minutes to run against all 100 servers, and random spot checks on VM-034, VM-066 and VM-092 proved that the machines had all been activated.</p><p>Note that without the RefreshLicenseStatus() call, when you log into any machine (at least within a certain amount of time), you'll get a prompt about verifying that Windows is genuine. When you proceed, it will simply tell you that Windows has been successfully activated. I also realize that your list of servers might not be so conveniently-named; you can just as easily pull the list of servers from a text file or database.<br></p><p>Hopefully this will come in handy for someone as forgetful as me.</p><p>&nbsp; <br></p>