Solved

How to check if user has an Exchange mailbox

Posted on 2011-03-16
7
4,498 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
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…
In this video we show how to create a mailbox database 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 Servers >> Data…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now