.bat run as admin but I need logon user

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Krump
Posts: 45
Joined: 02 Dec 2014 14:53

.bat run as admin but I need logon user

#1 Post by Krump » 05 Dec 2014 09:03

Hi,

I run .bat as admin, but in script I run programs (call) and I must run it as logon user. I know that exist command runas /user: but how can I get automaticlly username?

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: .bat run as admin but I need logon user

#2 Post by Squashman » 05 Dec 2014 10:12

%username%

Krump
Posts: 45
Joined: 02 Dec 2014 14:53

Re: .bat run as admin but I need logon user

#3 Post by Krump » 05 Dec 2014 11:00

When I run bat as admin, %username% is admin name not logged user.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: .bat run as admin but I need logon user

#4 Post by foxidrive » 05 Dec 2014 15:14

Krump wrote:When I run bat as admin, %username% is admin name not logged user.

How are you running the batch file as admin?
Are you logged in as admin?

Krump
Posts: 45
Joined: 02 Dec 2014 14:53

Re: .bat run as admin but I need logon user

#5 Post by Krump » 05 Dec 2014 16:49

I'm logged on some one user for example pltomywar. then i copy batch file from server to desktop and right click -> run as administrator. Next windows want admin autorization, so I write my admin pass for example login: plaxgo pass: 123456.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: .bat run as admin but I need logon user

#6 Post by foxidrive » 05 Dec 2014 21:22

Krump wrote:I'm logged on some one user for example pltomywar. then i copy batch file from server to desktop and right click -> run as administrator. Next windows want admin autorization, so I write my admin pass for example login: plaxgo pass: 123456.


I'm not sure what you are doing at all.

Explain the task fully and someone will help you.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: .bat run as admin but I need logon user

#7 Post by ShadowThief » 05 Dec 2014 21:46

Krump wrote:I'm logged on some one user for example pltomywar. then i copy batch file from server to desktop and right click -> run as administrator. Next windows want admin autorization, so I write my admin pass for example login: plaxgo pass: 123456.

Are you logged in to the server or the desktop? Because right-click->Run as Administrator doesn't change the value of %username%... unless you're logged into the Desktop as Administrator.

Krump
Posts: 45
Joined: 02 Dec 2014 14:53

Re: .bat run as admin but I need logon user

#8 Post by Krump » 06 Dec 2014 16:45

I'm logged on te desktop as someone user and in this account run aplication as admin. And this change the value.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: .bat run as admin but I need logon user

#9 Post by Squashman » 06 Dec 2014 23:27

Parse the output of one of these commands.

Code: Select all

query sessions

or

Code: Select all

query user


All I did was Google search for the main points of your question.
It was the first link Google Found.

Second Link Google found.

Code: Select all

WMIC ComputerSystem Get UserName

Krump
Posts: 45
Joined: 02 Dec 2014 14:53

Re: .bat run as admin but I need logon user

#10 Post by Krump » 08 Dec 2014 05:58

Ou! It's very helpful. Now I trying inport it to my bat file.

Krump
Posts: 45
Joined: 02 Dec 2014 14:53

Re: .bat run as admin but I need logon user

#11 Post by Krump » 17 Dec 2014 11:42

Hi, Is't me again unfortunatly

I have two problems.

First... When I use function "runas /user:" in bat file, it don't wait for ending task as like as using call (problem is that I run next .bat, not .exe). How I can to force for waiting?

Second... I use "FOR /F "tokens=2 delims='='" %%A in ('wmic ComputerSystem Get username /value') do SET user=%%A" and I have domain first (example: mydomain\username). How I can cut domain and slash. I want only username.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: .bat run as admin but I need logon user

#12 Post by Squashman » 17 Dec 2014 13:50

Set user=%user:*\=%

Krump
Posts: 45
Joined: 02 Dec 2014 14:53

Re: .bat run as admin but I need logon user

#13 Post by Krump » 17 Dec 2014 14:15

Great! Second works perfectly :)

And now stay only first, what I can do with it? runas is very nice command but it don't wait for ending.


Or maybe are another better ways to run .bat in .bat as admin.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: .bat run as admin but I need logon user

#14 Post by Squashman » 17 Dec 2014 14:21

I am not sure if you are being clear on what is happening.

You said in a previous post that you are using the Right Click Context Menu to launch that batch file as your admin user.

I don't want to sit here an pull teeth. So if you can show us the batch file and explain how you are running it, we may be able to help with more information.

Krump
Posts: 45
Joined: 02 Dec 2014 14:53

Re: .bat run as admin but I need logon user

#15 Post by Krump » 17 Dec 2014 14:54

I change conception (when I Right-Click as Admin on bat and when I start outlook, Outlook don't automatically import data -> logon admin account) So in main bat (I start normaly) do somthing operations (among other things, run outlook) and in first bat start as admin second .bat.

So step by step.
1. First bat start as normal user (bat copy, open, install everything what don't need admin powers and I can normaly use %username% %localappdata% %localapp% etc)
2. In first .bat I open another bat, where are operations whose needs admin powers, and I use this script in first to run second:

:AUTORIZATION
echo.
echo ________________________________________________
set /p login_usera=Admin name:


echo.
runas /user:mydomain.net\%login_usera% "\\Tablety\asadmin.bat"
if "%ERRORLEVEL%"=="1" goto AUTORIZATION

3. And when second bat start and do what it must, my first bat go further. I want first bat wait until the second bat finish do everything and close. Some like as "call" or "start /wait" with admin powers.
Last edited by Krump on 17 Dec 2014 14:59, edited 1 time in total.

Post Reply