Find the missing machine

Welcome Forums General PowerShell Q&A Find the missing machine

This topic contains 8 replies, has 3 voices, and was last updated by

 
Participant
2 years, 6 months ago.

  • Author
    Posts
  • #55079

    Participant
    Topics: 4
    Replies: 9
    Points: 0
    Rank: Member

    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

  • #55080

    Keymaster
    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

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

  • #55081

    Participant
    Topics: 4
    Replies: 9
    Points: 0
    Rank: Member

    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

    Keymaster
    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    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

    Participant
    Topics: 4
    Replies: 9
    Points: 0
    Rank: Member

    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.

  • #55091

    Participant
    Topics: 4
    Replies: 9
    Points: 0
    Rank: Member

    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

    Keymaster
    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

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

  • #55102

    Participant
    Topics: 8
    Replies: 1190
    Points: 639
    Helping Hand
    Rank: Major Contributor

    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

    Participant
    Topics: 4
    Replies: 9
    Points: 0
    Rank: Member

    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?

The topic ‘Find the missing machine’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort