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?
$user.Teamis null or that property does not exist. the line matching that error comes from the if statement you posted correct?($user.active -ne 0 -and $user.Team.ToLower() -like "team one")It fails when I add all of the-or's$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?