I would like a way to initiate a single command file - but that runs both with the full admin rights and the non-Admin token -
Either running as non-admin - and converting to admin - or running as admin and initiating a non-admin process would be fine
In particular I need to run the commands
SUBST E: G:\SUBST\E
NET USE Y: \\SVR\PATH
both as an administrator and as a non-administrator
Built in stuff such as VBS, PS would be ok - I'm using Windows 7
I've the following batch file with RUNAS and confusingly it does not work at all - I also tried the direct runas /user:domain\usename /savecred "cmd /c subst" - I wrote the batch files to see what was happening
a1.bat
subst e: C:\Users\username\Documents\subst\e
subst
pause
A.bat
runas /user:domain\usename /savecred "cmd /c C:\Users\username\Documents\subst\E\a1.bat"
- If I run A1.bat in an Admin CMD THEN other admin CMD environments get the drive
- if I run A.bat in an Admin CMD THEN No admin CMDs except the temporary runas one gets the drive -including the original
- If I run A1.bat in a non-Admin cmd THEN other admin CMD get the drive
- if I run A.bat in a non-Admin cmd THEN no cmd environment gets the drive - except the temporary runas one
I have not tried with the NET USE commands
The background is that what I have found is that when I run
NET USE Y: \\SVR\PATH
without the administrative rights then I do not see the Y: drive when I later run a command with the administrative rights - at the moment I just have two shortcuts on my desktop - which I double click - one which prompts for admin and one which does not - I need to double click the Admin one first as there at some programs which are single instance only , stay resident and need to run as admin.
I have a simple keyboard shortcut menu system which runs various commands when I press keycombinations - To make this portable I use a SUBST command to map my E: drive to a location. As some of the commands need to be run as Administrator - the menu system needs the with admin and without admin environments to have the same drives mappings
Answer
Since you say you are fine with built in Windows functions, I've provided a few batch file solutions below with the admin and non-admin operations I tested with Windows 7. Since you say you are prompted for admin, this solution will also prompt for the password of the/an admin credential to run it as with one having an option to save for future use after entering it the first time only.
Batch File (Admin Prompted)
RUNAS /USER: "CMD /C SUBST E: G:\SUBST\E"
RUNAS /USER: "CMD /C NET USE Y: \\SVR\PATH"
Note: When you use this method, you have to type in the password of the administrator account you run the commands as per each time
these commands are run. You can useCMD /K
here instead if you wish the elevated command prompt to stay up after running the commands.
To Remove Saved Credentials
In the Run dialog (⊞
+R
) type in rundll32.exe keymgr.dll, KRShowKeyMgr
and press Enter
. When the Stored User Names and Passwords windows opens up, you can highlight the credential(s) you want to delete which is(are) saved and then press the Remove option.
Batch File (Admin with /SaveCred
)
RUNAS /USER: /SAVECRED "CMD /C SUBST E: G:\SUBST\E"
RUNAS /USER: /SAVECRED "CMD /C NET USE Y: \\SVR\PATH"
Note: When you use this method, you have to type in the password of the administrator account the first time you use it only and then on subsequent runs when you specify the
/SAVECRED
switch you do not need to enter the password any longer. You can useCMD /K
here instead if you wish the elevated command prompt to stay up after running the commands.
Batch File (Non-Admin)
SUBST E: G:\SUBST\E
NET USE Y: \\SVR\PATH
Note: When you use this method, you are not prompted and basically you run as the non-elevated user you are signed onto the machine as
security context wise.
No comments:
Post a Comment