Click here to Skip to main content
11,706,455 members (35,263 online)
Click here to Skip to main content

Check if a user has an Exchange mailbox

, 23 Sep 2003 122.7K 24
Rate this:
Please Sign up or sign in to vote.
Check in a command file if a user has an exchange mailbox
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS--><!-- Add the rest of your HTML here -->

Introduction

This VBScript is used to check if a user has an exchange mailbox. The result is the Errorlevel variable which will be set. This script can be called in a command file. For example, you could run outlook only if the user has a mailbox.

Solution

The solution is to check if there is Exchange information in active directory for this user.
As you can see, the username (logonname) is optional. In case you do not give the username, then the name will be taken from the environment variable USERNAME.

In a command file (e.g. Logon script) it is sufficient to call this script and to check the Errorlevel.
REM *** Run Outlook when user has an exchange account ***
HasMailBox.vbs
If not errorlevel==1 outlook.exe

Change the Domain Controller information, replace MyMainDC with the domain controller you want to query. (LDAP)

DCServer = "MyMainDC"

'***********************************************
'
' HasMailbox.vbs
' (c) 2003 Computech 
' Written by Peter Verijke
' Checks is user has a mailbox in Exchange
'
' Usage : HasMailbox [LogonName]
' Returns : Errorlevel==1 : not found
'
'***********************************************
 

Dim ArgObj
Dim WshShell ' as object
Dim objEnv ' as collection
Dim objUser 'As IADsUser
Dim objMailbox 'As CDOEXM.IMailboxStore
Dim sUserLDAPName 'As String
Dim DCServer 'As String

 

' Get the Arguments object
Set ArgObj = WScript.Arguments

 

If ArgObj.Count < 1 Then
   Set WshShell = WScript.CreateObject("WScript.Shell")
   Set objEnv = WshShell.Environment("PROCESS")

 

   sUserName = objEnv("USERNAME")
else
   sUserName = UCase(ArgObj(0))
End If

 

DCServer = "MyMainDC"
sUserLDAPName = QueryActiveDirectory(sUserName)

 

Set objUser = GetObject("LDAP://" & DCServer + "/" & sUserLDAPName)

Set objMailbox = objUser
'check if user is mailbox enabled
If objMailbox.HomeMDB = "" Then
   WScript.Quit 1
Else
   WScript.Quit 0
End If

 


Public Function QueryActiveDirectory(sUserName)
'Function:      QueryActiveDirectory
'Purpose:       Search the Active Directory's Global Catalog for users
'Parameters:    UserName - user to search for
'Return:        The user's distinguished name
 
    Dim oAD 'As IADs
    Dim oGlobalCatalog 'As IADs
    Dim oRecordSet 'As Recordset
    Dim oConnection 'As New Connection
    Dim strADsPath 'As String
    Dim strQuery 'As String
    Dim strUPN 'As String

 

    set oRecordSet = CreateObject("ADODB.Recordset")
    set oConnection = CreateObject("ADODB.Connection")

 

    'Determine the global catalog path
    Set oAD = GetObject("GC:")
    For Each oGlobalCatalog In oAD
        strADsPath = oGlobalCatalog.AdsPath
    Next
    'Initialize the ADO object
    oConnection.Provider = "ADsDSOObject"
    'The ADSI OLE-DB provider
    oConnection.Open "ADs Provider"
    'Create the search string
    strQuery = "<" & strADsPath & _
      ">;(&(objectClass=user)(objectCategory=person)(samaccountName=" & _
      sUserName & "));userPrincipalName,cn,distinguishedName;subtree"  
        'Execute the query

    Set oRecordSet = oConnection.Execute(strQuery)
    If oRecordSet.EOF And oRecordSet.BOF Then    
       'An empty recordset was returned
        QueryActiveDirectory = "Not Found"
    Else    'Records were found; loop through them
        While Not oRecordSet.EOF
            QueryActiveDirectory = oRecordSet.Fields("distinguishedName")
            oRecordSet.MoveNext
        Wend
    End If
End Function

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Peter Verijke
Founder Computech bvba
Belgium Belgium
I´m a freelance ICT consultant and Technical Project Manager for the last 15 years already.
Before that, I was a Software Developer for the Pharmaceutical and Petrochemical industry.
I developed a very wide knowledge in the ICT world due to my intrest.
This includes Networking (Lan, Wan), Routing Switching Bridging, etc...
Windows environment, which has no secrets for me.
Developing: To many languages to mension here.
Of course, the latest one on the list is dot net.

You may also be interested in...

Comments and Discussions

 
Questionhow to apply this code in Microsoft Script Editor Pin
tatminglee20-Jul-06 18:27
membertatminglee20-Jul-06 18:27 
GeneralMonitor an Exchange Mailbox for replies Pin
gyzmo7120-Jul-06 8:25
membergyzmo7120-Jul-06 8:25 
GeneralNo output or HasMailbox.vbs(47, 1) (null): An invalid dn syntax has been specified. Pin
TopCatt16-May-06 1:21
memberTopCatt16-May-06 1:21 
GeneralRe: No output or HasMailbox.vbs(47, 1) (null): An invalid dn syntax has been specified. Pin
Peter Verijke16-May-06 1:51
memberPeter Verijke16-May-06 1:51 
GeneralRe: No output or HasMailbox.vbs(47, 1) (null): An invalid dn syntax has been specified. Pin
TopCatt16-May-06 2:36
memberTopCatt16-May-06 2:36 
GeneralRe: No output or HasMailbox.vbs(47, 1) (null): An invalid dn syntax has been specified. Pin
Peter Verijke16-May-06 5:45
memberPeter Verijke16-May-06 5:45 
GeneralDeleting redundant SMTP addresses Pin
GeoffPeel11-Oct-04 18:58
memberGeoffPeel11-Oct-04 18:58 
GeneralRe: Deleting redundant SMTP addresses Pin
Peter Verijke13-Oct-04 5:58
memberPeter Verijke13-Oct-04 5:58 
GeneralRe: Deleting redundant SMTP addresses Pin
Peter Verijke13-Oct-04 14:03
memberPeter Verijke13-Oct-04 14:03 
GeneralModifying attribute value Pin
Michel Paquin10-May-04 7:55
sussMichel Paquin10-May-04 7:55 
GeneralRe: Modifying attribute value Pin
Anonymous13-May-04 3:45
sussAnonymous13-May-04 3:45 
GeneralExchange Server Settings Pin
RPA26-Sep-03 15:10
memberRPA26-Sep-03 15:10 
QuestionMail retrieval? Pin
toticow24-Sep-03 22:59
membertoticow24-Sep-03 22:59 
AnswerRe: Mail retrieval? Pin
Peter Verijke24-Sep-03 23:36
memberPeter Verijke24-Sep-03 23:36 
GeneralRe: Mail retrieval? Pin
toticow25-Sep-03 1:47
membertoticow25-Sep-03 1:47 
GeneralRe: Mail retrieval? Pin
Anonymous26-Sep-03 5:51
sussAnonymous26-Sep-03 5:51 
QuestionRe: Mail retrieval without pop3? Pin
MFMan75826-Sep-07 4:25
memberMFMan75826-Sep-07 4:25 
GeneralRe: Mail retrieval? Pin
#teve26-Sep-03 5:24
member#teve26-Sep-03 5:24 
GeneralRe: Mail retrieval? Pin
Peter Verijke26-Sep-03 5:57
memberPeter Verijke26-Sep-03 5:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 24 Sep 2003
Article Copyright 2003 by Peter Verijke
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid