Sign in to follow this  
Followers 0
ReitzelTechnology

If Object Exists in OU

32 posts in this topic

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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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).

Can you give me an example of this?

Share this post


Link to post
Share on other sites

#8 ·  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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Edited my code above - should work now.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

_AD_Open is missing.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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.

no error and _arraydisplay shows the distinguished name

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Don't get what you mean.

Can you post a screenshot?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0