Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to check if user has an Exchange mailbox

Posted on 2011-03-16
7
Medium Priority
?
5,726 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 400 total points
ID: 35149557
$a.Value.Length will be 0 if there's no GUID.
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 22

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 22

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 22

Author Closing Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Here in this article, you will get a step by step guidance on how to restore an Exchange database to a recovery database. Get a brief on Recovery Database and how it can be used to restore Exchange database in this section!
As much as Microsoft wants to kill off PST file support, just as they tried to do with public folders, there are still times when it is useful or downright necessary to export Exchange mailboxes to PST files. Thankfully, it is still possible to e…
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

886 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