Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Administrator privilege run code  (Read 40988 times)

0 Members and 1 Guest are viewing this topic.

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Administrator privilege run code
« on: May 02, 2021, 01:29:38 AM »
Hi all,

I found code that can run any batch file as Administrator and after insert this code into my bat file works properly but sometimes show this message "The system cannot find the drive specified.
                                                   Press any key to continue . . ."

but still my code works fine after pressing any key to continue

Here is Administrator code
Code: [Select]
:init
setlocal DisableDelayedExpansion
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 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%"
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
"%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %*
exit /B

:gotPrivileges
setlocal & pushd .
cd /d %~dp0
if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)

How can I ignore this Error message and go on?

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Re: Administrator privilege run code
« Reply #1 on: May 13, 2021, 02:30:08 PM »
Is there any one can solve this issue?

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Administrator privilege run code
« Reply #2 on: May 13, 2021, 04:25:44 PM »
First two steps in troubleshooting a batch file.
1) Don't use ECHO OFF
2) Run the batch file from the command prompt instead of with your mouse.

By using both of those techniques you can see what line of code is causing the error message. And looking back you have been told this before in your previous threads.

As far as your code goes you should not use an Apostrophe to surround IF comparisons.  You should use double quotes as that protects against spaces and other special characters that may cause failures in the comparison.
Code: [Select]
IF "%var%"=="string"

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Re: Administrator privilege run code
« Reply #3 on: May 16, 2021, 10:50:52 PM »
Thanks Squashman for your reply
And I'll try to do your suggestion
Then feed back to you.

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Re: Administrator privilege run code
« Reply #4 on: May 24, 2021, 09:03:10 AM »
Dear Squashman, I tried to do what I understand from your suggestion but the problem still exists "the same error message"
and here is the last code after replaced any single quote with double quotes and also I have tried to remove @echo off and run it under CMD but I can't understand it where is the error?

Code: [Select]
:init
setlocal DisableDelayedExpansion
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 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%"
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
"%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %*
exit /B

:gotPrivileges
setlocal & pushd .
cd /d %~dp0
if "%1"=="ELEV" (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Re: Administrator privilege run code
« Reply #5 on: September 23, 2021, 09:07:29 PM »
Hi all,

Any reply to my question?
I still have the same issue, and get this message "The system cannot find the drive specified"

Please help?