Avatar of karephre
karephre
 asked on

run vbscript with alternate credentials

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

VB Script

Avatar of undefined
Last Comment
RobSampson

8/22/2022 - Mon
RobSampson

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

ASKER
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

ASKER CERTIFIED SOLUTION
RobSampson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
karephre

ASKER
Work like a champ ... Thanks RobSampson
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
RobSampson

No problem. Thanks for the grade.

Rob.