Search code examples
powershelladamcompareobject

Compare more than two strings


Here is what I am trying to achieve...

I have to view the ADAM db in VMWARE to see the replication times. My question is how would I compare more than two strings using the compare-object command. I cannot find any articles on more than two values.

This is what I started writing. I am trying to make this as dynamic as possible...

#PORT FOR LDAP
$ldap = 389;

#PATH
$path = 'DC=vdi,DC=vmware,DC=int';

#SERVERS
$vm = @("fqdn" , "fqdn" , "fqdn");

#ARRAY FOR LOOP
$comp = @();

#LOOP FOR ARRAY COMPARE
for($i = 1; $i -le $vm.count; $i++)
{
    $comp += repadmin.exe /showrepl $svr":"$ldap $path | Select-String "Last attempt";
}

#CREATE DYNAMIC VARIABLES
for($i = 0; $i -le ($comp.count - 1); $i++)
{
    New-Variable -name repl$i -Value $comp[$i];
}

Thank you in advanced!!!


Solution

  • As I mentioned in my comment, your question is too vague for us to provide a good answer for your situation, so I'll focus on "compare more than two strings". To do this, I wuold recommend Group-Object. Ex.

    $data = @"
    ==== INBOUND NEIGHBORS ======================================
    
    CN=Configuration,CN={B59C1E29-972F-455A-BDD5-1FA7C1B7D60D}
        ....
            Last attempt @ 2010-05-28 07:29:34 was successful.
    
    CN=Schema,CN=Configuration,CN={B59C1E29-972F-455A-BDD5-1FA7C1B7D60D}
        ....
            Last attempt @ 2010-05-28 07:29:34 was successful.
    
    OU=WSFG,DC=COM
        ....
            Last attempt @ 2010-05-28 07:29:35 failed, result -2146893008
    (0x8009033
    0):
    "@ -split [environment]::NewLine
    
    $comp = $data | Select-String "Last attempt"
    
    $comp | Group-Object
    
    Count Name                                                                  Group                
    ----- ----                                                                  -----                
        2         Last attempt @ 2010-05-28 07:29:34 was successful.            {        Last atte...
        1         Last attempt @ 2010-05-28 07:29:35 failed, result -2146893008 {        Last atte...
    

    Group-Object and PowerShell is very flexible, so you could customize this to ex. display the servernames and status for the servers that wasn't equal to the rest (ex. count = 1 or not in any of the biggest groups) etc., but I won't spend more time going into details because I have no idea of what you are trying to achieve, so I'll probably just waste both of ours time.

    Summary: What I can tell you is the I would proabably (99% sure) use Group-Object to "compare more than two strings".