Comparing arrays

by mcmiv413 at 2012-12-27 08:07:52

I’m not sure if this can be done as I want to do it but it is worth asking I think.

I’m working on a series of scripts for the purpose of reporting on the configuration settings for multiple servers. Dll versions, app pool settings, config files, etc. I have the scripts in place to gather and report the information already. What I’m having difficulty with is moving on to the next step which is to compare the reports to the "gold standard" config. This way if there is something on any of the servers that does not match our standard we will know about it. I’m trying to write it in such a way that I could take any 2 sets of data (gold and server config) and compare their values. Anything that does not match the gold would be included in an email indicating that there is a configuration mismatch.

Now I think I can write this script but it seems like one that can quickly become overly complicated. I tried using compare-object but that really doesn’t seem to give me the information I need.

Here is some sample data of what I’m looking to compare:

Gold:

Name,is32bit,PrivateMemLimit,Interval
AppPool1,FALSE,250000,1740
AppPool2,FALSE,2000000,1740
AppPool3,FALSE,1700000,1740
AppPool4,FALSE,2500000,720
AppPool5,FALSE,2000000,720
AppPool6,FALSE,2000000,720
AppPool7,FALSE,515000,1740
AppPool8,FALSE,1048576,1740

Server Config:

Server,Name,is32Bit,PrivateMemLimit,Interval
Server1,AppPool1,FALSE,250000,1740
Server2,AppPool1,FALSE,250000,1740
Server1,AppPool4,FALSE,2500000,720
Server2,AppPool4,FALSE,0,1740
Server1,AppPool2,FALSE,2000000,1740
Server2,AppPool2,FALSE,2000000,1740
Server1,AppPool5,FALSE,2000000,1740
Server2,AppPool5,FALSE,0,720
Server1,AppPool7,FALSE,515000,1740
Server2,AppPool7,FALSE,515000,1740
Server1,AppPool8,FALSE,1048576,1740
Server2,AppPool8,FALSE,1048576,1740
Server1,AppPool3,FALSE,1700000,1740
Server2,AppPool3,FALSE,1700000,1740
Server1,AppPool6,FALSE,1700000,720
Server2,AppPool6,FALSE,2000000,720

To compare the data I’d like to key off of the App Pool name.

Does anyone have a suggestion to point me in the right direction here? Thanks in advance.

Michael
by vandreytrindade at 2012-12-27 10:27:58
Hi mcmiv413,

I don’t know if this can help you but, let’s try right? =]

I’ve created two files:

C:\test.txt (With the first CSV content)

Name,is32bit,PrivateMemLimit,Interval
AppPool1,FALSE,250000,1740
AppPool2,FALSE,2000000,1740
AppPool3,FALSE,1700000,1740
AppPool4,FALSE,2500000,720
AppPool5,FALSE,2000000,720
AppPool6,FALSE,2000000,720
AppPool7,FALSE,515000,1740
AppPool8,FALSE,1048576,1740


C:\test2.txt (With the second CSV content)


Server,Name,is32Bit,PrivateMemLimit,Interval
Server1,AppPool1,FALSE,250000,1740
Server2,AppPool1,FALSE,250000,1740
Server1,AppPool4,FALSE,2500000,720
Server2,AppPool4,FALSE,0,1740
Server1,AppPool2,FALSE,2000000,1740
Server2,AppPool2,FALSE,2000000,1740
Server1,AppPool5,FALSE,2000000,1740
Server2,AppPool5,FALSE,0,720
Server1,AppPool7,FALSE,515000,1740
Server2,AppPool7,FALSE,515000,1740
Server1,AppPool8,FALSE,1048576,1740
Server2,AppPool8,FALSE,1048576,1740
Server1,AppPool3,FALSE,1700000,1740
Server2,AppPool3,FALSE,1700000,1740
Server1,AppPool6,FALSE,1700000,720
Server2,AppPool6,FALSE,2000000,720


Then, I imported the CSV files to some variables:

$file1 = Import-CSV C:\Teste.txt
$file2 = Import-CSV C]

Now I removed the column that you don’t want to use:

$file2 = $file2 | select Name,is32bit,PrivateMemLimit,Interval

So I could compare the data in the files:

Compare-Object -ReferenceObject $file1 -DifferenceObject $file2

And I’ve got the result:

InputObject SideIndicator
----------- -------------
@{Name=AppPool7; is32Bit=FALSE; Priv… =>
@{Name=AppPool7; is32Bit=FALSE; Priv… =>
@{Name=AppPool8; is32Bit=FALSE; Priv… =>
@{Name=AppPool8; is32Bit=FALSE; Priv… =>
@{Name=AppPool3; is32Bit=FALSE; Priv… =>
@{Name=AppPool3; is32Bit=FALSE; Priv… =>
@{Name=AppPool6; is32Bit=FALSE; Priv… =>
@{Name=AppPool6; is32Bit=FALSE; Priv… =>


Let me know if it helped =]
by mcmiv413 at 2012-12-27 10:37:29
I think I solved my own problem, here is what I have so far:

[code2=powershell]$gold = import-csv C:\temp\gold.csv
$current = Import-Csv C:\temp\current.csv
$key = 'Name'
$differences = @()
$header = @()

foreach ($noteproperty in $($gold | Get-Member | where {$.membertype -like "noteproperty"}) ) {$header += $noteproperty.name}

foreach ($goldconfig in $gold)
{
$compare = $current | where {$
.name -like $goldconfig.name}
foreach ($item in $compare)
{
foreach ($column in $header)
{
$diff = New-Object System.Object
if ($item.$column -like $goldconfig.$column)
{}
else
{
$diff | Add-Member -type NoteProperty -name Server -value $item.server
$diff | Add-Member -type NoteProperty -name $key -value $($item.$key)
$diff | Add-Member -type NoteProperty -name Value -value $("$Column = $($item.$column)")
$diff | Add-Member -type NoteProperty -name Correction -value $($goldconfig.$column)

$differences += $diff
}
}

}
}

$differences | sort server,$key[/code2]

This gives me a table like this:

Server,Name,Value,Correction
------,----,-----,----------
Server1,AppPool5,Interval = 1740,720
Server1,AppPool6,PrivateMemLimit = 1700000,2000000
Server2,AppPool4,Interval = 1740,720
Server2,AppPool4,PrivateMemLimit = 0,2500000
Server2,AppPool5,PrivateMemLimit = 0,2000000

Sorry I don;t know how to get the table to display nicely here so I added some commas to it to make it decipherable.

Looks like this will give me what I’m looking for, the next step will be to set it up for parameters to be passed to it and to have it send out and html formated email with the results.

Let me know if there is something I could be doing better with this as it still feels a bit messy to me.

Michael
by mcmiv413 at 2012-12-27 10:41:16
[quote="vandreytrindade"]Hi mcmiv413,

I don’t know if this can help you but, let’s try right? =]

I’ve created two files:

C:\test.txt (With the first CSV content)

Name,is32bit,PrivateMemLimit,Interval
AppPool1,FALSE,250000,1740
AppPool2,FALSE,2000000,1740
AppPool3,FALSE,1700000,1740
AppPool4,FALSE,2500000,720
AppPool5,FALSE,2000000,720
AppPool6,FALSE,2000000,720
AppPool7,FALSE,515000,1740
AppPool8,FALSE,1048576,1740


C:\test2.txt (With the second CSV content)


Server,Name,is32Bit,PrivateMemLimit,Interval
Server1,AppPool1,FALSE,250000,1740
Server2,AppPool1,FALSE,250000,1740
Server1,AppPool4,FALSE,2500000,720
Server2,AppPool4,FALSE,0,1740
Server1,AppPool2,FALSE,2000000,1740
Server2,AppPool2,FALSE,2000000,1740
Server1,AppPool5,FALSE,2000000,1740
Server2,AppPool5,FALSE,0,720
Server1,AppPool7,FALSE,515000,1740
Server2,AppPool7,FALSE,515000,1740
Server1,AppPool8,FALSE,1048576,1740
Server2,AppPool8,FALSE,1048576,1740
Server1,AppPool3,FALSE,1700000,1740
Server2,AppPool3,FALSE,1700000,1740
Server1,AppPool6,FALSE,1700000,720
Server2,AppPool6,FALSE,2000000,720


Then, I imported the CSV files to some variables:

$file1 = Import-CSV C:\Teste.txt
$file2 = Import-CSV C]

Now I removed the column that you don’t want to use:

$file2 = $file2 | select Name,is32bit,PrivateMemLimit,Interval

So I could compare the data in the files:

Compare-Object -ReferenceObject $file1 -DifferenceObject $file2

And I’ve got the result:

InputObject SideIndicator
----------- -------------
@{Name=AppPool7; is32Bit=FALSE; Priv… =>
@{Name=AppPool7; is32Bit=FALSE; Priv… =>
@{Name=AppPool8; is32Bit=FALSE; Priv… =>
@{Name=AppPool8; is32Bit=FALSE; Priv… =>
@{Name=AppPool3; is32Bit=FALSE; Priv… =>
@{Name=AppPool3; is32Bit=FALSE; Priv… =>
@{Name=AppPool6; is32Bit=FALSE; Priv… =>
@{Name=AppPool6; is32Bit=FALSE; Priv… =>


Let me know if it helped =][/quote]

Thanks vandreytrindade but unfortunately that was one of the first methods I attempted and it is not giving the results I need. I need to know specifically which values are different and the compart-object is just listing out a hash of the values of each number of the array, not doing a sub-property comparission. I just posted what I think will get me the results I’m looking for but I’m open to other suggestions. There are always better ways of doing things :slight_smile:
by vandreytrindade at 2012-12-28 02:34:02
No problem!
It’s good to know that u find the answer =]

Cya