Jump to content

Recommended Posts

Posted

I have a script that automatically moves computers to the correct OU In Active Directory based on their name. It works fine except that if it is already in the OU it will keep trying to move it.

I want to be able to have an IF statement to check to see if the object exists in the target OU. I am not sure which command to use or parameters.

Here is the OU: OU=Workstations,OU=Reitzel_Technology,DC=ReitzelTechnology,DC=int, Computer name would @ComputerName & "$"

Posted

Please have a look at function _AD_ObjectExists in my AD UDF.

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

 

Posted

Please have a look at function _AD_ObjectExists in my AD UDF.

This is what I have right now for the function:

Func _WorkstationMove()
 Global $iResult,$sAD_OU="OU=Workstations,OU=Reitzel_Technology,DC=ReitzelTechnology,DC=int", $sLogMsg="Computer moved to:" & $sAD_OU
 ; Open Connection to the Active Directory
 _AD_Open($sAD_UserIdParam, $sAD_PasswordParam)
 If _AD_ObjectExists(@ComputerName & "$" & $sAD_OU) Then
  _FileWriteLog($sLogPath, "Computer Exists in target OU", -1)
  Else
  _AD_MoveObject($sAD_OU, @ComputerName & "$")
  _FileWriteLog($sLogPath, $sLogMsg, -1)
 EndIf
 _AD_Close()
EndFunc
Posted

As the sAMAccountName attribute of any object is unique in the domain you have to change line

If _AD_ObjectExists(@ComputerName & "$" & $sAD_OU) Then
to
If _AD_ObjectExists(@ComputerName & "$") Then

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

 

Posted

If _AD_ObjectExists returns 1 then you can use function _AD_GetObjectProperties to get the distinguishedname and cn properties. Strip off the cn from the distinguishedname and you have the OU (FQDN).

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

 

Posted

If _AD_ObjectExists returns 1 then you can use function _AD_GetObjectProperties to get the distinguishedname and cn properties. Strip off the cn from the distinguishedname and you have the OU (FQDN).

Can you give me an example of this?

Posted (edited)

Can't test it at the moment but something like this.

If _AD_ObjectExists(@ComputerName & "$") Then
    $aResult = _AD_GetObjectProperties(@ComputerName & "$", "distinguishedname, cn")
    If @error then Exit MsgBox(... handle the error here ...)
    $sOU = StringMid($aResult[1][0], StringLen($aResult[1][1])+1)
    If $sOU = ... ; for the OUs being equal
EndIf
Edited by water

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

 

Posted

Edited my code above - should work now.

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

 

Posted

This is what we have right now for code and it is susposed to not move the machine if is present and write to the log that machine already exists. It keeps writing that it was moved.

Func _WorkstationMove()
 Global $iResult,$sAD_OU="OU=Workstations,OU=Reitzel_Technology,DC=ReitzelTechnology,DC=int", $sLogMsg="Computer moved to:" & $sAD_OU
 ; Open Connection to the Active Directory
 _AD_Open($sAD_UserIdParam, $sAD_PasswordParam)
 If _AD_ObjectExists(@ComputerName & "$") Then
    $aResult = _AD_GetObjectProperties(@ComputerName & "$", "distinguishedname, cn")
    $sOU = StringMid($aResult[1][0], StringLen($aResult[1][1])+1)
    If $sOU = $sAD_OU Then
  _FileWriteLog($sLogPath, "Computer Exists in target OU", -1)
  Exit
 Else
 _AD_MoveObject($sAD_OU, @ComputerName & "$")
 _FileWriteLog($sLogPath, $sLogMsg, -1)
EndIf
EndIf
 _AD_Close()
EndFunc
Posted

I'm not at my Windows PC at the moment and hence can't test it myself.

You have to do some error testing yourself.

Does any of the _AD_* function calls return an error (@error <> 0), what does the returned array look like etc.

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

 

Posted

Says variable must be type object if I try to move it to a different script and have it simply return a dialog box with the ou

#include <[url="file://reitzeltechnology.intadm$AutoIt_ScriptsAD.au3"]reitzeltechnology.intadm$AutoIt_ScriptsAD.au3[/url]>
If _AD_ObjectExists(@ComputerName & "$") Then
  $aResult = _AD_GetObjectProperties(@ComputerName & "$", "distinguishedname, cn")
  $sOU = StringMid($aResult[1][0], StringLen($aResult[1][1]) + 1)
  MsgBox(0,"1", "OU IS" & $sOU)
 EndIf
Posted

_AD_Open is missing.

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

 

Posted

_AD_Open is missing.

Duh brainfart....

Added that and it runs the script, but the popup box it returns just shows "OU IS" and not the OU that the function should have returned. Maybe thats the issue with the other script.

Posted

What I said above: I can't test at the moment.

You get an array from _AD_GetObjectProperties. So please check @error after calling the function. If 0 display the array using _ArrayDisplay and see what you get.

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

 

Posted

What I said above: I can't test at the moment.

You get an array from _AD_GetObjectProperties. So please check @error after calling the function. If 0 display the array using _ArrayDisplay and see what you get.

no error and _arraydisplay shows the distinguished name

Posted

Does the array show the cn as well (should be "CN=" & @Computername & "$") or is it just @Computername & "$"?

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

 

Posted

Does the array show the cn as well (should be "CN=" & @Computername & "$") or is it just @Computername & "$"?

It has CN under col 0 and the computer name under col 1.

It has distinguishedname under col 0 and and CN=computername,OU=workstations..... under col 1

Posted

Don't get what you mean.

Can you post a screenshot?

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

 

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
  • Recently Browsing   0 members

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