Find the missing machine

This topic contains 8 replies, has 3 voices, and was last updated by  carl trevett 1 year, 8 months ago.

  • Author
  • #55079

    carl trevett


    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?


  • #55080

    Don Jones

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

  • #55081

    carl trevett

    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

  • #55082

    Don Jones

    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.

  • #55086

    carl trevett

    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:

    Vm1, ,vm1
    Vm2,vm2, ,vm2

    This way will be nice and easy to compare.

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

  • #55091

    carl trevett

    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

  • #55094

    Don Jones

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

  • #55102

    Rob Simmers

    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 ",")
  • #55103

    carl trevett

    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

    vm1,    ,vm1,  
    vm2,vm2,    ,vm2

    guessing this might not be possible?

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

You must be logged in to reply to this topic.