Ezine 163 - Employ Compare-Object to Evaluate DifferencesEzine 163 - Employ Compare-Object to Evaluate DifferencesIt is surprising how often when we troubleshoot computer problems it's helpful to compare two files. What I have in mind is playing detective where I examine the 'before' and the 'after' a crucial event. Topics for PowerShell's Compare-Object
♣ This Week's SecretThis week's PowerShell secret is never miss a chance to examine parameters in general and -syncWindow in particular. Always take the opportunity to study the parameters of any new PowerShell command; I guarantee that whenever you try 'help verb-noun' with the -full switch, you will learn something useful. Here is an example: when I created a script featuring compare-Object the list of differences was much smaller than I anticipated. Moreover, I knew there were differences between the two files which were not showing up in the output. Once I discovered compare-Object had a switch called -syncWindow, I got a complete list of all the differences - problem solved. Guy's Mission to Generate a List of DifferencesIn this mission I had a directory of files. Some of those files were missing a phrase, I wanted a list of files with these deficiencies. Stage one was to create a text file containing a directory listing. Stage two was to create another file listing only those files containing a particular phrase. Stage three was to call for compare-Object, process the two files and create a third file with a list of all the files that were missing my particular phrase. In these ezine I just want to focus on stage three, generating a list of differences. Preamble, Call for HelpThis script is a useful reminder to see compare-Object's parameters. # Microsoft PowerShell script to research
compare-Object help compare-Object -full Note 1: With compare-Object, the two required parameters are -referenceObject and -differenceObject. In most examples -referenceObject controls the master content, which holds ALL the information, while -differenceObject has the 'after the event', or 'second' file. Also note what help says about these parameters, what we learn is that that the position of these parameters is important, reference list first, difference list second. Note 2: In a production script we employ get-Content to open the file and read it. What I do is assign this part of the script to a variable, for example $strReference = get-Content "D:\scripts\list.txt" Note 3: Please make note of other parameters such as -includeEqual and -syncWindow. Example 1: To Compare Two Files, and List DifferencesFor the sake of clarity, and so that we focus on compare-Object, I have scaled down our mission to just tackle comparing two files. The best way to understand my example is if you immerse yourself in my shopping list scenario. Preparation BeforeShopList: Save these 7 items into a text file, I called mine C:\temp\BeforeShopList.txt Oranges AfterShopCheckout: Save these 6 items into a text file, I called mine C:\temp\AfterShopCheckout.txt Oranges Now we are ready to execute this compare-Object script. My favoured method is to save these instructions into a cmdlet with a .ps1 extension, then call the file from the PowerShell command line. Alternatively, copy the following lines and then paste into the command line. # Microsoft PowerShell script to research compare-Object clear-host Note 1: You could use the explicit parameters: -referenceObject
-differenceObject and thus substitute this for the last line: The reason that I omitted these positional parameters is that PowerShell can deduce what to compare from the sequence. Note 2: If your script fails, then check a) That you have two files, b) They are in the folder referenced by your script. Challenge 1: You could add the -includeEqual parameter to display all the items that you bought that were on the original list. Expected result InputObject SideIndicator SideIndicator I wonder if it's me, but when I read this word quickly it says, 'Site' indicator and not 'Side' indicator. => Means the InputObject is present in the difference (second) file, but not in the first file. In this scenario it means we bought stuff, which was not on the original list. <= Present in reference (first) file, but not in the second file. In my analogy, we forgot to buy this item on our shopping list. == Present in both files. Try my challenge and add the -includeEqual parameter, then you will see this double-equals underneath 'SideIndicator' in the output. Guy Recommends:
The Free IP Address Tracker (IPAT)
|
|||||
Custom Search
|
Guy Recommends: WMI Monitor and It's Free!
|
|
Home Copyright © 1999-2012 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |