Computer Hope

Microsoft => Microsoft DOS => Topic started by: Abo-Zead on May 02, 2021, 01:29:38 AM

Title: Administrator privilege run code
Post by: Abo-Zead 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?
Title: Re: Administrator privilege run code
Post by: Abo-Zead on May 13, 2021, 02:30:08 PM
Is there any one can solve this issue?
Title: Re: Administrator privilege run code
Post by: Squashman 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"
Title: Re: Administrator privilege run code
Post by: Abo-Zead 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.
Title: Re: Administrator privilege run code
Post by: Abo-Zead 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)
Title: Re: Administrator privilege run code
Post by: Abo-Zead 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?