Welcome Forums General PowerShell Q&A First If statement not detecting, or ElseIf overwriting?

Viewing 1 reply thread
  • Author
    Posts
    • #167509
      Participant
      Topics: 1
      Replies: 2
      Points: 18
      Rank: Member

      Hi there! I have a script for work that runs an lookup in AD for computer names. We have a lot of computers and servers, and so I put a lot of conditions as to which naming conventions to ignore. For some reason, a certain naming convention keeps getting past my original (first) If statement, and passes it onto the next ElseIf’s. Here’s a the part of the code in question.

      (#####Comments explain context#####)

      https://gist.github.com/Mazarith/2bc9b8f32a817da26721e8843b6225b0

      The naming convention that is somehow defying the original if statement (which looks for “-” or “_”) is

      Non-COE_X999999 (Which contains both of the conditions I’m looking for)

      Instead, it goes ahead and tries to query for it in the ElseIf part, and I don’t get why.

       

      If anyone can shed some light on this, that would be great. I don’t know if its the If statement that’s ignoring it, or the ElseIf which is somehow overwriting it, but either its not working right.

       

      Thanks,
      Mazarith

       

    • #167539
      Participant
      Topics: 0
      Replies: 45
      Points: 243
      Helping Hand
      Rank: Participant
      If I understand your problem correctly then you need to use “like “operator
      $PCName -Like “*-*_*
      • #167548
        Participant
        Topics: 1
        Replies: 2
        Points: 18
        Rank: Member

        Yeah I thought maybe the filter had to do with it. So I tried to do more combinations, all the combinations, and for some reason only some of them work. -contains only works half the time, and I ended up having to seperate them as individual variables like so:

        ($PCName -like “*-*” -or $PCName -like “*_*”)

        then I added an -not to make it a NOR statement.

        -not(($PCName -like “*-*”) -or ($PCName -like “*_*”))

        and that works…

        Thanks @Evila Osa for your help, because I honestly wasn’t sure if it was just my elseif that was overwriting it… somehow.

         

        Thanks!

      • #167551
        Participant
        Topics: 2
        Replies: 512
        Points: 1,309
        Helping Hand
        Rank: Community Hero

        You could opt for a few different things there.

        Your difficulty with -in or -contains is that they aren’t string operators, they’re array operators.

        "apple" -contains "a" # false
        "a", "p", "p", "l", "e" -contains "a" # true

        In contrast, strings have a .Contains() method that does work:

        "apple".Contains("a") # true

        For multiple things you want to check, you can also work in a bit of regex with a -match or -notmatch operator:

        "apple" -match "a" # true

        As for your problematic if statement, you could replace your -not, -like, and -or with a single -notmatch:

        $PCName -notmatch '-|_'

        Where regex is concerned, | is effectively a logical “or”. 🙂

      • #167563
        Participant
        Topics: 1
        Replies: 2
        Points: 18
        Rank: Member

        This is amazing! The last one did the trick perfectly. Appreciate the help!

Viewing 1 reply thread
  • The topic ‘First If statement not detecting, or ElseIf overwriting?’ is closed to new replies.