Jump to content

Active Directory and Database PROBLEM


Recommended Posts

Hi, i want to make an script

:) 1- Get the user logued name, and then go to a database and find the name of the net-units of this username, and connect them automatically.

:) 2- (This part doesn´t work)

i want to make the same as in the first part, but getting the user groups automatically from the Active directory(could be more than 1 group) and then when we get the groups, go to an acces database and get and mount the net-units for this group.

On this step i get the message error :

>"C:\Archivos de programa\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "E:\bueno 3.0.au3"

E:\bueno 3.0.au3 (124) : ==> Object referenced outside a "With" statement.:

$salida2 = $adoRs.Fields("id").value

$salida2 = $adoRs.Fields("id")^ ERROR

>Exit code: 1 Time: 8.857

Thanks

#include <WinNet.au3>

#include <AD.au3>

#include <Array.au3>

#Include <WinAPI.au3>

;-------------------------------------------------------------

Dim $usuario = @UserName ; Guarda el Nombre de Usuario

Dim $nombrecomputadora = @ComputerName ; Guarda el Nombre de la Computadora

; Lista las unidades

$var = DriveGetDrive( "all" )

If NOT @error Then

MsgBox(4096,"", "Encontrada " & $var[0] & " Unidades")

For $i = 1 to $var[0]

MsgBox(4096,"Unidad " & $i, $var[$i])

Next

EndIf

;--------------------------------------------------------------

MsgBox(0, "Usuario actual conectado:", $usuario) ; Muestra el usuario logueado

MsgBox(0, "Nombre computadora desde la que se conecta:", $nombrecomputadora) ; Muestra el terminal

; Desconectamos todas las unidades virtuales

RunWait(@COMSPEC & " /c echo Desconectando unidades")

RunWait(@COMSPEC & " /c net use * /d /y") ; net use [/DELETE:{yes | no}]]

;(Variables para la BBDD)

$prueba="Informatica-Sistemas"

Dim $salida2

Dim $nombredbuser = "E:\mapeando.mdb" ; Ruta de la BBDD de los usuarios

Dim $nombredbdepart = "E:\mapeando2.mdb" ; Ruta de la BBDD de los departamentos

Dim $nombretabla = "MAPEO" ; Nombre de la tabla

Dim $query = "SELECT UNIDAD FROM "& $nombretabla &" WHERE USUARIO = "& $usuario ; Consulta Access

Dim $query2 = "SELECT UNIDAD FROM "& $nombredbdepart &" WHERE DEPARTAMENTO = "& $prueba

Dim $title

MsgBox(0,"Consulta en la Base de Datos...",$query) ; Muestra la consulta

;Obtiene Unidades asignadas al USUARIO por su *** USERNAME ***

Dim $salida

$adoCon = ObjCreate("ADODB.Connection")

$adoCon.Open("Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & $nombredbuser)

$adoRs = $adoCon.Execute($query)

MsgBox(0, "Dentro de funcion",$query)

While Not $adoRs.EOF

$salida = $adoRs.Fields("UNIDAD").value

DriveMapAdd("*",$salida)

$len = StringLen($salida)

MsgBox(0, "Longitud de la cadena:", $len)

$result = StringTrimRight($salida, $len)

MsgBox(0, "Cadena sin los caracteres:", $result)

$adoRs.MoveNext

WEnd

$var = DriveGetDrive( "all" )

If NOT @error Then

MsgBox(4096,"", "Encontradas " & $var[0] & " Unidades")

For $i = 1 to $var[0]

MsgBox(4096,"Drive " & $i, $var[$i])

Next

EndIf

MsgBox(0, "DOMINIO", $strDNSDomain)

MsgBox(0, "SERVIDOR", $strHostServer)

$adoCon.Close

;Obtiene Unidades asignadas al USUARIO por su *** DEPARTAMENTO ***

Local $oUsr

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

$objRecordSet = $objConnection.Execute ($strQuery) ; Retrieve the FQDN for the logged on user

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

$oUsr = ObjGet($ldap_entry) ; Retrieve the COM Object for the logged on user

$usergroups = $oUsr.GetEx ("memberof")

$count = UBound($usergroups)

MsgBox(0,"strQuery",$strQuery)

MsgBox(0,"strHostServer",$strHostServer)

MsgBox(0,"String DNS Domain",$strDNSDomain)

MsgBox(0, "Perteneces a estos grupos", $count)

MsgBox(0, "Nombre de los grupos???", $ldap_entry)

MsgBox(0, "?", $oUsr.CN)

$stringa=StringSplit($oUsr.CN,".")

$result = StringCompare($stringa[2], "Informatica-Sistemas", 0) ; "0" Not case sensitive

MsgBox(0, "Resultante", $result)

MsgBox(0, "String separado [1]?", $stringa[1])

MsgBox(0, "String separado [2]?", $stringa[2])

MsgBox(0, "String separado [3]?", $stringa[3])

MsgBox(0, "Cadena de prueba para comparar", $prueba)

$adoCon = ObjCreate("ADODB.Connection")

$adoCon.Open("Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & $nombredbdepart)

$adoRs = $adoCon.Execute($query2)

MsgBox(0, "Dentro de funcion",$query2)

While Not $adoRs.EOF

$salida2 = $adoRs.Fields("id").value

DriveMapAdd("*",$salida2)

$len2 = StringLen($salida2)

MsgBox(0, "Longitud de la cadena:", $len2)

$result = StringTrimRight($salida2, $len2)

MsgBox(0, "Cadena sin los caracteres:", $result)

$adoRs.MoveNext

WEnd

$adoCon.Close

Link to comment
Share on other sites

To get the Groups a user is member of you could use adfunctions.au3

Function _ADGetUserGroups should do what you want - I use it all the time.

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

To get the Groups a user is member of you could use adfunctions.au3

Function _ADGetUserGroups should do what you want - I use it all the time.

Yes, i have adfunctions.au3 included into a library i dont know why de debugger is telling me this:

>"C:\Archivos de programa\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "E:\bueno 3.0.au3"

E:\bueno 3.0.au3 (124) : ==> Object referenced outside a "With" statement.:

$salida2 = $adoRs.Fields("id").value

$salida2 = $adoRs.Fields("id")^ ERROR

>Exit code: 1 Time: 8.857

Link to comment
Share on other sites

To get all groups the currently logged on user is a member of you only need this:

#include <adfunctions.au3>
Global $asGroups[1]
_ADGetUserGroups($asGroups, @UserName)
_ArrayDisplay($asGroups)

So you can significantly strip down your code.

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

I introduce into my code the reference to this function but the error keeping telling the same...

#include <adfunctions.au3>  
Global $asGroups[1]  
_ADGetUserGroups($asGroups, @UserName)  
_ArrayDisplay($asGroups)

[autoit]>"C:\Archivos de programa\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "E:\bueno 3.0.au3"

E:\bueno 3.0.au3 (129) : ==> Object referenced outside a "With" statement.:

$salida2 = $adoRs.Fields("UNIDAD").value

$salida2 = $adoRs.Fields("UNIDAD")^ ERROR

>Exit code: 1 Time: 74.818[autoit]

THIS IS THE MESSAGE......

Posted Image

Link to comment
Share on other sites

Could you please just run my code? If it runs fine then the next step would be to build it into your code.

If you have still problems could you please post the version of adfunctions.au3 (the commented lines in the header)?

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

Sorry , im introducing to autoit.. this is the library adfunctions.au3

:)

[auto it]; Active Directory Function Library. Include this to allow access to Active Directory Functions

; Author : Jonthan Clelland

; Email : jclelland@statestreet.com

; Version : 3.0

; _ADDoError : Error event handler for COM errors. This is global so will pick up errors from your program if you include this library

; _ADCreateUser : Creates a user in a particular OU

; _ADCreateGroup : Creates a group in a particular OU

; _ADAddUserToGroup : Adds a user to a group (if the user is not already a member of the group)

; _ADRemoveUserFromGroup : Removes a user from a group

; _ADObjectExists : Returns 1 if the given object (SamAccountName) exists in the local AD Tree

; _ADModifyAttribute : Sets the attribute of the given object to the value specified

; _ADIsMemberOf : Returns 1 if the user is a member of the group

; _ADGetUserGroups : Returns an array (byreference) containing the groups that the user is a member of

; _ADRecursiveGetMemberOf : Returns a recursed list of group membership for a group or user [/auto it]

................

Func _ADGetUserGroups(ByRef $usergroups, $user = @UserName)

Local $oUsr

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

$objRecordSet = $objConnection.Execute ($strQuery) ; Retrieve the FQDN for the logged on user

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

$oUsr = ObjGet($ldap_entry) ; Retrieve the COM Object for the logged on user

$usergroups = $oUsr.GetEx ("memberof")

$count = UBound($usergroups)

_ArrayInsert($usergroups, 0, $count)

EndFunc ;==>_ADGetUserGroups

In the attachments there´s the main program : bueno 3.0.au3

Thank u for your help!!! :)

Link to comment
Share on other sites

The script should do this:

1.- Script should run automatically when the user login into the machine.

2.- Script should disconnect all the default netdrives assigned by windows default

3.- Then should take the loged on Username

4.- With this Username, the script should go to a 'Access DB' and take all the net drives assigned to this user and mount them.

5.- The script should make the same but looking in another 'Access DB' and searching the net drives to mount by the groups assigned to this Username in the Windows Active Directory

Thank U so much!!!

Link to comment
Share on other sites

To norrow down the problem I would suggest to

  • download the current version of adfunctions.au3 (see link in post #2). Yours is quite old
  • Test the smallest possible script. Could you please run my test script from post #4 (without incorporating it into your script) and post the Scite Console?
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

To norrow down the problem I would suggest to

  • download the current version of adfunctions.au3 (see link in post #2). Yours is quite old
  • Test the smallest possible script. Could you please run my test script from post #4 (without incorporating it into your script) and post the Scite Console?

Ok, thanks, im going to check it and ill post the results

Link to comment
Share on other sites

To norrow down the problem I would suggest to

  • download the current version of adfunctions.au3 (see link in post #2). Yours is quite old
  • Test the smallest possible script. Could you please run my test script from post #4 (without incorporating it into your script) and post the Scite Console?
Hi,

just a suggestion:

Why you don't use central login script on domain controller? This can do all the task you mentioned above. If you have to make changes, you don't have to copy exe files on all client machines.

Have a look at Kix Script. Works absolutely fine in combination with MS AD.

;-))

Stefan

Link to comment
Share on other sites

I´ve downloaded and updated the current version of 'adfunctions.au3' over my old one.

Running alone your test script from post #4 the result is this

#include <AD.au3>     ;---------------------------------> This is the name i gave to adfuntions
Global $asGroups[1] 
_ADGetUserGroups($asGroups, @UserName) 
_ArrayDisplay($asGroups)

Result just compiling this piece of code:

Posted Image

Edited by cajadeavila
Link to comment
Share on other sites

Hi,

just a suggestion:

Why you don't use central login script on domain controller? This can do all the task you mentioned above. If you have to make changes, you don't have to copy exe files on all client machines.

Have a look at Kix Script. Works absolutely fine in combination with MS AD.

;-))

Stefan

Yes thats a good idea but first i wanna make it func in autoit. Kix looks fine . thanks 99ojo

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