Solved

How to check if user has an Exchange mailbox

Posted on 2011-03-16
7
5,103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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 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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
In this video we show how to create an email address policy 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 Mail Flow…

710 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