This topic contains 4 replies, has 3 voices, and was last updated by
July 24, 2019 at 3:49 pm #167509
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#####)
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.
July 24, 2019 at 4:00 pm #167539ParticipantTopics: 0Replies: 44Points: 235Rank: ParticipantIf I understand your problem correctly then you need to use “like “operator$PCName -Like "*-*_*"
July 24, 2019 at 5:24 pm #167548
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.
July 24, 2019 at 5:43 pm #167551ParticipantTopics: 2Replies: 483Points: 1,151Rank: Community Hero
You could opt for a few different things there.
Your difficulty with
-containsis 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
"apple" -match "a" # true
As for your problematic if statement, you could replace your
-orwith a single
$PCName -notmatch '-|_'
Where regex is concerned,
|is effectively a logical "or". 🙂
July 24, 2019 at 6:36 pm #167563
This is amazing! The last one did the trick perfectly. Appreciate the help!
You must be logged in to reply to this topic.