Hi @Eaven HUANG ,
I think you have answer in your previous question.
Try something like this:
elseif (($jobtitle.ToLower().Contains("teacher")) -OR ($jobtitle.ToLower().Contains("visiting")) -OR ($jobtitle.ToLower().Contains("lecturer")))
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Dear all,
I'm trying to use following if statement to verify when user's job title contains the keyword of teacher, or visiting, or lecturer, or teaching fellow, or editor, then we will add some attributes for this users. however, the syntax i used below didn't actually take effect, what am I missing?
Thanks.
elseif ($jobtitle.ToLower().Contains("teacher" -or "visiting" -or "lecturer" -or "teaching fellow" -or 'Editor'))
Hi @Eaven HUANG ,
I think you have answer in your previous question.
Try something like this:
elseif (($jobtitle.ToLower().Contains("teacher")) -OR ($jobtitle.ToLower().Contains("visiting")) -OR ($jobtitle.ToLower().Contains("lecturer")))
Here are two ways to approach the problem:
$jobtitle = 'teaching fellow'
# this might be easier to maintain:
$found = $false
Switch -regex ($jobtitle) {
'\bteacher\b' {$found = $true; break}
'\bvisiting\b' {$found = $true; break}
'\blecturer\b' {$found = $true; break}
'\bteaching fellow\b' {$found = $true; break}
'\beditor\b' {$found = $true; break}
default {$found = $false}
}
if ($found){
"found it"
}
else{
"not there"
}
#
# this is more compact, but harder to read and my become unwieldy if the number of choices increases
if ($jobtitle -match '\bteacher|visiting|lecturer|teaching\sfellow|editor\b'){
"found it"
}
else{
"not there"
}
In the string class, you can search for a string within a string, but you can't provide alternate choices: system.string.contains
Hello there,
You can nest blocks of if/else statements, depending on your particular use case.
If you write you if/else block in a single line, then PowerShell won't bark at you. To nest if/else statements you must use the keyword elseif, which must have a condition specified (unlike a flat else block which directly contains the code to execute).
You can have a quick look into this thread https://social.technet.microsoft.com/Forums/windowsserver/en-US/f34920d1-8d94-4d47-ba0f-791ed11a6d56/if-with-multiple-conditions?forum=winserverpowershell
---------------------------------------------------------------------------------------------------------------------------------------------
--If the reply is helpful, please Upvote and Accept it as an answer–