I have a vbs script that runs at user logon however I'd like it only to run
if the user has an exchange mailbox, can you help?
--
Steve

RE: Email by Engtmk

Engtmk
Wed Mar 12 19:22:00 CDT 2008

hi Steven,

this can be done easily using the below script

if you wish more info you can review the complete article at :

http://www.codeproject.com/KB/vbscript/ExchangeMailBox.aspx

'***********************************************
'
' 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


I hope this will help you :-)



"Steven" wrote:

> I have a vbs script that runs at user logon however I'd like it only to run
> if the user has an exchange mailbox, can you help?
> --
> Steve
>

Re: Email by James

James
Wed Mar 12 20:30:40 CDT 2008

"Steven" <Steven@discussions.microsoft.com> wrote in message
news:5691B9D6-7858-4D42-8E0D-0DA30FB8559D@microsoft.com...
>I have a vbs script that runs at user logon however I'd like it only to run
> if the user has an exchange mailbox, can you help?

Put this at the top of the script and see if it does what you want:

With CreateObject("ADSystemInfo")
If IsEmpty(GetObject("LDAP://" & .UserName).mail) Then WScript.Quit
End With