Page 1 of 3
.bat run as admin but I need logon user
Posted: 05 Dec 2014 09:03
by Krump
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?
Re: .bat run as admin but I need logon user
Posted: 05 Dec 2014 10:12
by Squashman
%username%
Re: .bat run as admin but I need logon user
Posted: 05 Dec 2014 11:00
by Krump
When I run bat as admin, %username% is admin name not logged user.
Re: .bat run as admin but I need logon user
Posted: 05 Dec 2014 15:14
by foxidrive
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?
Re: .bat run as admin but I need logon user
Posted: 05 Dec 2014 16:49
by Krump
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.
Re: .bat run as admin but I need logon user
Posted: 05 Dec 2014 21:22
by foxidrive
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.
Re: .bat run as admin but I need logon user
Posted: 05 Dec 2014 21:46
by ShadowThief
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.
Re: .bat run as admin but I need logon user
Posted: 06 Dec 2014 16:45
by Krump
I'm logged on te desktop as someone user and in this account run aplication as admin. And this change the value.
Re: .bat run as admin but I need logon user
Posted: 06 Dec 2014 23:27
by Squashman
Parse the output of one of these commands.
or
All I did was Google search for the main points of your question.
It was the first link Google Found.
Second Link Google found.
Re: .bat run as admin but I need logon user
Posted: 08 Dec 2014 05:58
by Krump
Ou! It's very helpful. Now I trying inport it to my bat file.
Re: .bat run as admin but I need logon user
Posted: 17 Dec 2014 11:42
by Krump
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.
Re: .bat run as admin but I need logon user
Posted: 17 Dec 2014 13:50
by Squashman
Set user=%user:*\=%
Re: .bat run as admin but I need logon user
Posted: 17 Dec 2014 14:15
by Krump
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.
Re: .bat run as admin but I need logon user
Posted: 17 Dec 2014 14:21
by Squashman
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.
Re: .bat run as admin but I need logon user
Posted: 17 Dec 2014 14:54
by Krump
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.