Author Posts

October 7, 2016 at 3:44 pm

Hi

I have 4 sets of data (machine names from different products) the machine names should exist in all 4 data sets. I am trying to output the data into 4 columns and if a machine name is missing from 1 data set a blank line is left so all results are aligned and easy to spot anything missing. Can someone please help?

Thanks
Carl

October 7, 2016 at 3:48 pm

Not knowing what the data looks like, I'd ordinarily use Compare-Object.

October 7, 2016 at 3:53 pm

4 arrays(string) with machine names

$1 = vm1,vm2,vm3,vm4
$2 = vm2,vm3,vm4,vm5
$3 = vm1,vm3,vm4,vm5
$4 = vm2,vm3,vm4,vm5

something like the above, I thought with compare-object you can only have one reference and one difference object

October 7, 2016 at 3:58 pm

1. Concatenate all the arrays into one.
2. Run the combined array through Select -Unique
3. The combined array now contains all possible names
4. Compare each original array to the combo array to see what each one is missing.

October 7, 2016 at 5:07 pm

Thanks that makes sense, don't even want to let you know my ideas around it. Is there anyway to plot the data in csv:

$1,$2,$3,$4
Vm1, ,vm1
Vm2,vm2, ,vm2
Vm3,vm3,vm3,vm3
Vm4,vm4,vm4,vm4
,vm5,vm5,vm5

This way will be nice and easy to compare.

Probably taking the mick now, but thanks for all your help.

October 7, 2016 at 6:57 pm

just got home and completed your 4 steps, you are right that does give me what's missing from each original array. But for my clean up task I need to know which arrays they exist in so I can delete those objects before I can deploy the machines again....hope that makes sense

October 7, 2016 at 7:36 pm

Just add some verbose output. "Now checking array 1..." etc.

October 7, 2016 at 10:59 pm

It sounds like you are close. Here is a basic example of the steps:

$1 = "vm1", "vm2", "vm3", "vm4"
$2 = "vm2", "vm3", "vm4", "vm5"
$3 = "vm1", "vm3", "vm4", "vm5"
$4 = "vm2", "vm3", "vm4", "vm5"

$arrToCompare = $1, $2, $3, $4
#Concatenate all the arrays into one.
$combined = $1 + $2 + $3 + $4
#Run the combined array through Select -Unique (and ignore null values)
#The combined array now contains all possible names
$unique  = $combined | Where{ $_ } |Select -Unique

#Compare each original array to the combo array to see what each one is missing.
foreach ($arr in $arrToCompare) {
    
    $test = Compare-Object -ReferenceObject $arr -DifferenceObject $unique | Select -ExpandProperty InputObject
    "{0} does not contain {1}" -f ($arr -join ","), (@($test) -join ",")

}

October 8, 2016 at 6:44 am

Thanks Rob, that looks good but only issue is I'm dealing with 5000+ machines so the output is messy. still keen to get in a CSV format, like the below

set1,set2,set3,set4
vm1,    ,vm1,  
vm2,vm2,    ,vm2
vm3,vm3,vm3,vm3
vm4,vm4,vm4,vm4
   ,vm5,vm5,vm5

guessing this might not be possible?

  • This reply was modified 1 year, 11 months ago by  carl trevett.