Jump to content

This "Create a Exchnage 2003 Mail box" UDF not working


Recommended Posts

This Active directory UDF was created by another member of these forums who I have asked for help but has not replied.

It should create an exchange mailbox .

I have written and app that passes input to the Active directory APP called DSADD that creates a USer in Active directory fine but would like to use the UDF below to create a mailbox also.

I have not been able to get it to work and am unsure where I am going worng

could someone have a look and help me get this working correctly.

CODE
; UserCreateMailbox

; $user - User to add mailbox to

; $mdbstore - Mailbox storename

; $store - Information store

; $server - Email server

; $admingroup - Administrative group in Exchange

; $domain - Domain name

Func UserCreateMailbox($user, $mdbstore, $store, $server, $admingroup, $domain)

If ObjectExists($user) = 0 Then Return 0

Dim $objConnection, $objRootDSE

$objConnection = ObjCreate("ADODB.Connection") ; Create COM object to AD

$objConnection.Provider = "ADsDSOObject"

$objConnection.Open ("Active Directory Provider") ; Open connection to AD

$objRootDSE = ObjGet("LDAP://RootDSE")

Global $strDNSDomain = $objRootDSE.Get ("defaultNamingContext") ; Retrieve the current AD domain name

$strQuery = "<LDAP://" & $strDNSDomain & ">;(sAMAccountName=" & $user & ");ADsPath;subtree"

$objRecordSet = $ObjConnection.Execute ($strQuery) ; Retrieve the FQDN for the group, if it exists

$ldap_entry = $objRecordSet.fields (0).value

$oUser = ObjGet($ldap_entry) ; Retrieve the COM Object for the object

If $oUser.HomeMDB <> "" Then Return 0

$mailboxpath = "LDAP://CN="

$mailboxpath = $mailboxpath & $mdbstore

$mailboxpath = $mailboxpath & ",CN="

$mailboxpath = $mailboxpath & $store

$mailboxpath = $mailboxpath & ",CN=InformationStore"

$mailboxpath = $mailboxpath & ",CN="

$mailboxpath = $mailboxpath & $server

$mailboxpath = $mailboxpath & ",CN=Servers,CN="

$mailboxpath = $mailboxpath & $admingroup

$mailboxpath = $mailboxpath & ",CN=Administrative Groups,CN=State Street,CN=Microsoft Exchange,CN=Services,CN=Configuration,"

$mailboxpath = $mailboxpath & $domain

$oUser.CreateMailbox ($mailboxpath)

$oUser.SetInfo

Return 1

EndFunc ;==>UserCreateMailbox

Link to comment
Share on other sites

  • 4 months later...

@all

Maybe this can help you out.

; ------ SCRIPT CONFIGURATION ------
$strDCName = "<DC>" ;  e.g. "batman"
$strUserName = "CN=<userCN>"        ; e.g. "Random User"
; ------ END CONFIGURATION ------

; get the default and config NC names
 $oIADS = ObjGet("LDAP://RootDSE")
$strDefaultNC = $oIADS.Get("defaultnamingcontext")
$strConfigNC = $oIADS.Get("configurationNamingContext") 
$strContainer= "/CN=Users," & $strDefaultNC
 $objContainer = ObjGet("LDAP://" & $strDCName & $strContainer)

; find the target user
 $oIADSUser = ObjGet("LDAP://" & $strUserName & ",CN=Users," & $strDefaultNC)
 $oMailBox = $oIADSUser

; Open the Connection.
 $oConnection = ObjCreate("ADODB.Connection")
 $oCommand = ObjCreate("ADODB.Command")
 $oRecordSet = ObjCreate("ADODB.Recordset")
$oConnection.Provider = "ADsDSOObject"
$oConnection.Open ("ADs Provider")

; Build the query to find the private MDBs. Use the first one if any are found.
$strQuery = "<LDAP://" & $strConfigNC & ">;(objectCategory=msExchPrivateMDB);name,adspath;subtree"

$oCommand.ActiveConnection = $oConnection
$oCommand.CommandText = $strQuery
 $oRecordSet = $oCommand.Execute
 
If Not $oRecordSet.EOF Then
    $oRecordSet.MoveFirst()
    $firstMDB = String($oRecordSet.Fields("ADsPath").Value)
Else
    $firstMDB = ""
EndIf

; create the mailbox
$oMailBox.CreateMailbox ($firstMDB)
$oIADSUser.SetInfo()

ConsoleWrite ("Created mailbox for " & $strUserName)

Enjoy !!

Regards,

ptrex

Link to comment
Share on other sites

thx for the anser. But i dont understand the working. Can you give me an example?

example domain: put.local

username: test

i have made chages @

$strDCName = "dc=put,dc=local" ; e.g. "batman"

$strUserName = "CN=test" ; e.g. "Random User"

but i geht the error

C:\Dokumente und Einstellungen\Administrator\Desktop\excha.au3 (39) : ==> Variable must be of type "Object".:

$oMailBox.CreateMailbox ($firstMDB)

$oMailBox^ ERROR

thx for any help!

Link to comment
Share on other sites

well the errormessage explains where your problem lies..

you perform an action on a variable that is not possible:

$oMailBox = let's say at the moment an integer or a string text, not a connection to an object,

then you try to launch a method that should be exposed through your connection to the object.

you can see this yourself if you put a messagebox in front of the errorring rule.

$oIADSUser = ObjGet("LDAP://" & $strUserName & ",CN=Users," & $strDefaultNC)

$oMailBox = $oIADSUser

this code is not too good practise..

I would write :

$oMailBox = ObjGet("LDAP://" & $strUserName & ",CN=Users," & $strDefaultNC)

if NOT isobj($oMailBox) then exit

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...