.bat run as admin but I need logon user

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

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

#16 Post by Squashman » 17 Dec 2014 14:58

I have no idea what FIRST BAT and SECOND BAT are.
I know you are trying to explain it buy BE VERY SPECIFIC!

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

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

#17 Post by Krump » 17 Dec 2014 15:05

First BAT install/copy/open programs that don't need admin powers.
Second BAT instal/copy/openl programs that need admin powers.

In first BAT I open Second (with admin rights)

And when second don't finish, first must wait

If you still dont udnerstand i copy source.

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

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

#18 Post by Squashman » 17 Dec 2014 15:11

Krump wrote:If you still dont udnerstand i copy source.

I asked for that a couple of posts back.

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

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

#19 Post by Krump » 17 Dec 2014 15:12

First Bat:

Code: Select all

@echo off

mode con:cols=80 lines=25

echo Instalator Konfiguracji Uzytkownika Koncowego

:AUTORIZATION
echo.
echo ________________________________________________
set /p login_usera=Podaj login administratora:


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

echo.
echo Konfiguracja OutLook
call "D:\Tablety\Sygnatura.docx"
call "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office 2013\Outlook 2013.lnk"

echo.
echo Instalacja T-Smart'a
copy "D:\Tablety\T-Smart.url" "%userprofile%\Desktop\"
call "%userprofile%\Desktop\T-Smart.url"

echo.
echo Odinstalowywanie Aplikacji PowerShell
Powershell.exe -executionpolicy remotesigned -File "D:\Tablety\PowerShell.ps1"

echo.
echo Konwertowanie Ekranu Startowego
copy "D:\Tablety\appsFolder.itemdata-ms" "%LocalAppData%\Microsoft\Windows\"
copy "D:\Tablety\appsFolder.itemdata-ms.bak" "%LocalAppData%\Microsoft\Windows\"

echo.
echo Zakończono konfiguracje tabletu %computername%.
pause


Second Bat (asadmin.bat)"

Code: Select all

@echo off

mode con:cols=80 lines=25
FOR /F "tokens=2 delims='='" %%A in ('wmic ComputerSystem Get username /value') do SET user=%%A
set user=%user:*\=%


echo Instalator Konfiguracji Uzytkownika Koncowego

echo.
echo Instalacja Bluetooth'a
call D:\Tablety\5130-64Bit_Network_Driver_GX371_WN_3.7.2.59_A01.EXE

echo.
echo Instalacja TeamVier'a
call "D:\Tablety\TeamViewer_Host_Setup.exe"

echo.
echo Instalacja CDM
call "D:\Tablety\CDM20814_Setup.exe"

echo.
echo Instalacja DirectAccess
call "D:\Tablety\Microsoft_DirectAccess_Connectivity_Assistant_x64.msi"
pause
echo.
echo Wyłączanie Sklepu Windows
del "C:\Users\%user%\Desktop\TeamViewer 9 Host.lnk"
del "C:\Users\%user%\Desktop\Adobe Reader X.lnk"
call "D:\Tablety\set.reg"
REG DELETE HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Taskband /F
taskkill /f /im explorer.exe
start explorer.exe
pause

echo.
echo GPUpdate
gpupdate /force

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

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

#20 Post by Squashman » 17 Dec 2014 15:24

You can get rid of the SET command I told you to use earlier and you can just use this. You can use multiple delimiters with the FOR /F command.

Code: Select all

FOR /F "tokens=3 delims==\" %%A in ('wmic ComputerSystem Get username /value') do SET user=%%A

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

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

#21 Post by Krump » 17 Dec 2014 15:57

Ok, thanks it's will halpful in the future.

But now important for me is open second bat as admin in such a manner that the first wait for second finish.

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

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

#22 Post by Squashman » 17 Dec 2014 16:12

Give this a try. Not sure if it will work or not.

Code: Select all

runas /user:domain.net\%login_usera% "Cmd /C \"D:\Tablety\asadmin.bat\""

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

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

#23 Post by Squashman » 17 Dec 2014 16:20

We tried to tackle this problem 4 years ago on the forums. It never did get resolved.
viewtopic.php?f=3&t=1181
You may have to put in some type of convoluted LOOP to check for the second batch file still running in TASKLIST. Once you don't see it running in tasklist then continue with the first batch file.

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

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

#24 Post by foxidrive » 17 Dec 2014 23:46

Try something like this:

Code: Select all

runas /user:mydomain.net\%login_usera% "start "" /w %comspec% /c \\Tablety\asadmin.bat"

The errorlevel after this line may be from runas and not the batch file.

Xboxer
Posts: 25
Joined: 27 Nov 2014 13:58

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

#25 Post by Xboxer » 18 Dec 2014 20:59

Krump wrote:Ok, thanks it's will halpful in the future.

But now important for me is open second bat as admin in such a manner that the first wait for second finish.


You can open second bat as admin (elevated permissions) with this power shell script added to your .bat
xbox

Code: Select all

(NET FILE||(powershell -command Start-Process '%0' -Verb runAs -ArgumentList '%* '&EXIT /B))>NUL 2>&1

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

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

#26 Post by Krump » 20 Dec 2014 03:41

Squashman your way don't work.

foxidrive your way don't work too.

Xboxer your way work but wrong in this situation

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

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

#27 Post by Squashman » 20 Dec 2014 12:35

Krump wrote:Squashman your way don't work.

foxidrive your way don't work too.

Xboxer your way work but wrong in this situation

Like I said in my previous post, we have previously tried to find a solution for this problem in the past. But, maybe you can elaborate more on what is wrong with Xboxer's solution. You can't just make an ambiguous statement like that without providing some kind of detailed feedback.

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

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

#28 Post by Krump » 05 Jan 2015 08:49

Ok, because it autorization, but don't want start the proces.

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

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

#29 Post by Squashman » 05 Jan 2015 14:36

What version of Windows are you running this on?
You may be able to use the WAITFOR command if you are using a newer version of Windows.

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

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

#30 Post by Krump » 29 Jan 2015 09:10

I start this bat on Win 8.1.

WAITFOR?

How I can use this command?

Post Reply