Q: check if a PScustomobject contains a certain pattern

This topic contains 6 replies, has 3 voices, and was last updated by  GregW 2 years, 11 months ago.

  • Author
    Posts
  • #21243

    CTX_Christian
    Participant

    Hi NG,

    I got a pscustomobject name = $catalogselection with 1 property = brokercatalog

    Here's the | gm output:

    TypeName: Selected.System.Management.Automation.PSCustomObject

    Name MemberType Definition
    —- ———- ———-
    Equals Method bool Equals(System.Object obj)
    GetHashCode Method int GetHashCode()
    GetType Method type GetType()
    ToString Method string ToString()
    brokercatalog NoteProperty System.String brokercatalog=MyCatalog-N01

    The content of the pscustomobject is sort of this:

    MyCatalog-N01-STANDARD
    MyCatalog-N65-PREMIUM
    ...and so on.

    I would like to check against this pscustomobject now whether it contains a certain pattern. I tried -match and -contains but both fail. I read in the about_Comparison_Operators and found that -match does only support strings...

    My aim is to get a true or false return if I query the pscustomupject like this:

    $catalogselection -contains "N01", "PREMIUM"

    My expectation = false

    $catalogselection -contains "N65", "PREMIUM"

    My expectation = true

    I'm really out of ideas here on howto get this solved.

    Regards

    Christian

  • #21244

    Don Jones
    Keymaster

    Well, you can't match the entire object, you have to match one of its properties.

    $obj.brokerCatalog -match "whatever"

    For example. The -Contains operator doesn't do pattern matching.

    • #21246

      CTX_Christian
      Participant

      Hi Don,

      thanks for your fast reply. Well at the end of the day I just want to know if my pattern matches any of the objects in the array.

      Which way should I best follow up to get this solved? When I use match I get the following:

      PS $catalogselection.brokercatalog -match "N01"

      PS MyCatalog-N01-STANDARD

      If I try this match:

      PS $catalogselection.brokercatalog -match "N01", "STANDARD"

      It returns nothing. No True and no False.

      Regards

      Christian

  • #21251

    GregW
    Participant

    Not sure I understand your requirements fully, but the -match operator is interesting and I have been wanting to learn regex...

    Here are some interesting results... Your example PS $catalogselection.brokercatalog -match "N01", "STANDARD" syntax isn't something I could find, but I think I duplicated the sentiment:

    May not fit your use case, but this definitely gives me a reason to learn regex. I gave my objects two properties since it seems like a one property object would be better served by an array of strings, so I wanted it to fit what I see more.

    .* – match wild string
    | – OR

    $test=new-object pscustomobject -property @{"broker"="blahblah";"note"="one"}
    $test2=new-object pscustomobject -property @{"broker"="yadayadayada";"note"="two"}
    $test3=new-object pscustomobject -property @{"broker"="whosits";"note"="three"}
    $arry=@($test,$test2,$test3)
    $arry
    $arry -match "bla"
    $arry -match "bl.*h"
    $arry -match "bl|ya"
    $arry -match "bl|re"
    
  • #21259

    GregW
    Participant

    So, to be more specific, I think your command would be PS $catalogselection.brokercatalog -match "N01.*STANDARD" if you wanted every item that contains both substrings and
    PS $catalogselection.brokercatalog -match "N01|STANDARD" if you wanted either of them.

  • #21263

    CTX_Christian
    Participant

    Hi Greg,

    thanks. So the dot notation also works if I want to match multiple values against a string array.

    Regards

    Christian

  • #21320

    GregW
    Participant

    Hi Christian,

    I'm definitely an amateur with the regex, so it's important to note the limited scope of my example, but this works with a string array too. Sequence is important in this example, but I know there are ways to ignore the sequence... I'll look for one of those, but see this example:

    [E:\g.working\regex]
    #$arry=@("Blue","Red","Yellow","Green","Purple","Orange","White","Black")

    [E:\g.working\regex]
    #$arry -match "e"
    Blue
    Red
    Yellow
    Green
    Purple
    Orange
    White

    [E:\g.working\regex]
    #$arry -match "l.*e"
    Blue
    Purple

    [E:\g.working\regex]
    #$arry -match "e.*l"
    Yellow

    $arry=@("Blue","Red","Yellow","Green","Purple","Orange","White","Black")
    $arry
    $arry -match "e" # Match strings containing "e"
    $arry -match "l.*e" # Match strings with "l" followed by "e"
    $arry -match "e.*l" # Match strings with "e" followed by "l"
    

You must be logged in to reply to this topic.