Jump to content

Problems with ObjCreate()


Recommended Posts

Hi everyone!

I'm trying to convert a VBS script into AU3.

The VBS script returns the "OutOfOffice" state from each user of a MS-Exchange-Server.

It works quite well. But I want more! :)

The original VBS script:

servername = wscript.arguments(0)
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile = fso.opentextfile("c:\temp\offexport-" & servername & ".xml",2,true) 
wfile.writeline("<?xml version=""1.0""?>")
wfile.writeline("<ExportedOffs ExportDate=""" & WeekdayName(weekday(now),3) & ", " & day(now()) & " " & Monthname(month(now()),3) & " " & year(now()) & " " & formatdatetime(now(),4) & ":00" & """>")
set conn = createobject("ADODB.Connection")
set com = createobject("ADODB.Command")
Set iAdRootDSE = GetObject("LDAP://RootDSE")
strNameingContext = iAdRootDSE.Get("configurationNamingContext")
strDefaultNamingContext = iAdRootDSE.Get("defaultNamingContext")
Conn.Provider = "ADsDSOObject"
Conn.Open "ADs Provider"
svcQuery = "<LDAP://" & strNameingContext & ">;(&(objectCategory=msExchExchangeServer)(cn=" & Servername & "));cn,name,legacyExchangeDN;subtree"
Com.ActiveConnection = Conn
Com.CommandText = svcQuery
Set Rs = Com.Execute
while not rs.eof    
    GALQueryFilter =  "(&(&(&(& (mailnickname=*)(!msExchHideFromAddressLists=TRUE)(| (&(objectCategory=person)(objectClass=user)(msExchHomeServerName=" & rs.fields("legacyExchangeDN") & ")) )))))"
    strQuery = "<LDAP://"  & strDefaultNamingContext & ">;" & GALQueryFilter & ";distinguishedName,mailnickname,mail;subtree"
    com.Properties("Page Size") = 100
    Com.CommandText = strQuery
    Set Rs1 = Com.Execute
    while not Rs1.eof
        call procmailboxes(servername,rs1.fields("mail"))
        wscript.echo rs1.fields("mail")
        rs1.movenext
    wend
    rs.movenext
wend
rs.close
wfile.writeline("</ExportedOffs>")
wfile.close
set fso = nothing
set conn = nothing
set com = Nothing

wscript.echo "Done"




sub procmailboxes(servername,MailboxAlias)

Set msMapiSession = CreateObject("MAPI.Session")
on error Resume next
msMapiSession.Logon "","",False,True,True,True,Servername & vbLF & MailboxAlias
if err.number = 0 then
    on error goto 0
    if msMapiSession.outofoffice = false and msMapiSession.outofofficetext = "" then
        wscript.echo "No OOF Data for user"
    else 
        wfile.writeline("<OOFSetting DisplayName=""" & msMapiSession.CurrentUser & """ EmailAddress=""" & MailboxAlias & """ Offset=""" _
        & msMapiSession.outofoffice & """><![CDATA[ " & msMapiSession.outofofficetext & "]]></OOFSetting>")
    End if
else
    Wscript.echo "Error Opening Mailbox"
end if
Set msMapiSession = Nothing
Set mrMailboxRules = Nothing

End Sub

Here's what I have so far:

#include <Array.au3>


Local $sMailServer = "YourMailServer_NetBIOSName"


Local $oConnection  = ObjCreate("ADODB.Connection")
Local $oCommand     = ObjCreate("ADODB.Command")
Local $oRootDSE     = ObjGet("LDAP://RootDSE")


Local $sCNC = $oRootDSE.Get("configurationNamingContext")
Local $sDNC = $oRootDSE.Get("defaultNamingContext")


$oConnection.Provider = "ADsDSOObject"
$oConnection.Open("ADs Provider")


$oCommand.ActiveConnection = $oConnection
$oCommand.CommandText = "<LDAP://" & $sCNC & ">;(&(objectCategory=msExchExchangeServer)(cn=" & $sMailServer & "));cn,name,legacyExchangeDN;subtree"


Local $oExecute = $oCommand.Execute
While Not $oExecute.EOF
    Local $GALQueryFilter = "(&(&(&(&(mailnickname=*)(!msExchHideFromAddressLists=TRUE)(|(&(objectCategory=person)(objectClass=user)(msExchHomeServerName=" & $oExecute.Fields("legacyExchangeDN").Value & ")))))))"
    $oCommand.Properties("Page Size") = 100
    $oCommand.CommandText = "<LDAP://"  & $sDNC & ">;" & $GALQueryFilter & ";distinguishedName,mailnickname,mail;subtree"
    Local $oExecute2 = $oCommand.Execute
    While Not $oExecute2.EOF
        Local $aTest = _MapiSession($sMailServer, $oExecute2.Fields("mail").Value)
        If Not @error Then
            _ArrayDisplay($aTest)
        EndIf
        $oExecute2.MoveNext
    WEnd
    $oExecute.MoveNext
WEnd


Func _MapiSession($sMS_Server, $sMS_User)
    Local $oMapiSession = ObjCreate("MAPI.Session")
    If Not @error Then
        $oMapiSession.Logon("", "", False, True, True, True, $sMS_Server & @LF & $sMS_User)
        Local $aMS_Return = _ArrayCreate($sMS_User, $oMapiSession.CurrentUser, $oMapiSession.OutOfOffice, $oMapiSession.OutOfOfficeText)
        $oMapiSession = 0
        Return SetError(0, 0, $aMS_Return)
    EndIf
    Return SetError(1, 0, 0)
EndFunc

The script COULD work properly, but the '$oMapiSession.Logon()' command does not work. ;)

There must be something "special" about it, I suppose.

Could someone give me a clue? What's wrong with my code?

Greets,

-supersonic.

Edited by supersonic
Link to comment
Share on other sites

Try this:

I put an error handler in there to help you to debug...

#AutoIt3Wrapper_AU3Check_Parameters=-w 1 -w 2 -w 3 -w 4 -w 6 -d

#include <Array.au3>

Global $sMailServer = "YourMailServer_NetBIOSName"

#region Error Handler
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
Global $EventError

Func MyErrFunc()
    ConsoleWrite(@LF & "===========================================================" & @LF & _
            "! AutoItCOM Test -- We intercepted a COM Error !" & @LF & @TAB & _
            "err.description    : " & $oMyError.description & @LF & @TAB & _
            "err.windescription : " & $oMyError.windescription & @TAB & _
            "err.number     : " & Hex($oMyError.number, 8) & @LF & @TAB & _
            "err.lastdllerror   : " & $oMyError.lastdllerror & @LF & @TAB & _
            "err.scriptline     : " & $oMyError.scriptline & @LF & @TAB & _
            "err.source     : " & $oMyError.source & @LF & @TAB & _
            "err.helpfile   : " & $oMyError.helpfile & @LF & @TAB & _
            "err.helpcontext    : " & $oMyError.helpcontext & @LF & _
            "===========================================================" & @LF)

    Local $Err = $oMyError.number
    If $Err = 0 Then $Err = -1

    $EventError = $Err ; to check for after this function returns
EndFunc ;==>MyErrFunc
#endregion Error Handler

Global $oConnection = ObjCreate("ADODB.Connection")
Global $oCommand = ObjCreate("ADODB.Command")
Global $oRootDSE = ObjGet("LDAP://RootDSE")

Global $sCNC = $oRootDSE.Get("configurationNamingContext")
Global $sDNC = $oRootDSE.Get("defaultNamingContext")

$oConnection.Provider = "ADsDSOObject"
$oConnection.Open("ADs Provider")

$oCommand.ActiveConnection = $oConnection
$oCommand.CommandText = "<LDAP://" & $sCNC & ">;(&(objectCategory=msExchExchangeServer)(cn=" & $sMailServer & "));cn,name,legacyExchangeDN;subtree"

Global $oExecute = $oCommand.Execute

Global $GALQueryFilter
Global $oExecute2
Global $aTest

While Not $oExecute.EOF
    $GALQueryFilter = "(&(&(&(&(mailnickname=*)(!msExchHideFromAddressLists=TRUE)(|(&(objectCategory=person)(objectClass=user)(msExchHomeServerName=" & $oExecute.Fields("legacyExchangeDN" ).Value & ")))))))"

    $oCommand.Properties("Page Size") = 100
    $oCommand.CommandText = "<LDAP://" & $sDNC & ">;" & $GALQueryFilter & ";distinguishedName,mailnickname,mail;subtree"

    $oExecute2 = $oCommand.Execute

    While Not $oExecute2.EOF
        $aTest = _MapiSession($sMailServer, $oExecute2.Fields("mail" ).Value)

        If Not @error Then
            _ArrayDisplay($aTest)
        EndIf

        $oExecute2.MoveNext
    WEnd

    $oExecute.MoveNext
WEnd

Func _MapiSession($sMS_Server, $sMS_User)
    Local $oMapiSession = ObjCreate("MAPI.Session")

    If Not @error Then
        $oMapiSession.Logon("", "", False, True, True, True, $sMS_Server & @LF & $sMS_User)

        Local $aMS_Return = _ArrayCreate($sMS_User, $oMapiSession.CurrentUser, $oMapiSession.OutOfOffice, $oMapiSession.OutOfOfficeText)

        $oMapiSession = 0

        Return SetError(0, 0, $aMS_Return)
    EndIf

    Return SetError(1, 0, 0)
EndFunc ;==>_MapiSession
Link to comment
Share on other sites

jaberwocky,

the internal error AutoIt handler - and yours too - returns this:

===========================================================

! AutoItCOM Test -- We intercepted a COM Error !

err.description : [Collaboration Data Objects - [MAPI_E_NOT_INITIALIZED(80040605)]]

err.windescription : err.number : 80020009

err.lastdllerror : 0

err.scriptline : 77

err.source : Collaboration Data Objects

err.helpfile :

err.helpcontext : 0

===========================================================

I've already installed Collaboration Data Objects V1.2.

Why does the VB script works and the AU3 script not?

Is there something special when calling ObjCreate()?

Have I missed something to convert?

Edited by supersonic
Link to comment
Share on other sites

servername = wscript.arguments(0)
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile = fso.opentextfile("c:\temp\offexport-" & servername & ".xml",2,true) 
wfile.writeline("<?xml version=""1.0""?>")
wfile.writeline("<ExportedOffs ExportDate=""" & WeekdayName(weekday(now),3) & ", " & day(now()) & " " & Monthname(month(now()),3) & " " & year(now()) & " " & formatdatetime(now(),4) & ":00" & """>")

I noticed that you left this out. How important is this? 'servername' is what is passed to the _MapiSession function. I think you have "Global $sMailServer = "YourMailServer_NetBIOSName"" in place of the code above. Is that sufficient? Not sure if it makes a difference. It's worth looking into probably.

Edited by jaberwocky6669
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...