Run User Program As Administrator


  1. Posts : 207
    W10 Pro v22H2 64-bit
       #1

    Run User Program As Administrator


    I want to create a small program (bat, exe, PowerShell, whatever) to delete all Event logs. I want to put this on the C drive (not a shortcut on the desktop), double-click it, and it runs. I don't want to have to right-click and choose Run as administrator. Approving the UAC prompt is OK. I'm not a programmer, so I have to go with what I see on the Internet.

    I have a shortcut on the desktop (again, not what I want), but it does exactly what I want. The code is: C:\Windows\System32\cmd.exe /k for /F "tokens=*" %1 in ('wevtutil.exe el') DO wevtutil.exe cl "%1".

    I also have a bat file created by Brink that I put on the C drive (where I want it) that requires me to click on Run as administrator (again, not what I want). The code is: for /F "tokens=*" %%a in ('wevtutil.exe el') DO wevtutil.exe cl "%%a". Plus, I put a PAUSE in there at the end.

    I've tried various methods I've seen to try to get something to run automatically as administrator, but nothing has worked. I also tried IExpress, but that didn't work.

    Why is this so complicated, and why does something have to be a shortcut to have the Run as administrator checkbox?

    Can anybody make this happen? Is it even possible? Thanks.

    (And please, no lectures about the advisability of deleting the Event logs. Thanks.)
      My Computer


  2. Posts : 5,478
    2004
       #2

    You can use the code from here to auto-elevate your batch file. It will check if you are running as administrator and if not relauch (you'll get the UAC prompt).

    windows - How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required? - Stack Overflow

    Just put what your want after ::START. I put for /f %%a in ('WEVTUTIL EL') do WEVTUTIL CL "%%a" to clear event logs but you can put what you want. Save it as "ClearLogs.bat" or whatever name you like with .bat on the end.

    Code:
    ::::::::::::::::::::::::::::::::::::::::::::
    :: Elevate.cmd - Version 4
    :: Automatically check & get admin rights
    ::::::::::::::::::::::::::::::::::::::::::::
     @echo off
     CLS
     ECHO.
     ECHO =============================
     ECHO Running Admin shell
     ECHO =============================
    
    :init
     setlocal DisableDelayedExpansion
     set cmdInvoke=1
     set winSysFolder=System32
     set "batchPath=%~0"
     for %%k in (%0) do set batchName=%%~nk
     set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
     setlocal EnableDelayedExpansion
    
    :checkPrivileges
      NET FILE 1>NUL 2>NUL
      if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
    
    :getPrivileges
      if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
      ECHO.
      ECHO **************************************
      ECHO Invoking UAC for Privilege Escalation
      ECHO **************************************
    
      ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
      ECHO args = "ELEV " >> "%vbsGetPrivileges%"
      ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
      ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"
      ECHO Next >> "%vbsGetPrivileges%"
    
      if '%cmdInvoke%'=='1' goto InvokeCmd 
    
      ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
      goto ExecElevation
    
    :InvokeCmd
      ECHO args = "/c """ + "!batchPath!" + """ " + args >> "%vbsGetPrivileges%"
      ECHO UAC.ShellExecute "%SystemRoot%\%winSysFolder%\cmd.exe", args, "", "runas", 1 >> "%vbsGetPrivileges%"
    
    :ExecElevation
     "%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
     exit /B
    
    :gotPrivileges
     setlocal & cd /d %~dp0
     if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)
    
     ::::::::::::::::::::::::::::
     ::START
     ::::::::::::::::::::::::::::
    for /f %%a in ('WEVTUTIL EL') do WEVTUTIL CL "%%a"
    pause
      My Computer


  3. Posts : 207
    W10 Pro v22H2 64-bit
    Thread Starter
       #3

    Ix07, thanks for your reply.

    Four things:

    1. Since I already know I'm not running as administrator (even though my user ID is set as administrator--M$ sucks), can we simplify your code by removing whatever lines constitute the check?

    2. I went to the link you gave. There are a lot of comments/answers following the basic question at least implying things can be done more simply. Can any of that stuff be used to slim down the code you provided?

    3. Isn't there just a one-line command of some sort, like 'runas', that can say to run the code as administrator?

    4. As far as I'm concerned, this doesn't have to be a .bat file. Can something else like PowerShell, .exe, whatever be used to produce a simpler solution?

    Thanks again for your response.
      My Computer


  4. Posts : 317
    Microsoft Windows 10 x64
       #4

    Code:
    %SystemRoot%\System32\runas.exe /user:YOUR-PC-NAME\Administrator /savecred "%SystemRoot%\System32\cmd.exe"
    %SystemRoot%\System32\runas.exe /user:YOUR-PC-NAME\Administrator /savecred %SystemRoot%\regedit.exe
    %SystemRoot%\System32\runas.exe /user:YOUR-PC-NAME\Administrator /savecred %SystemRoot%\System32\CompMgmtLauncher.exe


    i have created many shortcuts as above to run any program as Administrator
    Administrator password is only asked one time

    Create a shorcut pointing your batch file and that's it
    may be this will help you
      My Computer


  5. Posts : 93
    Windows 10
       #5

    Don’t use the code that’s going around as demonstrated in lx07’s post. It’s unfortunately very poorly programmed.

    I have a couple of my own solutions to this, including one-liners, in the thread How to run Batch File as Admin?

    See post #21 for one-line solutions.
    See post #22 for a stable solution.
    See post #26 for a bypass UAC solution.

    @vanp If you need assistance implementing any of theses, just give the word.
      My Computer


  6. Posts : 207
    W10 Pro v22H2 64-bit
    Thread Starter
       #6

    D4ni3l, thanks for your help, but again, I already have a shortcut, and that's not how I want to implement this task.

    Pyprohly, you appear to be the MAN. All I ever wanted was the simplest (hopefully, one-line) solution, and you seem to have it.

    First, I tried the 2nd PowerShell line from post 21. It displayed the black Command Prompt screen for a few seconds, and I thought it was hung there. Then it displayed the UAC prompt, in focus, and I chose Yes. Then it started running the command to delete the logs. That worked.

    Then I tried the VBScript line from post 21. It displayed the black Command Prompt screen but for a much shorter time. Then everything progressed as above.

    Then I went back to the 2nd PowerShell line again. It displayed the black Command Prompt screen but for a much shorter time than at first. Then everything worked again as it should.

    As I stated in my OP, dealing with the UAC prompt is acceptable. I guess ideally it wouldn't be there, but I don't want to add a lot of extra code to suppress it. Also, who knows, in the future M$ might do something that would cause it not to work, or worse.

    I'll experiment with these two lines of code. If they seem about equal I guess I'll settle on the shortest line.

    So, unless somebody comes up with an alternate solution that is somehow better than one line of code, I'll consider this the solution. When I make a final decision, I'll mark the thread as solved.

    Thanks to everybody who responded, including Brink, who apparently deleted his (the first) response and my reply.
      My Computer


  7. Posts : 5,478
    2004
       #7

    Pyprohly said:
    I have a couple of my own solutions to this, including one-liners...
    Those are really quite clever especially the event viewer one.

    Proves you should not run as a user that is part of Administrators group. I thought before it didn't matter if you ran as Standard or Admin user as UAC would take care of it. I was wrong apparently.

    I'd hope MS close down that loophole though to be honest.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 10:51.
Find Us




Windows 10 Forums