There are two problems with your approach:
Your logic is flawed: because you're using negated operators you need -and
rather than -or
- otherwise, the condition is always $true
:
- To give a simplified example with the
-ne
(non-inequality) operator:
$val -ne 'a' -or $val -ne 'b'
is $true
irrespective of the value of $val
:
- if
$val
is 'a'
it is by definition not 'b'
, so the second condition is $true
, and therefore the conditional as a whole.
- if
$val
is 'b'
or any other value, the first condition is $true
, and therefore the conditional as a whole.
PowerShell's -contains
operator and its variants do not test a string for substrings - instead, they test the RHS string for being contained in full in the LHS collection, as the -in
operator does, only with the operands reversed - see this answer.
By contrast, it is the System.String.Contains()
.NET method that performs literal substring matching (case-sensitively by default), so one solution is:
$fullAccess = 'add'
if (-not $fullAccess.Contains('add') -and -not $fullAccess.Contains('delete')) {
$fullAccess = Read-Host "Define if you would like to ADD or REMOVE fullAccess for $user user to $mailboxName mailbox"
} else {
Write-Host 's'
}
You could also a variant of -like
, the wildcard matching operator:
$fullAccess -cnotlike '*add*' -and $fullAccess -cnotlike '*delete*'
Alternative formulation with positive operators and -or
, negated as a whole with -not
:
-not ($fullAccess -clike '*add*' -or $fullAccess -clike '*delete*')
Alternatively, and more succinctly, you could use a variant of -match
, the regular-expression matching operator:
$fullAccess = 'add'
if ($fullAccess -cnotmatch 'add|delete') {
$fullAccess = Read-Host "Define if you would like to ADD or REMOVE fullAccess for $user user to $mailboxName mailbox"
} else {
Write-Host 's'
}