4

I have a batch that is being run with administrative privileges on a Windows 7 machine. That batch has to run another batch, which should access a mapped network drive. However, network drives are only mapped for the non-administrator part of the logged-in user. Since the second batch doesn't need the administrative rights, I'd like it to run as a limited user, and that will let it access the mapped drives. Is there any way to do that?

To make what I want clear, I'd like to get the following flow:

  1. User eran logs into PC. eran is a local administrator.
  2. eran runs b1.bat as administrator.
  3. b1.bat runs b2.bat with eran's limited user credentials (how? that's what I'm asking...)
  4. b2.bat accesses some mapped drives is now has access to.

Just in case the reason for the problem is not clear...

EEAA
  • 109,363
  • 18
  • 175
  • 245
Eran
  • 267
  • 1
  • 6
  • 14
  • Could you use drive mapping in the b2.bat file? net map z: \\server\share. Then follow up with net use z: /d? – Nixphoe Jul 10 '11 at 16:20
  • @Nixphoe - well, that's an interesting suggestion... not exactly what I was hoping for, but if there's no other way, I might go for it. Could you please write it as an answer? – Eran Jul 10 '11 at 16:53
  • I updated my answer with that suggestion. – Nixphoe Jul 11 '11 at 01:25

2 Answers2

2

You can use the runas command. Check out runas /? or Microsoft runas for more info. Put this in your b1.bat file.

runas /noprofile /user:username@domain.local "C:\batch\b2.bat"

Credit here


If that doesn't work you could map the drive in the b2.bat file. Add net map z: \\server\share. Then follow up with net use z: /d

Nixphoe
  • 4,584
  • 7
  • 34
  • 52
  • Thanks, but this doesn't work :(... I'm using the same user to run both batches, and the admin credentials seem to propagate from the first to the second (I can't use some arbitrary local user, because then I won't have the network drives mapped). – Eran Jul 10 '11 at 14:31
1

Create a scheduled task as user2. User1 executes that scheduled task which is configured to run in the context of user2. schtasks is the interface to run from the command line.

"schtasks /Run /TN taskname" would run the batch file for the other user.

Luke
  • 64
  • 3