run vbscript with alternate credentials

karephre
karephre used Ask the Experts™
on
Hello I use a local script to query my active directory. On the part that deletes a user or computer object. i would like that to run on administrator credentials.

I know how to set it up if I was using VB to run on a remote system, but cant figur out how to get it to run on a local one. with out doing run as admin.
Below is the part of my code I want to run under admin.

thanks experts

Public Function delMac(macName,macLoc)
On Error Resume Next 
Set objOU = GetObject("LDAP://"& macLoc &"")
objOU.Delete "user", "cn="& macName &""

End Function 

Public Function delCom(ComDn)
On Error Resume Next
	set objComputer = GetObject("LDAP://" & ComDn & "")
objComputer.DeleteObject (0)
	
End Function 

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hi, you can use OpenDSObject.

      Const ADS_SECURE_AUTHENTICATION = 1
      Set objNS = GetObject("LDAP:")
      Set objOU = objNS.OpenDSObject("LDAP://" & macLoc, "test\admin", "password",ADS_SECURE_AUTHENTICATION)
        objOU.Delete "user", "cn=" & macName

Open in new window


Regards,

Rob.
karephreIT Specialist (Server Management)

Author

Commented:
Below is what I added to my code, and im getting object already exists. After the computers are deleted I create them again.

Public Function delMac(macName,macLoc)
On Error Resume Next 
Const ADS_SECURE_AUTHENTICATION = 1
Set objOU = GetObject("LDAP://"& macLoc ,"d.hot", "THer$#2",ADS_SECURE_AUTHENTICATION)
objOU.Delete "user", "cn="& macName &""

End Function 

Public Function delCom(ComDn)
On Error Resume Next
Const ADS_SECURE_AUTHENTICATION = 1
	set objComputer = GetObject("LDAP://" & ComDn ,"d.hot", "THer$#2",ADS_SECURE_AUTHENTICATION)
objComputer.DeleteObject (0)
	
End Function 

Open in new window

Most Valuable Expert 2012
Top Expert 2014
Commented:
That won't be correct.  You are putting the credentials into the GetObject call, instead of using OpenDSObject.

Try this to delete the computer object
Public Function delCom(ComDn)
	Const ADS_SECURE_AUTHENTICATION = 1
	Set objNS = GetObject("LDAP:")
	Set objComputer = objNS.OpenDSObject("LDAP://" & ComDN, "domain\username", "password",ADS_SECURE_AUTHENTICATION)
	objComputer.DeleteObject(0)
End Function

Open in new window


Note that I have removed On Error Resume Next so you can see any error you may have.  You can put that back in after you've tested it.

Regards,

Rob.
karephreIT Specialist (Server Management)

Author

Commented:
Work like a champ ... Thanks RobSampson
Most Valuable Expert 2012
Top Expert 2014

Commented:
No problem. Thanks for the grade.

Rob.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial