0

I have created a powershell script to connects to exchange online. The way it works is that, the script accepts a commandline argument as input(userprincipalname of a user), retrieves all mailboxes in exchange online then it checks if the user issued userprincipalname matches mailbox in exchange online. If the mailbox does not exist, i'm writing host, "mailbox does not exist", if the mailbox exists, i'm writing host "mailbox exists."

Problem The problem is the scripts returns both if and else statement bodies. I expect to see if statement body returned only if the mailbox exists and else statement body returned only if the mailbox does not exist.

What I'm i doing wrong.

Below is the script.


param($m)
# Add your Global admin plain password here
$password_ = "mysecurepassword"
$password = ConvertTo-SecureString $password_ -AsPlainText -Force

# Add your global administrator login email here.
$upn = "bernardmwanza@bernardcomms.onmicrosoft.com"

# Automated login to azure ad
$AppCredential = New-Object System.Management.Automation.PSCredential($upn, $password)
Connect-ExchangeOnline -Credential $AppCredential

# Retrieving all mailboxes in exchange online
$usermbxs = (Get-EXOMailbox).UserPrincipalName
foreach($usermbx in $usermbxs){

# Check if the user given mailbox exists in exchangeonline
if($m -match $usermbx){

write-host $m "Mailbox does exists"

}else{

write-host "The mailbox does not exist"

}

}

The output i get when i pass upn of a user who has mailbox in exchange online enter image description here

The output i get when i pass upn of a user who does not exist in exchange online enter image description here

Bernietechy
  • 186
  • 1
  • 16
  • Use Format-Table to help debug. Add to the code $usermbxs | Format-Table and inside the for loop $usermbx | Format-Table Format table will reveal all the properties and let you know if any are empty. – jdweng Dec 13 '22 at 10:32

1 Answers1

1

What am I doing wrong?

Ignoring the "put your global admin password in this script" requirement (you should be using modern authentication instead for better security - upgrade your PowerShell module for ExchangeOnline), your script is comparing each mailbox against the $m passed one at a time. Presuming your tenant has 6 accounts, it is checking each UserPrincipalName at a time, and advising if that UPN matches the $m param.

$usermbxs = (Get-EXOMailbox).UserPrincipalName
foreach($usermbx in $usermbxs){

# Check if the user given mailbox exists in exchangeonline
if($m -match $usermbx){

write-host $m "Mailbox does exists"

}else{

write-host "The mailbox does not exist"

}

}

You can simplify your code by removing the "foreach" check, and comparing using the -contains operator, which checks for the existence of a value in an collection, which would be the $usermbxs array. -contains is case insensitive - if (for whatever reason) you need case sensitive checking, use -ccontains.

$usermbxs = (Get-EXOMailbox).UserPrincipalName
if ($usermbxs -contains $m) {
Write-Host "$m mailbox exists"
} 
else {
Write-Host "$m mailbox does not exist"
}
Guy S
  • 457
  • 4
  • 9
  • 1
    Thank for this, it worked. Thanks also for recommendation on using modern authentication. I'm exploring the same using microsoft graph api instead of using exomodule – Bernietechy Dec 13 '22 at 11:16