Solved

How to check if user has an Exchange mailbox

Posted on 2011-03-16
7
4,764 Views
Last Modified: 2012-05-11
I am writing a script that will perform some functions if the user has an Exchange mailbox. I'm getting stuck on how to check if the user has a mailbox.

Right now I'm using powershell but I can use VBS or batch commands if that's easier.

Here's what I have with powershell:
$strName = "testuser"

$strFilter = "(&(objectCategory=User)(samAccountName=$strName))"
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.Filter = $strFilter
$objPath = $objSearcher.FindOne()
$objUser = $objPath.GetDirectoryEntry()

$a = $objUser.msExchMailboxGuid

Open in new window




That works fine to get the mailbox GUID. What I would like to do is compare that $a variable to see if it's empty. But I can't figure out how to run a compare. Here's the value when there is no GUID:

PS C:\> Get-Variable -Name a |fl *

Name        : a
Description :
Value       : {}
Visibility  : Public
Module      :
ModuleName  :
Options     : None
Attributes  : {}

Open in new window


Does anyone have suggestions on how to do this with PowerShell or any other method?

Thanks!
0
Comment
Question by:Jeremy Weisinger
7 Comments
 
LVL 16

Expert Comment

by:uescomp
ID: 35149405
You could try:

Get-User user1 | Select-Object Name,RecipientType,RecipientTypeDetails
0
 
LVL 7

Expert Comment

by:waleeda
ID: 35149537
you do through query from the active directory users and computer, to show all user with a value in the email address field
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 100 total points
ID: 35149557
$a.Value.Length will be 0 if there's no GUID.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 18

Author Comment

by:Jeremy Weisinger
ID: 35149878
@uescomp I should have given a little more detail. I'm running this command on an RDSH server. So no AD and no Exchange installed on the server so I can't run that command.

@waleeda Sorry but I need this automated in a script

@tgerbert Good suggestion but it's not working for the variable. I seemingly can't evaluate the value. When I run $a.value.length it just returns to the next prompt without giving output. Here's what I get:
PS C:\> $a.value.length
PS C:\>

Open in new window

It's almost like it has a null entry but when I try and compare it to null it just returns to the prompt. If I create the x variable with a null value I get the following:
PS C:\> $x = $null
PS C:\> $x.value.length
PS C:\>

Open in new window


But I can compare it... :
PS C:\> $x -eq $null
True
PS C:\>

Open in new window

but not the empty GUID:
PS C:\> $a -eq $null
PS C:\>

Open in new window

That above command should return either True or False but it is not returning anything. This is why I can't seem to compare it.
0
 
LVL 18

Accepted Solution

by:
Jeremy Weisinger earned 0 total points
ID: 35150277
OK, I found out what I was missing. The datatype is an Object. I can use one of the object properties to get what I'm looking for. Here's the code:

$strName = "testuser"

$strFilter = "(&(objectCategory=User)(samAccountName=$strName))"
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.Filter = $strFilter
$objPath = $objSearcher.FindOne()
$objUser = $objPath.GetDirectoryEntry()

$a = $objUser.msExchMailboxGuid
if($a.count -eq 1)
{write-host User has mailbox}

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35150961
$a.Value -eq $null
0
 
LVL 18

Author Closing Comment

by:Jeremy Weisinger
ID: 35178801
Thanks for posting. You help me head in the right direction.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
In this video we show how to create a User Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Mailb…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question