I have the following batch file which creates a network share using cmd as admin. When run from any directory on the c: drive it opens 2 command prompt windows and waits for the user to press a key. When run from any other drive however, it quickly flashes 2 command prompt windows, dissapears, and does not run the command. How can I make it work from any drive?
@echo off
:: BatchGotAdmin
::-------------------------------------
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"
set params = %*:"="
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
::--------------------------------------
::ENTER YOUR CODE BELOW:
net share sharename=C:\tada /grant:everyone,FULL
echo ...
echo ...
echo PLEASE CHECK ABOVE IF SHARE WAS SUCCESFUL. YOU MAY NOW CLOSE THE WINDOW(S)
echo ...
echo ...
pause
Update:
Running it from any subdirectory in C:
works. Running it from G:
works. Running it from G:\Users\myname\Documents\couple_more_subfolders
does not work
Answer
I found another script that does not have this problem. Turns out the first one fails if there is a space in the filepath where it is located (so really, it had nothing to do with the drive letter):
@echo off
if _%1_==_payload_ goto :payload
:getadmin
echo %~nx0: elevating self
set vbs=%temp%\getadmin.vbs
echo Set UAC = CreateObject^("Shell.Application"^) >> "%vbs%"
echo UAC.ShellExecute "%~s0", "payload %~sdp0 %*", "", "runas", 1 >> "%vbs%"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
goto :eof
:payload
::echo %~nx0: running payload with parameters:
::echo %*
echo ---------------------------------------------------
cd /d %2
shift
shift
net share sharename=C:\tada /grant:everyone,FULL
echo ...
echo ...
echo PLEASE CHECK ABOVE IF SHARE WAS SUCCESFUL. YOU MAY NOW CLOSE THE WINDOW(S)
echo ...
echo ...
pause
goto :eof
No comments:
Post a Comment