1

I have following variable:

[string]$fullAccess = 'add'

and IF instruction:

if(($fullAccess -cnotcontains '*add*') -or ($fullAccess -cnotcontains '*remove*')) {
    $fullAccess = Read-Host "Define if you would like to ADD or REMOVE fullAccess for $user user to $mailboxName mailbox"
}else{
    Write-Host 's'
}

Even tho my variable contains word add, it still prompts me to enter add or remove.

What's the issue here?

Pogorzela
  • 15
  • 3
  • 2
    please, _please_, read the instructions on how the `-contains` and `-in` operators work. **_you are doing it OH-SO-WRONG._** [*grin*] – Lee_Dailey Dec 07 '20 at 01:41
  • I get exactly same result with: `if(($fullAccess -notlike '*add*') -or ($fullAccess -notlike '*remove*')) { $fullAccess = Read-Host "Define if you would like to ADD or REMOVE fullAccess for $user user to $mailboxName mailbox" }` – Pogorzela Dec 07 '20 at 01:46
  • 1
    .contains may be more of what you’re after. -contains works against arrays. – Doug Maurer Dec 07 '20 at 02:19
  • @Pogorzela - please take a look at the Answer i posted. it shows one way to get a user choice and how to handle valid/invalid responses. i think that is what you really are aiming at. – Lee_Dailey Dec 07 '20 at 02:24

2 Answers2

3

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'
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
1

your code logic is strange. i gave up trying to sort out the glitch and rewrote the "choose one" code. [grin]

what it does ...

  • initializes the $Vars
  • uses a while $Choice is $Null block to get the user choice
  • shows the question
  • gets the user choice
  • checks it against the allowed values
  • if valid, sets $FullAccess to the desired value
  • if NOT valid, sets $Choice to $Null
  • displays the final valid value

the code ...

$UserName = $env:USERNAME
$MailBoxName = 'SharedMailBox@example.com'
$FullAccess = $Null

$Choice = $Null
while ([string]::IsNullOrWhiteSpace($Choice))
    {
    Write-Host ('Do you want to (A)dd or (R)emove full access for user [ {0} ] to mailbox [ {1} ] ?' -f $UserName, $MailBoxName)
    $Choice = Read-Host '    Your choice '

    if ($Choice -eq 'a')
        {
        $FullAccess = 'Add'
        }
        elseif ($Choice -eq 'r')
        {
        $FullAccess = 'Remove'
        }
        else
        {
        $Choice = $Null
        }
    }

'$FullAccess = {0}' -f $FullAccess

output ...

Do you want to (A)dd or (R)emove full access for user [ MyUserName ] to mailbox [ SharedMailBox@example.com ] ?
    Your choice : 3
Do you want to (A)dd or (R)emove full access for user [ MyUserName ] to mailbox [ SharedMailBox@example.com ] ?
    Your choice : t
Do you want to (A)dd or (R)emove full access for user [ MyUserName ] to mailbox [ SharedMailBox@example.com ] ?
    Your choice : wer
Do you want to (A)dd or (R)emove full access for user [ MyUserName ] to mailbox [ SharedMailBox@example.com ] ?
    Your choice : r
$FullAccess = Remove
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26