ReitzelTechnology Posted February 4, 2012 Share Posted February 4, 2012 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 More sharing options...
water Posted February 4, 2012 Share Posted February 4, 2012 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
ReitzelTechnology Posted February 4, 2012 Author Share Posted February 4, 2012 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 More sharing options...
water Posted February 4, 2012 Share Posted February 4, 2012 As the sAMAccountName attribute of any object is unique in the domain you have to change line If _AD_ObjectExists(@ComputerName & "$" & $sAD_OU) ThentoIf _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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
ReitzelTechnology Posted February 4, 2012 Author Share Posted February 4, 2012 As the sAMAccountName attribute of any object is unique in the domain you have to change line If _AD_ObjectExists(@ComputerName & "$" & $sAD_OU) ThentoIf _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 More sharing options...
water Posted February 4, 2012 Share Posted February 4, 2012 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
ReitzelTechnology Posted February 4, 2012 Author Share Posted February 4, 2012 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? Link to comment Share on other sites More sharing options...
water Posted February 4, 2012 Share Posted February 4, 2012 (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 February 4, 2012 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
ReitzelTechnology Posted February 4, 2012 Author Share Posted February 4, 2012 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 Link to comment Share on other sites More sharing options...
water Posted February 4, 2012 Share Posted February 4, 2012 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
ReitzelTechnology Posted February 4, 2012 Author Share Posted February 4, 2012 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 More sharing options...
water Posted February 4, 2012 Share Posted February 4, 2012 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
ReitzelTechnology Posted February 4, 2012 Author Share Posted February 4, 2012 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 More sharing options...
water Posted February 4, 2012 Share Posted February 4, 2012 _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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
ReitzelTechnology Posted February 4, 2012 Author Share Posted February 4, 2012 _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. Link to comment Share on other sites More sharing options...
water Posted February 4, 2012 Share Posted February 4, 2012 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
ReitzelTechnology Posted February 4, 2012 Author Share Posted February 4, 2012 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 Link to comment Share on other sites More sharing options...
water Posted February 4, 2012 Share Posted February 4, 2012 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
ReitzelTechnology Posted February 4, 2012 Author Share Posted February 4, 2012 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 Link to comment Share on other sites More sharing options...
water Posted February 4, 2012 Share Posted February 4, 2012 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 - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now