kuat
kuat

Reputation: 35

PowerShell if elseif else

Wondering if anyone could please assist with below.

I've tried a few different ways to get the below code working. Have three possible outcomes.

  1. UM enabled with 5 digit extension.
  2. UM enabled with no 5 digit extension.
  3. UM not enabled.
          #test for UM extension number#
          $um1 = Get-UmMailbox $user -ErrorAction SilentlyContinue | Select @{name=”Extensions”;expression={$_.Extensions -join “;”}} | ?{ $_.Extensions -match '^\d\d\d\d\d$' }
          $umextensionnumber = if ($um1) { $um1.Extensions } elseif (!$um1) { 'UM not enabled' } else { 'No UM Extension Number' }

Any help would be greatly appreciated.

Upvotes: 0

Views: 40

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

Way easier to read (and reason about) with nested if's instead:

$umextensionnumber = if($um1){
  # UM is enabled, let's check the extension:
  if($um1.Extensions -match '\d\d\d\d\d'){
    $um1.Extensions
  }
  else {
    "No extension found"
  }
}
else {
  "UM not enabled"
}

Upvotes: 2

Related Questions