locked
Compare strings RRS feed

  • Question

  • Hello,

    ive quite a beginner question  (I'm hitting a wall for more than 20mins though)

    I'm writing a script for OSD (Operating system deployment).  Part of the script must decide, it the server has 4 physical drives in correct order or 8.

    => ctrl all show config
    
    Smart Array P410i in Slot 0 (Embedded)
       array A (SAS, Unused Space: 241875 MB)
    
    
          logicaldrive 1 (150.0 GB, RAID 5, OK)
          logicaldrive 2 (50.0 GB, RAID 5, OK)
          logicaldrive 3 (1000.0 GB, RAID 5, OK)
    
          physicaldrive 1I:1:1 (port 1I:box 1:bay 1, SAS, 300 GB, OK)
          physicaldrive 1I:1:2 (port 1I:box 1:bay 2, SAS, 300 GB, OK)
          physicaldrive 1I:1:3 (port 1I:box 1:bay 3, SAS, 300 GB, OK)
          physicaldrive 2I:1:5 (port 2I:box 1:bay 5, SAS, 300 GB, OK)
          physicaldrive 2I:1:6 (port 2I:box 1:bay 6, SAS, 300 GB, OK)
          physicaldrive 2I:1:7 (port 2I:box 1:bay 7, SAS, 300 GB, OK)

    So I'm interested in part 1I:1:1; 1I:1:2 ..

    So, I extracted the parts this way and trying to compare them:

    $result = (.\hpacucli.exe ctrl all show config |Select-String "Physicaldrive").Line.trim() |%{($_.split(" "))[1]}
    
    
    $cfg1 = @'
    1I:1:1
    1I:1:2
    1I:1:3
    2I:1:5
    2I:1:6
    2I:1:7
    '@
    $cfg1 = $cfg1.Split() |?{$_ -ne ""}
    
    $result |select *
    
    Length
    ------
         6
         6
         6
         6
         6
         6
    
    $cfg1 |select *
    
    Length
    ------
         6
         6
         6
         6
         6
         6
    
    $result.Equals($cfg1)
    False
    Both looks the same, both are system.string with same length, still false  ..  what am I missing ...<

    $result
    1I:1:1
    1I:1:2
    1I:1:3
    2I:1:5
    2I:1:6
    2I:1:7
    
    $result |gm
       TypeName: System.String
    
    $cfg1 |gm
       TypeName: System.String



    • Edited by Mekac Friday, March 23, 2018 12:26 PM
    Friday, March 23, 2018 12:22 PM

Answers

  • What is it you are asking?  If you are trying to extract the data use RegEx.

    The following is an example. You will have to decide how you need to do this and adjust the RegEx patterns.

    $results = @'
    Smart Array P410i in Slot 0 (Embedded)
       array A (SAS, Unused Space: 241875 MB)
    
    
          logicaldrive 1 (150.0 GB, RAID 5, OK)
          logicaldrive 2 (50.0 GB, RAID 5, OK)
          logicaldrive 3 (1000.0 GB, RAID 5, OK)
    
          physicaldrive 1I:1:1 (port 1I:box 1:bay 1, SAS, 300 GB, OK)
          physicaldrive 1I:1:2 (port 1I:box 1:bay 2, SAS, 300 GB, OK)
          physicaldrive 1I:1:3 (port 1I:box 1:bay 3, SAS, 300 GB, OK)
          physicaldrive 2I:1:5 (port 2I:box 1:bay 5, SAS, 300 GB, OK)
          physicaldrive 2I:1:6 (port 2I:box 1:bay 6, SAS, 300 GB, OK)
          physicaldrive 2I:1:7 (port 2I:box 1:bay 7, SAS, 300 GB, OK)
    '@
    $results | 
        Where-Object{$_ -match 'physicaldrive'} |
            ForEach-Object{
                [pscustomobject]@{
                    Drive = $(if($_ -match '(\dI:\d:\d)'){$matches[1]})
                    Port = $(if($_ -match 'Port (\d.)'){$matches[1]})
                    Bay = $(if($_ -match 'Bay (\d)'){$matches[1]})
                }
            }
    


    \_(ツ)_/

    • Marked as answer by Mekac Friday, March 23, 2018 7:48 PM
    Friday, March 23, 2018 12:48 PM
    • Marked as answer by Mekac Friday, March 23, 2018 10:41 PM
    Friday, March 23, 2018 2:05 PM
  • Today I'm ending with this solution

    $result = (.\hpacucli.exe ctrl all show config |Select-String "Physicaldrive").Line.trim() |%{($_.split(" "))[1]}
    
    
    $cfg1 = @'
    1I:1:1
    1I:1:2
    1I:1:3
    2I:1:4
    2I:1:6
    2I:1:7
    '@
    $cfg1 = $cfg1.Split() |?{$_ -ne ""}
    
    $cfg2 = @'
    1I:1:1
    1I:1:2
    1I:1:3
    2I:1:5
    2I:1:6
    2I:1:7
    '@
    $cfg2 = $cfg2.Split() |?{$_ -ne ""}
    
    
    $sb_cfg1 = {
    
    for ($i =0;$i -le $cfg1.Count;$i++)
    {
    if ($result[$i] -eq $cfg1[$i])
     {
        $judge = $true
     }
    else
     {
        $judge = $false
        break
     }
    }
    $judge
    }
    
    
    $sb_cfg2 = {
    
    for ($i =0;$i -le $cfg2.Count;$i++)
    {
    if ($result[$i] -eq $cfg2[$i])
     {
        $judge = $true
     }
    else
     {
        $judge = $false
        break
     }
    }
    $judge
    }
    
    if ($(Invoke-Command $sb_cfg1) -eq "True") 
    {
        $smsbay = 1
        Write-Verbose "CFG1 configuration is valid" -Verbose
    }
    elseif ($(Invoke-Command $sb_cfg2) -eq "True")
    {
        $smsbay = 2
        Write-Verbose "CFG2 is valid" -Verbose}
    else 
    {
        $smsbay = "error"
        Write-Verbose "No valid DiskArray configuration" -Verbose
    }
     
    

    • Marked as answer by Mekac Friday, March 23, 2018 7:48 PM
    Friday, March 23, 2018 3:05 PM

All replies

  • What is it you are asking?  If you are trying to extract the data use RegEx.

    The following is an example. You will have to decide how you need to do this and adjust the RegEx patterns.

    $results = @'
    Smart Array P410i in Slot 0 (Embedded)
       array A (SAS, Unused Space: 241875 MB)
    
    
          logicaldrive 1 (150.0 GB, RAID 5, OK)
          logicaldrive 2 (50.0 GB, RAID 5, OK)
          logicaldrive 3 (1000.0 GB, RAID 5, OK)
    
          physicaldrive 1I:1:1 (port 1I:box 1:bay 1, SAS, 300 GB, OK)
          physicaldrive 1I:1:2 (port 1I:box 1:bay 2, SAS, 300 GB, OK)
          physicaldrive 1I:1:3 (port 1I:box 1:bay 3, SAS, 300 GB, OK)
          physicaldrive 2I:1:5 (port 2I:box 1:bay 5, SAS, 300 GB, OK)
          physicaldrive 2I:1:6 (port 2I:box 1:bay 6, SAS, 300 GB, OK)
          physicaldrive 2I:1:7 (port 2I:box 1:bay 7, SAS, 300 GB, OK)
    '@
    $results | 
        Where-Object{$_ -match 'physicaldrive'} |
            ForEach-Object{
                [pscustomobject]@{
                    Drive = $(if($_ -match '(\dI:\d:\d)'){$matches[1]})
                    Port = $(if($_ -match 'Port (\d.)'){$matches[1]})
                    Bay = $(if($_ -match 'Bay (\d)'){$matches[1]})
                }
            }
    


    \_(ツ)_/

    • Marked as answer by Mekac Friday, March 23, 2018 7:48 PM
    Friday, March 23, 2018 12:48 PM
  • We are expecting new servers.. my script should verify if the HDDs are in correct Bays .. servers will have 2 configurations   (4 disks and 8 disks)

    So.. I thought I put correct configuration in $cfg1 here-string variable, and compare the output with report from hpacucli ..  

    Need to match whole $result with $cfg1   (by human eye they looks the same, but computer thinks otherwise)

    Thanks for splitting the columns!!!


    Simply put:  need to determine, if data in the $result are the same as in $cfg1
    • Edited by Mekac Friday, March 23, 2018 1:59 PM
    Friday, March 23, 2018 12:58 PM
    • Marked as answer by Mekac Friday, March 23, 2018 10:41 PM
    Friday, March 23, 2018 2:05 PM
  • Read the Remarks section: https://msdn.microsoft.com/en-us/library/bsc2ak47(v=vs.110).aspx

    Well if that isn't the best explanation I have ever seen then ....

    Convert the text to objects and compare the objects properties as needed.  That is the simplest approach to what you are doing.  You need a structured comparison and a set of rules to use for the comparison.  Full objects that describe the drives and their assignment would be the only way to go and it would be the easiest reliable method.


    \_(ツ)_/

    Friday, March 23, 2018 2:11 PM
  • Well I try to do my best

    Now I'm trying comparing the single elements

    for ($i =0;$i -le $cfg1.Count;$i++)
    {
    if ($result[$i] -eq $cfg1[$i])
     {
        $judge = $true
     }
    else
     {
        $judge = $false
        break
     }
    }
    
    
    $judge
    Which.. so far.. gives me correct True and False results

    Friday, March 23, 2018 2:16 PM
  • Today I'm ending with this solution

    $result = (.\hpacucli.exe ctrl all show config |Select-String "Physicaldrive").Line.trim() |%{($_.split(" "))[1]}
    
    
    $cfg1 = @'
    1I:1:1
    1I:1:2
    1I:1:3
    2I:1:4
    2I:1:6
    2I:1:7
    '@
    $cfg1 = $cfg1.Split() |?{$_ -ne ""}
    
    $cfg2 = @'
    1I:1:1
    1I:1:2
    1I:1:3
    2I:1:5
    2I:1:6
    2I:1:7
    '@
    $cfg2 = $cfg2.Split() |?{$_ -ne ""}
    
    
    $sb_cfg1 = {
    
    for ($i =0;$i -le $cfg1.Count;$i++)
    {
    if ($result[$i] -eq $cfg1[$i])
     {
        $judge = $true
     }
    else
     {
        $judge = $false
        break
     }
    }
    $judge
    }
    
    
    $sb_cfg2 = {
    
    for ($i =0;$i -le $cfg2.Count;$i++)
    {
    if ($result[$i] -eq $cfg2[$i])
     {
        $judge = $true
     }
    else
     {
        $judge = $false
        break
     }
    }
    $judge
    }
    
    if ($(Invoke-Command $sb_cfg1) -eq "True") 
    {
        $smsbay = 1
        Write-Verbose "CFG1 configuration is valid" -Verbose
    }
    elseif ($(Invoke-Command $sb_cfg2) -eq "True")
    {
        $smsbay = 2
        Write-Verbose "CFG2 is valid" -Verbose}
    else 
    {
        $smsbay = "error"
        Write-Verbose "No valid DiskArray configuration" -Verbose
    }
     
    

    • Marked as answer by Mekac Friday, March 23, 2018 7:48 PM
    Friday, March 23, 2018 3:05 PM
  • Well if that isn't the best explanation I have ever seen then ....

    What was wrong with it? Not enough snarky and insulting comments to meet your standards?

    Here is another example that shows why the Equals method will not work for this usage.

    $a = $b = @(1, 2, 3)
    $c = @(1, 2, 3)
    $a.Equals($b) # True
    $a.Equals($c) # False
    $b.Equals($c) # False

    Friday, March 23, 2018 8:58 PM
  • Well if that isn't the best explanation I have ever seen then ....

    What was wrong with it? Not enough snarky and insulting comments to meet your standards?


    No. It was a good explanation nd one that I hadn't found yet.

    Don't mistake my dry humor for "snarkiness".  It is just my country-boy out-on-the-range cowboy humor.  I grew up in the wild and lived in the great outdoors.  I didn't learn to worry about people having fragile egos. 

    When working as a member or leader of many programming teams we also "busted chops" a bit with the understanding that we all need to learn the lingo and technology on our own.  Now everything is learned in a forum.  No one gives a damn about knowing the technology.  That also causes me to poke fun at time.

    No.  You misunderstood.  My line was what it was.  No humor  or criticism.   Read between the lines but, when their is only one line, what can you read?


    \_(ツ)_/

    Friday, March 23, 2018 9:06 PM
  • No.  You misunderstood.  My line was what it was.  No humor  or criticism.   Read between the lines but, when their is only one line, what can you read?


    \_(ツ)_/

    My apologies. I thought it was sarcasm due to the shortness of my post or something like that.
    Friday, March 23, 2018 9:26 PM