0

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.

1 Answer 1

2

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"
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Mathias. I see the logic in the nested if's that is something I will remember. What are your thoughts on extracting only the 5 digit number to output? Extensions property looks like this {[email protected], 12345} or {12345} I can only get output for latter, former ends up outputting No Extension Found.
-match doubles as a filter operator, so simply output @($um1.Extensions) -match '\d\d\d\d\d' and it'll result in only "12345"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.