Jump to content

If Object Exists in OU


Recommended Posts

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 & "$"

Link to comment
Share on other sites

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

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

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
Link to comment
Share on other sites

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

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

But will it find the computer within the specified OU? Its going to exists in AD but we want to check to see if it is in the OU before running the move command
Link to comment
Share on other sites

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

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

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

get following error

post-60598-0-36347200-1328392112_thumb.p

Link to comment
Share on other sites

Edited my code above - should work now.

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

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
Link to comment
Share on other sites

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

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
Link to comment
Share on other sites

_AD_Open is missing.

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

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

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

Don't get what you mean.

Can you post a screenshot?

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