.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

#31 Post by Squashman » 29 Jan 2015 10:52

Krump wrote:I start this bat on Win 8.1.

WAITFOR?

How I can use this command?

Code: Select all

C:\BatchFiles>waitfor /?

WaitFor has two ways of working:

Syntax 1: to send a signal
    WAITFOR [/S system [/U user [/P [password]]]] /SI signal

Syntax 2: to wait for a signal
    WAITFOR [/T timeout] signal

Description:
    This tool sends, or waits for, a signal on a system. When /S is not
    specified, the signal will be broadcasted to all the systems in a
    domain. If /S is specified, then the signal will be sent only
    to the specified system.

Parameter List:
    /S     system         Specifies remote system to send signal to.

    /U     [domain\]user  Specifies the user context under which
                          the command should execute.

    /P     [password]     Specifies the password for the given user context.

    /SI                   Sends the signal across the net to waiting machines

    /T     timeout        Number of seconds to wait for signal. Valid range
                          is 1 - 99999. Default is to wait forever for signal.

    signal                The name of the signal to wait for or to send.

    /?                    Displays this help message.

    NOTE: A system can wait for multiple unique signal names.
    The signal name cannot exceed 225 characters and cannot
    contain characters other than a-z, A-Z, 0-9 and ASCII
    characters in the range 128-255.

Examples:
    WAITFOR /?
    WAITFOR SetupReady
    WAITFOR CopyDone /T 100
    WAITFOR /SI SetupReady
    WAITFOR /S system  /U user /P password /SI CopyDone

C:\BatchFiles>

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

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

#32 Post by Krump » 05 Feb 2015 02:41

Can U write some example, becouse I don't know how use it for wait on program.

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

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

#33 Post by Krump » 10 Feb 2015 01:57

Hi, for my solution I have sensational way. This code paste before your code in bat where you want use administrator permissions.

Code: Select all

@echo off
:: BatchGotAdmin (Run as Admin code starts)
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:: BatchGotAdmin (Run as Admin code ends)
:: Your codes should start from the following line


It's great and I don't have any problem with permissions like as using run as.

So I repeat the request of abot example for waitfor, because when in first bat I do:

Code: Select all

call D:\Tablety\asadmin.bat

My first .bat goes further.

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

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

#34 Post by Krump » 13 Feb 2015 04:08

I do something like that:

test1:

Code: Select all

@ECHO OFF

call test2.bat

echo before loop
:LOOP
PSLIST test2 >nul 2>&1
IF ERRORLEVEL 1 (
  GOTO CONTINUE
) ELSE (
  ECHO Batch is still running
  SLEEP 10
  GOTO LOOP
)

:CONTINUE
echo after loop
pause

test2:

Code: Select all

@echo off

%~d0
cd %~d0%~p0

:: BatchGotAdmin (Run as Admin code starts)
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"

call notepad.exe
pause

But it isint work, because don't find pslist

Post Reply