Jump to content
TheCrimsonCrusader

Trying to delete AD computer object in as few lines as possible

Recommended Posts

TheCrimsonCrusader

I'm trying to take the following working VBScript:

strComputer = "LAB820-G3"
set objComputer = GetObject("LDAP://CN=" & strComputer & ",OU=COE,OU=Workstations-Win10,DC=corp,DC=CompanyName,DC=com")
objComputer.DeleteObject (0)

And convert it into AutoIt.  However, when I run the compiled code, I receive the "Error:  Variable must be of type 'Object'.  I am aware of Water AD library, but after dropping the AD.au3 into the AutoIt Include folder, it was getting the message about the WinAPIConv.au3 missing, so I found one on the Internet and dropped that one in as well, but then it ran into additional problems.  Regardless, I didn't need that additional functionality anyway, I was just trying to delete an object from AD with a simple script consisting of a few lines.

Is what I have below even in the ball part or am I way off?  Obviously, I need to address the "Error:  Variable must be of type 'Object' issue, but not sure how to or if I will be running into other issues after that as well.  What I did looks logical, but I'm not the sharpest tool in the shed when it comes to this stuff.  :-)  I can't call netdom from AutoIt because I need this script to delete computer objects regardless of whether they are currently active on the network or not and netdom is limited to online only objects from what I understand.

Any help would be greatly appreciated!

$strComputer = "LAB820-G3"
$objComputer = ObjGet("LDAP://CN=" & $strComputer & ",OU=COE,OU=Workstations-Win10,DC=corp,DC=CompanyName,DC=com")
$GetReturnCode = $objComputer.DeleteObject (0)

MsgBox(262144,"Title",$ReturnCode)

 

Share this post


Link to post
Share on other sites
water
Posted (edited)

The VBScript code does work for you?

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
TheCrimsonCrusader

Yes, I just have to "Run as different user" and specify account credentials that have permissions to delete the computer object.  I have ran it a few times on some test computer objects to confirm it works and is consistent.

Share this post


Link to post
Share on other sites
water

Then I suggest to add a COM error handler to get better debugging info:

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

$strComputer = "LAB820-G3"
$objComputer = ObjGet("LDAP://CN=" & $strComputer & ",OU=COE,OU=Workstations-Win10,DC=corp,DC=CompanyName,DC=com")
$GetReturnCode = $objComputer.DeleteObject (0)

MsgBox(262144,"Title",$ReturnCode)

; User's COM error function. Will be called if COM error occurs
Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
TheCrimsonCrusader

Hm, nothing is come back for the $GetReturnCode (I corrected it for the message box variable).

I guess the problem lies with the $ObjComputer.DeleteObject (0) since it doesn't even return a return code. 

Test.au3 (4) : ==> COM Error intercepted !
    err.number is:         0x8007202B
    err.windescription:    A referral was returned from the server.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     4
    err.retcode is:     0x00000000

Test.au3 (5) : ==> COM Error intercepted !
    err.number is:         0x000000A9
    err.windescription:    Variable must be of type 'Object'.
    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     5
    err.retcode is:     0x00000000

Share this post


Link to post
Share on other sites
water

The message is best described here: https://stackoverflow.com/questions/6954170/a-referral-was-returned-from-the-server-exception-when-accessing-ad-from-c-sha

Quote

A referral is sent by an AD server when it doesn't have the information requested itself, but know that another server have the info. It usually appears in trust environment where a DC can refer to a DC in trusted domain.

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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

×