Jump to content

Active Directory Scripts


Johny Clelland
 Share

Recommended Posts

You want to move OUs including all sub-OUs and containers to a new OU? Or just the computers in this source OUs and sub-OUs?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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'm not sure this is possible (I just have read-only access to our AD).

Could you please add

_AD_ErrorNotify(2)

at the top of your script so we get better error information?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 did some research and found a script that confirms that moving an OU should be possible.

At the moment function _AD_MoveObject is designed to move CN-objects (user, group, computer ...) but not OUs.

But it should be possible to adapt the function to move OUs as well.

If you have some spare time and are willing to do some tests then I'm going to modify the function.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 will do some tests tomorrow and hope to come up with a solution in the next few days.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

This is the code I've found on the web translated to AutoIt.

#include <AD.au3>
_AD_Open()
$sSourceOU = "OU=Computers,OU=carpark,OU=Parking,DC=sd0500017,DC=carpark" ; The OU to be moved
$sTargetOU = "OU=TEMP,DC=sd0500017,DC=carpark"
Local $oTargetOU = __AD_ObjGet("LDAP://" & $sAD_HostServer & "/" & $sTargetOU)
ConsoleWrite(@error & @CRLF)
$oTargetOU.MoveHere("LDAP://" & $sAD_HostServer & "/" & $sSourceOU, Null)
; $oTargetOU.MoveHere("LDAP://" & $sAD_HostServer & "/" & $sSourceOU, Default)
; $oTargetOU.MoveHere("LDAP://" & $sAD_HostServer & "/" & $sSourceOU, "")
; $oTargetOU.MoveHere("LDAP://" & $sAD_HostServer & "/" & $sSourceOU)
ConsoleWrite(@error & @CRLF)
_AD_Close()

One of the 4 MoveHere variants should work.

PLEASE CHECK $sSourceOU and $sTargetOU in advance!!

Could you please test and post the results?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

This is the code I've found on the web translated to AutoIt.

#include <AD.au3>
_AD_Open()
$sSourceOU = "OU=Computers,OU=carpark,OU=Parking,DC=sd0500017,DC=carpark" ; The OU to be moved
$sTargetOU = "OU=TEMP,DC=sd0500017,DC=carpark"
Local $oTargetOU = __AD_ObjGet("LDAP://" & $sAD_HostServer & "/" & $sTargetOU)
ConsoleWrite(@error & @CRLF)
$oTargetOU.MoveHere("LDAP://" & $sAD_HostServer & "/" & $sSourceOU, Null)
; $oTargetOU.MoveHere("LDAP://" & $sAD_HostServer & "/" & $sSourceOU, Default)
; $oTargetOU.MoveHere("LDAP://" & $sAD_HostServer & "/" & $sSourceOU, "")
; $oTargetOU.MoveHere("LDAP://" & $sAD_HostServer & "/" & $sSourceOU)
ConsoleWrite(@error & @CRLF)
_AD_Close()

One of the 4 MoveHere variants should work.

PLEASE CHECK $sSourceOU and $sTargetOU in advance!!

Could you please test and post the results?

 

Error

1st: Type mismatch

2nd: Parameter not optional

3rd: An invalid directory pathname was passed

4th: Invalid number of parameters

The Source and Target OU's are ok.

Edited by Rijswijker
Link to comment
Share on other sites

Thanks for the reply!

What is the value of @error for thise 4 variantes?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

Thanks for the reply!

What is the value of @error for thise 4 variantes?

Error

1st: Type mismatch 0 and -2147352571

2nd: Parameter not optional 0 and-2147352561

3rd: An invalid directory pathname was passed 0 and-2147352567

4th: Invalid number of parameters 0 and-2147352562

Sorry for my late response.

Edited by Rijswijker
Link to comment
Share on other sites

I will post the VB script so we can see if the problem is AD or AutoIt related.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

  • 4 weeks later...

Sorry, forgot to post. Will do tomorrow. 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

The VB script I was talking about can be found here.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

  • 10 months later...

Hi,

I need to remove all groups from "Member Of" tab, and add two groups to it, for a list of users in an OU. How can I do that...?

Anyone got this piece of code or similar, will be appreciated.

TIA

 

Edited by BigNoter
Link to comment
Share on other sites

In short:

Grab the list of users from the OU by using function _AD_GetObjectsInOU. One of the returned properties should be "memberOf".
Loop through the resulting array of users.
Split property "memberOf" into an array (separator is "|"). Loop through this second array and call function _AD_RemoveUserFromGroup for each of the groups.
Then call _AD_AddUserToGroup to add new groups to the user.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

2 minutes ago, water said:

In short:

Grab the list of users from the OU by using function _AD_GetObjectsInOU. One of the returned properties should be "memberOf".
Loop through the resulting array of users.
Split property "memberOf" into an array (separator is "|"). Loop through this second array and call function _AD_RemoveUserFromGroup for each of the groups.
Then call _AD_AddUserToGroup to add new groups to the user.

Thnx for the quick reply.

This pseudo code helps, but a little.

last time I did code was, as I remember, 2007...!

If you, or anybody, provide a piece of code will help a lot.

TIA

Link to comment
Share on other sites

Untested! Please make sure to test on a test system only!!

#include <AD.au3>
Global $sOU = "" ; OU to process
Global $aObjects, $aGroups

; Open Connection to the Active Directory
_AD_Open()
If @error Then Exit MsgBox(16, "AD", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)

; Get a list of users in the specified OU (subtree)
$aObjects = _AD_GetObjectsInOU($sOU, "(&(objectcategory=person)(objectclass=user))", 2, "sAMAccountName,memberof")
If @error > 0 Then Exit MsgBox(16, "AD", "Function _AD_GetObjectsInOU encountered a problem. @error = " & @error & ", @extended = " & @extended)

; Process all users
For $i = 1 To $aObjects[0][0]
    $aGroups = StringSplit($aObjects[$i][1], "|")
    For $j = 1 To $aGroups[0]
        ; Remove User from group
        _AD_RemoveUserFromGroup($aGroups[$j], $aObjects[$i][0]) 
        If @error > 0 Then Exit MsgBox(16, "AD", "Function _AD_RemoveUserFromGroup encountered a problem. @error = " & @error & ", @extended = " & @extended)
    Next
    ; Add User to new groups (repeat the statement for each group to be added)
    _AD_AddUserToGroup(">>Set groupname here<<", $aObjects[$i][0]) 
    If @error > 0 Then Exit MsgBox(16, "AD", "Function _AD_AddUserToGroup encountered a problem. @error = " & @error & ", @extended = " & @extended)
Next

_AD_Close()

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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...