0

So I need to check if the field of a variable equals to a certain string.

It pretty much goes like this:

There are multiple teams that can be chosen as a field:

  • Team one
  • Team two

Depending on what team it is, the code does different stuff.

The catch is however, that the insert of the team is not standardized. Each member of "Team One" can have team one written in various ways: teamone / team 1 / team one/ team1 (lets say these are the possibilities).

In my code I tried to do the check by using:

if($user.active -ne 0 -and $user.Team.ToLower() -like "team one" -or $user.Team.ToLower() -like "teamone" -or $user.Team.ToLower() -like "team1" )
    {

    Write-Host "Member is in team 1"
    }
#Else check if its in team 2,....

My code works perfectly if I use -and $user.Team.ToLower() -like "team one") But now I get the error You cannot call a method on a null-valued expression

Is this a good approach to what I'm doing here? Or is there a better alternative?

4
  • That error tells me $user.Team is null or that property does not exist. the line matching that error comes from the if statement you posted correct? Commented Apr 7, 2017 at 11:52
  • But my code works when I use ($user.active -ne 0 -and $user.Team.ToLower() -like "team one") It fails when I add all of the -or's Commented Apr 7, 2017 at 11:54
  • I don't think that is the right conclusion. Your if looks perfectly valid (although there is a better solution.) Honestly I think you might have a spelling mistake or something in your real code like $uesr.team.toLower(). If that is the error you are getting ... you have a null variable. Is the error coming from the same line that you posted? Commented Apr 7, 2017 at 11:57
  • I just rechecked and I found it because one of the users doesn't have a team indeed. Can you mention your better solution please? Commented Apr 7, 2017 at 11:58

1 Answer 1

2

You cannot call a method on a null-valued expression

That error is very telling. You have a variable or property that is null and you are trying to call a method on it. If you insist on using your logic you would need to test for nulls first.

This specific case can be avoided when you understand that most PowerShell operators are case-insensitive by default. So .ToLower() does not appear to even be needed

That being said your if statement looks perfectly valid, outside of that first caveat, but there are better approaches for this "multiple potential match scenario" you have here.

-contains

Since you are trying to make a full match against one of many possibilies you could just wrap those up in an array to use the -contains operator on it for comparison

if($user.active -ne 0 -and $user.Team -and "team one","teamone", "team1", "team 1" -contains $user.Team){}

$user.Team is evaluated as a truthy/falsy statement so the last condition wont even be checked if $user.Team is not populated or is an empty string

Some regex

if($user.active -ne 0 -and $user.Team -and $user.Team -match "team\s?(one|1)"){}

Use a little regex to see if the team is one of the following

teamone
team one
team1
team 1

In either case above the -and $user.Team check is not required and will function just as well without it. Just thought I should mention this if you needed somewhere else to verify if that is populated easily.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Matt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.