Solved

How can I impersonate an account in vbscript

Posted on 2011-03-22
5
1,697 Views
Last Modified: 2012-05-11
I am trying to configure some VBScripts to run within a third-party system.  I need to impersonate a domain user from within the script.  How can this be done?  Normally the WMI classes allow impersonate, but, I am trying to use the Scripting.FileSystemObject.
0
Comment
Question by:wls3
  • 2
  • 2
5 Comments
 
LVL 7

Expert Comment

by:holthd
ID: 35194809
Is it an possiblity to run the entire script as another user? If so you can achieve this using Windows' built-in 'runas.exe' or a scheduled task, depending on your scenario.

If you trigger it remotely you can use 'psexec.exe' by Sysinternals (Microsoft).

If you need to trigger only a portion of the script as userA while the main script runs as userB you can allways re-call a portion of the script through WshShell.Run "Runas.exe <args> C:\Scripts\PortionOfMainScript.vbs". Might get messy. Use arguments to pass variables to the portion script.

-Daniel
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 35195355
If you want to do it with the FileSystemObject, you need to create a pipe, using alternate credentials.

            
		On Error Resume Next
		objNetwork.RemoveNetworkDrive "\\" & strComputer & "\IPC$", True, False
		Err.Clear
		On Error GoTo 0
		objNetwork.MapNetworkDrive "", "\\" & strComputer & "\IPC$", False, strUsername, strPassword
		' Use FSO methods now
		

Open in new window


Regards,

Rob.
0
 
LVL 10

Author Comment

by:wls3
ID: 35199016
Thanks Rob.  I had not had to dig into those NetworkDrive methods before.  I am checking them out on MSDN now.  Appreciate the pointer.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35202815
Thanks for the grade, you'll also want to remove the mapping again after you use the FSO methods.

Rob.
0
 
LVL 10

Author Comment

by:wls3
ID: 35203336
Appreciate the follow up comment.  I had seen some other folks suggesting something along these lines, but, I didn't know this was essentially creating a pipe.  From what I had seen I just assumed the close was to dispose of the object, not a pipe.  Again, thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question