Jump to content

Running part of ADO script multipul times error


Recommended Posts

Hey guys

i wish that there is someone out there who can help me, i've gotten a hold of a script that actually does work, the problem is making it run 2 times with out shutting down the program. The script is designed to get the experation date of a user in an AD, that part works, the problem is, i don't want to shut it down to open it again to look up a new user.

When i do i get error about the Line 52

$objRecordSet.MoveFirst

$objRecordSet.MoveFirst^Error

Error: The Requested Action with this object has failed.

Anyone who can help with this?

#include <Date.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <String.au3>

;~ $DomainPreFix = ""
$DomainDC="DC5"
$DomainLDAP="//DC5/dc=ceuherning,dc=int"
$DomainMaxPwAge="90"



#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Password Checker", 315, 215, 192, 124)
$UserName = GUICtrlCreateInput("", 16, 32, 113, 21)
$Button1 = GUICtrlCreateButton("OK", 32, 166, 97, 25)
GUICtrlSetState(-1, $GUI_DEFBUTTON)
$Button2 = GUICtrlCreateButton("Close", 171, 166, 97, 25)
$Label1 = GUICtrlCreateLabel("Username", 16, 8, 107, 17)
$Label2 = GUICtrlCreateLabel("Status", 152, 8, 146, 17)
$Edit1 = GUICtrlCreateEdit("", 152, 32, 153, 81)
GUICtrlSetData(-1, "")
GUICtrlSetFont(-1, 8, 400, 0, "Verdana")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $UserName = GUICtrlRead($UserName)

            $objConnection = ObjCreate("ADODB.Connection")
            $objCommand = ObjCreate("ADODB.Command")
            $objConnection.Provider = "ADsDSOObject"
            ;~ $objConnection.Properties("User ID") = $DomainPreFix & "\" & $UserName
            ;~ $objConnection.Properties("Password") = $PassWord
            $objConnection.Properties("Encrypt Password") = "TRUE"
            $objConnection.Open("Active Directory Provider")
            $objCommand.ActiveConnection = $objConnection
            $objCommand.Properties("Page Size") = 1000
            $objCommand.Properties("Timeout") = 30
            $objCommand.Properties("Searchscope") = 2
            
            $objCommand.CommandText = "SELECT distinguishedName FROM 'LDAP:" & $DomainLDAP & "' WHERE objectCategory='user' AND sAMAccountName='" & $UserName & "'"
            $objRecordSet = $objCommand.Execute
            $objRecordSet.MoveFirst
;~          $objRecordSet.MoveNext      
            
            $strDN = $objRecordSet.Fields("distinguishedName").Value
            $sqlString = "SELECT sAMAccountName, pwdLastSet FROM 'LDAP://" & $DomainDC & "/" & $strDN & "' WHERE objectCategory='user'"
            $objCommand.CommandText = $sqlString

            $objRecordSet = $objCommand.Execute
            $objDate = $objRecordSet.Fields("pwdLastSet").Value

            ; Convert hex string to large number equivelent to 100ns count
            $PWDateHigh = $objDate.HighPart
            $PWDateLow = $objDate.LowPart

            ; Compensate for IADsLargeInteger interface error where low part is a negative Int32
            If $PWDateLow < 0 Then $PWDateHigh += 1

            ; Sum value to large number (AutoIt float type)
            $PWDate = $PWDateHigh * 2 ^ 32
            $PWDate += $PWDateLow

            ; Convert 100ns count to integer seconds
            $iSeconds = Floor($PWDate / 10000000)

            ; Convert seconds since 12:00AM January 01, 1601 to date string
            $PWDate = _DateAdd("S", $iSeconds, "1601/01/01 00:00:00")

            $PassWordExp = $DomainMaxPwAge - _DateDiff( 'd',$PWDate,_NowCalc())
            $objRecordSet.MoveNext

;~              $objRecordSet.Close
;~          close seems to do nothing?

                if $PassWordExp > 0 Then
                    GUICtrlSetData($Edit1, $PassWordExp & " days")
                    GUICtrlSetColor(-1, 0x008B00)
                Else
                    GUICtrlSetData($Edit1,"expired " & $PassWordExp & " days ago")
                    GUICtrlSetColor(-1, 0xFF0000)
                EndIf
                
        Case $Button2
            Exit
    EndSwitch
WEnd
Link to comment
Share on other sites

The problem with your script is that you open a connection to the AD when you press the button for the first time but you never close the connection.

So when you press the button a second time you run into the problem.

May I suggest to use my Active Directory UDF (for download please see my signature)? It has all the functions you need.

Just use _AD_Open at the beginning of your script and _AD_Close at the end. Check the password with _AD_GetPasswordInfo, _AD_IsPasswordExpired and _AD_GetPasswordExpired.

A stripped down example would look like:

#include <GUIConstantsEx.au3>
#include <AD.au3>

_AD_Open()

#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Password Checker", 316, 115, 192, 124)
$UserName = GUICtrlCreateInput("", 16, 32, 113, 21)
$Button1 = GUICtrlCreateButton("OK", 16, 70, 97, 25)
GUICtrlSetState(-1, $GUI_DEFBUTTON)
$Button2 = GUICtrlCreateButton("Close", 203, 70, 97, 25)
$Label1 = GUICtrlCreateLabel("Username", 16, 8, 107, 17)
$Label2 = GUICtrlCreateLabel("Status", 152, 8, 146, 17)
$Edit1 = GUICtrlCreateLabel("", 152, 35, 153, 17)
GUICtrlSetFont(-1, 8, 400, 0, "Verdana")
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $Button2
            _AD_Close()
            Exit
        Case $Button1
            $User = GUICtrlRead($UserName)
            $Status = _AD_IsPasswordExpired($User)
            If $Status = 0 Then
                If @error = 1 Then
                    GUICtrlSetData($Edit1, "User " & $User & " does not exist!")
                    GUICtrlSetColor(-1, 0xFF0000)
                Else
                    GUICtrlSetData($Edit1, "Password is not expired")
                    GUICtrlSetColor(-1, 0x008B00)
                EndIf
            Else
                GUICtrlSetData($Edit1, "Password is expired")
                GUICtrlSetColor(-1, 0xFF0000)
            EndIf
    EndSwitch
WEnd
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

That's easy :blink:

Something like this:

#include <GUIConstantsEx.au3>
#include <AD.au3>

_AD_Open()

#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Password Checker", 316, 115, 192, 124)
$UserName = GUICtrlCreateInput("", 16, 32, 113, 21)
$Button1 = GUICtrlCreateButton("OK", 16, 75, 97, 25)
GUICtrlSetState(-1, $GUI_DEFBUTTON)
$Button2 = GUICtrlCreateButton("Close", 203, 75, 97, 25)
$Label1 = GUICtrlCreateLabel("Username", 16, 8, 107, 17)
$Label2 = GUICtrlCreateLabel("Status", 152, 8, 146, 17)
$Edit1 = GUICtrlCreateLabel("", 152, 30, 153, 25)
GUICtrlSetFont(-1, 8, 400, 0, "Verdana")
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $Button2
            _AD_Close()
            Exit
        Case $Button1
            $User = GUICtrlRead($UserName)
            $Status = _AD_IsPasswordExpired($User)
            If $Status = 0 Then
                If @error = 1 Then
                    GUICtrlSetData($Edit1, "User " & $User & " does not exist!")
                    GUICtrlSetColor(-1, 0xFF0000)
                Else
                    $aPWInfo = _AD_GetPasswordInfo($User)
                    $iDays = _DateDiff("D",_NowCalc(),$aPWInfo[9])
                    GUICtrlSetData($Edit1, "Password is not expired. Will expire in " & $iDays & " days")
                    GUICtrlSetColor(-1, 0x008B00)
                EndIf
            Else
                $aPWInfo = _AD_GetPasswordInfo($User)
                $iDays = _DateDiff("D",_NowCalc(),$aPWInfo[9])
                GUICtrlSetData($Edit1, "Password is expired. Expired " & Abs($iDays) & " days before")
                GUICtrlSetColor(-1, 0xFF0000)
            EndIf
    EndSwitch
WEnd

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Glad to be of service :blink:

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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...