<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Get-PowerShell &#187; Compare-Object</title>
	<atom:link href="http://get-powershell.com/category/compare-object/feed/" rel="self" type="application/rss+xml" />
	<link>http://get-powershell.com</link>
	<description></description>
	<lastBuildDate>Sat, 10 Jul 2010 15:44:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Comparing Installed Hotfixes on Servers</title>
		<link>http://get-powershell.com/2009/08/28/comparing-installed-hotfixes-on-servers/</link>
		<comments>http://get-powershell.com/2009/08/28/comparing-installed-hotfixes-on-servers/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 14:21:19 +0000</pubDate>
		<dc:creator>Andy Schneider</dc:creator>
				<category><![CDATA[Compare-Object]]></category>
		<category><![CDATA[Hot Fix]]></category>
		<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">http://get-powershell.com/2009/08/28/comparing-installed-hotfixes-on-servers/</guid>
		<description><![CDATA[This week I had the need to scan several systems and see which hotfixes were installed on which machines and also figure out which hotfixes I needed to install to make sure all machines were identical. Now that PowerShell V2 has been RTM’d in windows 7 and is included in the Release Candidate of the [...]]]></description>
			<content:encoded><![CDATA[<p>This week I had the need to scan several systems and see which hotfixes were installed on which machines and also figure out which hotfixes I needed to install to make sure all machines were identical. Now that PowerShell V2 has been RTM’d in windows 7 and is included in the <a href="https://connect.microsoft.com/windowsmanagement/Downloads">Release Candidate of the Windows Management Framework,</a> I am going to go with V2 features. However, this script could be ported to V1 if need be. </p>
<p>As I was thinking about this problem, there were a few tools in the toolkit I thought I could use. The first is the new cmdlet, get-hotfix. This is just a wrapper of Win32_QuickFixEngineering but its nice to have it abstracted up to the cmdlet level. The second tool I thought of using is compare-object. I have known about this cmdlet but haven’t had a real opportunity to use it much. It’s actually very powerful but it does take a bit of neuron firing to wrap your head around how it works. </p>
<p>The way I set this is up allows me to compare two servers.&#160; I also have a credential parameter that is used to access servers so I can do my part in supporting the principle of <a href="http://technet.microsoft.com/en-us/library/bb456992.aspx">Least Privilege</a> and not be logged in with a Domain Admin account all the time. </p>
<p>So I pull the list of installed hotfixes from each server and select only the HotfixId property. This will make using the compare-object cmdlet a bit easier. If we do a get-member on compare-object we see that it outputs a PSObject with 2 noteproperties, a InputObject and a SideIndicator.</p>
<p><a href="http://get-powershell.com/wp-content/uploads/2009/08/image12.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://get-powershell.com/wp-content/uploads/2009/08/image_thumb10.png" width="466" height="284" /></a> </p>
<p>From the help on compare-object we find this description of the SideIndicator property:</p>
<p><em>The result of the comparison indicates whether a property value appeared only in the object from the Reference set (indicated by the &lt;= symbol), only in the object from the Difference set (indicated by the =&gt; symbol) or, if the IncludeEqual parameter is specified, in both objects (indicated by the == symbol).</em></p>
<p>Well that does the job but frankly its output is difficult to easily interpret at first glance. The beauty of PowerShell is that if you really don’t like the way something works, you can easily work around it. In this case, I created a new array of custom objects that have three properties: KB, the name of the first server, and the name of the second server. (Lines 19 and 22)</p>
<p>Then I foreach’d (the new verb of the day) through the collection of compared hotfixes and switched on the “SideIndicator” property. I did this to make the output more clear so users of the script would not have to interpret all the arrows and equal signs generated by compare-object.</p>
<p>Here is a sample of the output:</p>
<p><a href="http://get-powershell.com/wp-content/uploads/2009/08/image13.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://get-powershell.com/wp-content/uploads/2009/08/image_thumb11.png" width="547" height="587" /></a> </p>
<p>I used Write-Host to generate some text output but I also get back an object that I can slice and dice later on.</p>
<div style="border-bottom: black 1px solid; border-left: black 1px solid; padding-bottom: 5px; padding-left: 5px; width: 750px; padding-right: 5px; font-family: consolas,lucida console; font-size: 10pt; overflow: auto; border-top: black 1px solid; border-right: black 1px solid; padding-top: 5px">
<table border="0" cellspacing="0" cellpadding="5">
<tbody>
<tr>
<td valign="top">
<div style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; font-family: consolas,lucida console; background: #cecece; font-size: 10pt; padding-top: 5px">001             <br />002              <br />003              <br />004              <br />005              <br />006              <br />007              <br />008              <br />009              <br />010              <br />011              <br />012              <br />013              <br />014              <br />015              <br />016              <br />017              <br />018              <br />019              <br />020              <br />021              <br />022              <br />023              <br />024              <br />025              <br />026              <br />027              <br />028              <br />029              <br />030              <br />031              <br />032              <br />033              <br />034              <br />035              <br />036              <br />037              <br />038              <br />039              <br />040              <br />041              <br />042              <br />043              <br />044              <br />045              <br />046              <br />047              <br />048              <br />049              <br />050              </div>
</td>
<td valign="top" nowrap="nowrap">
<div style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; font-family: consolas,lucida console; background: #fcfcfc; font-size: 10pt; padding-top: 5px"><span style="color: #00008b">Function</span><span style="color: #000000">&#160;</span><span style="color: #8a2be2">Compare-InstalledHotfix</span><span style="color: #000000">&#160;</span><span style="color: #000000">{</span>              <br /><span style="color: #00008b">param</span><span style="color: #000000">&#160;</span><span style="color: #000000">(</span>              <br /><span style="color: #a9a9a9">[</span><span style="color: #add8e6">parameter</span><span style="color: #000000">(</span><span style="color: #000000">Mandatory</span><span style="color: #a9a9a9">=</span><span style="color: #ff4500">$true</span><span style="color: #a9a9a9">,</span><span style="color: #000000">Position</span><span style="color: #a9a9a9">=</span><span style="color: #800080">0</span><span style="color: #000000">)</span><span style="color: #a9a9a9">]</span>              <br /><span style="color: #ff4500">$server1</span><span style="color: #a9a9a9">,</span>              </p>
<p><span style="color: #a9a9a9">[</span><span style="color: #add8e6">parameter</span><span style="color: #000000">(</span><span style="color: #000000">Mandatory</span><span style="color: #a9a9a9">=</span><span style="color: #ff4500">$true</span><span style="color: #a9a9a9">,</span><span style="color: #000000">Position</span><span style="color: #a9a9a9">=</span><span style="color: #800080">1</span><span style="color: #000000">)</span><span style="color: #a9a9a9">]</span>              <br /><span style="color: #ff4500">$server2</span><span style="color: #a9a9a9">,</span><span style="color: #000000">&#160;</span>              </p>
<p><span style="color: #a9a9a9">[</span><span style="color: #add8e6">parameter</span><span style="color: #000000">(</span><span style="color: #000000">Mandatory</span><span style="color: #a9a9a9">=</span><span style="color: #ff4500">$true</span><span style="color: #a9a9a9">,</span><span style="color: #000000">Position</span><span style="color: #a9a9a9">=</span><span style="color: #800080">3</span><span style="color: #000000">)</span><span style="color: #a9a9a9">]</span>              <br /><span style="color: #008080">[Management.Automation.PSCredential]</span>              <br /><span style="color: #ff4500">$credential</span>              <br /><span style="color: #000000">)</span>              </p>
<p><span style="color: #ff4500">$server1HotFix</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">=</span><span style="color: #000000">&#160;</span><span style="color: #0000ff">get-hotfix</span><span style="color: #000000">&#160;</span><span style="color: #000080">-computer</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$server1</span><span style="color: #000000">&#160;</span><span style="color: #000080">-Credential</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$credential</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">|</span><span style="color: #000000">&#160;</span><span style="color: #0000ff">select</span><span style="color: #000000">&#160;</span><span style="color: #8a2be2">HotfixId</span>              <br /><span style="color: #ff4500">$server2HotFix</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">=</span><span style="color: #000000">&#160;</span><span style="color: #0000ff">get-hotfix</span><span style="color: #000000">&#160;</span><span style="color: #000080">-computer</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$server2</span><span style="color: #000000">&#160;</span><span style="color: #000080">-Credential</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$credential</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">|</span><span style="color: #000000">&#160;</span><span style="color: #0000ff">select</span><span style="color: #000000">&#160;</span><span style="color: #8a2be2">HotfixId</span>              </p>
<p><span style="color: #ff4500">$comparedHotfixes</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">=</span><span style="color: #000000">&#160;</span><span style="color: #0000ff">compare-object</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$server2HotFix</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$server1HotFix</span><span style="color: #000000">&#160;</span><span style="color: #000080">-IncludeEqual</span>              </p>
<p><span style="color: #ff4500">$result</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">=</span><span style="color: #000000">&#160;</span><span style="color: #000000">@(</span><span style="color: #000000">)</span><span style="color: #000000">;</span>              </p>
<p><span style="color: #00008b">foreach</span><span style="color: #000000">&#160;</span><span style="color: #000000">(</span><span style="color: #ff4500">$c</span><span style="color: #000000">&#160;</span><span style="color: #00008b">in</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$comparedHotfixes</span><span style="color: #000000">)</span><span style="color: #000000">&#160;</span><span style="color: #000000">{</span>              <br /><span style="color: #000000">&#160;&#160;&#160; </span><span style="color: #ff4500">$kbinfo</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">=</span><span style="color: #000000">&#160;</span><span style="color: #8b0000">&quot;&quot;</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">|</span><span style="color: #000000">&#160;</span><span style="color: #0000ff">select</span><span style="color: #000000">&#160;</span><span style="color: #8a2be2">KB</span><span style="color: #a9a9a9">,</span><span style="color: #ff4500">$server1</span><span style="color: #a9a9a9">,</span><span style="color: #ff4500">$server2</span>              <br /><span style="color: #000000">&#160;&#160;&#160; </span><span style="color: #ff4500">$kbinfo</span><span style="color: #a9a9a9">.</span><span style="color: #000000">KB</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">=</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$c</span><span style="color: #a9a9a9">.</span><span style="color: #000000">InputObject</span><span style="color: #a9a9a9">.</span><span style="color: #000000">HotfixId</span>              <br /><span style="color: #000000">&#160;&#160;&#160; </span><span style="color: #00008b">switch</span><span style="color: #000000">&#160;</span><span style="color: #000000">(</span><span style="color: #ff4500">$c</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SideIndicator</span><span style="color: #000000">)</span>              <br /><span style="color: #000000">&#160;&#160;&#160; </span><span style="color: #000000">{</span>              <br /><span style="color: #000000">&#160;&#160;&#160; </span><span style="color: #8b0000">&quot;==&quot;</span><span style="color: #000000">&#160;</span><span style="color: #000000">{</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #0000ff">write-host</span><span style="color: #000000">&#160;</span><span style="color: #000080">-ForegroundColor</span><span style="color: #000000">&#160;</span><span style="color: #8a2be2">Green</span><span style="color: #000000">&#160;</span><span style="color: #8b0000">&quot;Both servers have $($c.InputObject.HotfixId)&quot;</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #ff4500">$kbinfo</span><span style="color: #a9a9a9">.</span><span style="color: #000000">(</span><span style="color: #ff4500">$server1</span><span style="color: #000000">)</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">=</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$true</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #ff4500">$kbinfo</span><span style="color: #a9a9a9">.</span><span style="color: #000000">(</span><span style="color: #ff4500">$server2</span><span style="color: #000000">)</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">=</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$true</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #ff4500">$result</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">+=</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$kbinfo</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #000000">}</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>              <br /><span style="color: #000000">&#160;&#160;&#160; </span><span style="color: #8b0000">&quot;=&gt;&quot;</span><span style="color: #000000">&#160;</span><span style="color: #000000">{</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #0000ff">write-host</span><span style="color: #000000">&#160;</span><span style="color: #000080">-ForegroundColor</span><span style="color: #000000">&#160;</span><span style="color: #8a2be2">Yellow</span><span style="color: #000000">&#160;</span><span style="color: #8b0000">&quot;$server1 has $($c.InputObject.HotfixId) but $server2 doesn&#8217;t&quot;</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #ff4500">$kbinfo</span><span style="color: #a9a9a9">.</span><span style="color: #000000">(</span><span style="color: #ff4500">$server1</span><span style="color: #000000">)</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">=</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$true</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #ff4500">$kbinfo</span><span style="color: #a9a9a9">.</span><span style="color: #000000">(</span><span style="color: #ff4500">$server2</span><span style="color: #000000">)</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">=</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$false</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #ff4500">$result</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">+=</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$kbinfo</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #000000">}</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>              <br /><span style="color: #000000">&#160;&#160;&#160; </span><span style="color: #8b0000">&quot;&lt;=&quot;</span><span style="color: #000000">&#160; </span><span style="color: #000000">{</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #0000ff">write-host</span><span style="color: #000000">&#160;</span><span style="color: #000080">-ForegroundColor</span><span style="color: #000000">&#160;</span><span style="color: #8a2be2">Magenta</span><span style="color: #000000">&#160;</span><span style="color: #8b0000">&quot;$server2 has $($c.InputObject.HotfixId) but $server1 doesn&#8217;t&quot;</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #ff4500">$kbinfo</span><span style="color: #a9a9a9">.</span><span style="color: #000000">(</span><span style="color: #ff4500">$server1</span><span style="color: #000000">)</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">=</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$false</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #ff4500">$kbinfo</span><span style="color: #a9a9a9">.</span><span style="color: #000000">(</span><span style="color: #ff4500">$server2</span><span style="color: #000000">)</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">=</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$true</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #ff4500">$result</span><span style="color: #000000">&#160;</span><span style="color: #a9a9a9">+=</span><span style="color: #000000">&#160;</span><span style="color: #ff4500">$kbinfo</span>              <br /><span style="color: #000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: #000000">}</span>              <br /><span style="color: #000000">&#160;&#160;&#160; </span><span style="color: #000000">}</span><span style="color: #000000">&#160;</span><span style="color: #006400"># End Switch</span>              <br /><span style="color: #000000">&#160; </span><span style="color: #000000">}</span><span style="color: #000000">&#160;</span><span style="color: #006400"># End foreach</span>              <br /><span style="color: #000000">&#160;&#160; </span><span style="color: #ff4500">$result</span>              <br /><span style="color: #000000">&#160;</span><span style="color: #000000">}</span><span style="color: #000000">&#160;</span><span style="color: #006400"># End Function</span>              </div>
</td>
</tr>
</tbody>
</table></div>
</p>
</p>
</p>
<p>This code is also available on the <a href="http://gallery.technet.microsoft.com/ScriptCenter/en-us/b01c1fab-b16b-4784-9249-63d3488b70b7">TechNet Code Gallery</a> and up on <a href="http://poshcode.org/1296">PoshCode</a></p>
]]></content:encoded>
			<wfw:commentRss>http://get-powershell.com/2009/08/28/comparing-installed-hotfixes-on-servers/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
