Please find attached what I have done.
It seems that once the UAC elevation given under circumstance where the
user has symlink creation right, then for any subsequent call the
symlinks are working well and going through the runas is not needed.
There is still some strange behaviour, the window title does not look
the same any longer, ie start is not working the same way for the
subsequent calls.
Vincent.
PS : Please rename the file to remove the filename extention
.remove-this-fnext. I had to do this because my SMTP server was
rejecting the email otherwise.
Le 14/08/2017 à 12:18, David Macek a écrit :
On 14. 8. 2017 12:03, Vincent Belaïche wrote:
PS : Certainly if the "runas" Shell.Application was launching
recursively the whole .cmd script instead of just the console it
would work better.
Would you like it if I investigate this way and contribute
an update of .cmd
I am personally not inclined to extend the script, but I assume it
will get approved if implemented correctly.
---
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel
antivirus Avast.
https://www.avast.com/antivirus
@echo off
setlocal
set "WD=%__CD__%"
if NOT EXIST "%WD%msys-2.0.dll" set "WD=%~dp0usr\bin\"
rem To activate windows native symlinks uncomment next line
set MSYS=winsymlinks:nativestrict
rem Set debugging program for errors
rem set
MSYS=error_start:%WD%../../mingw64/bin/qtcreator.exe^|-debug^|^<process-id^>
rem To export full current PATH from environment into MSYS2 use
'-use-full-path' parameter
rem or uncomment next line
rem set MSYS2_PATH_TYPE=inherit
rem default values
set UAC_ELEVATION=0
:checkparams
rem Help option
if "x%~1" == "x-help" (
call :printhelp "%~nx0"
exit /b %ERRORLEVEL%
)
if "x%~1" == "x--help" (
call :printhelp "%~nx0"
exit /b %ERRORLEVEL%
)
if "x%~1" == "x-?" (
call :printhelp "%~nx0"
exit /b %ERRORLEVEL%
)
if "x%~1" == "x/?" (
call :printhelp "%~nx0"
exit /b %ERRORLEVEL%
)
rem Shell types
if "x%~1" == "x-msys" shift& set MSYSTEM=MSYS& goto :checkparams
if "x%~1" == "x-msys2" shift& set MSYSTEM=MSYS& goto :checkparams
if "x%~1" == "x-mingw32" shift& set MSYSTEM=MINGW32& goto :checkparams
if "x%~1" == "x-mingw64" shift& set MSYSTEM=MINGW64& goto :checkparams
if "x%~1" == "x-mingw" shift& (if exist "%WD%..\..\mingw64" (set
MSYSTEM=MINGW64) else (set MSYSTEM=MINGW32))& goto :checkparams
rem Console types
if "x%~1" == "x-consolez" shift& set MSYSCON=console.exe& goto :checkparams
if "x%~1" == "x-mintty" shift& set MSYSCON=mintty.exe& goto :checkparams
if "x%~1" == "x-conemu" shift& set MSYSCON=conemu& goto :checkparams
if "x%~1" == "x-defterm" shift& set MSYSCON=defterm& goto :checkparams
rem Other parameters
if "x%~1" == "x-full-path" shift& set MSYS2_PATH_TYPE=inherit& goto :checkparams
if "x%~1" == "x-use-full-path" shift& set MSYS2_PATH_TYPE=inherit& goto
:checkparams
if "x%~1" == "x-here" shift& set CHERE_INVOKING=enabled_from_arguments& goto
:checkparams
if "x%~1" == "x-where" (
if "x%~2" == "x" (
echo Working directory is not specified for -where parameter. 1>&2
exit /b 2
)
cd /d "%~2" || (
echo Cannot set specified working diretory "%~2". 1>&2
exit /b 2
)
set CHERE_INVOKING=enabled_from_arguments
)& shift& shift& goto :checkparams
if {%~1}=={-uac-elevation} shift & set UAC_ELEVATION=1& goto checkparams
rem check whether or not we need UAC elevalation
if {%MSYS%}=={winsymlinks:nativestrict} (rem
) else (goto uac_elevation_not_needed)
call :check_symlinks OK
if %OK% neq 0 goto uac_elevation_not_needed
if %UAC_ELEVATION% equ 1 goto uac_elevation_can_t_make_it
rem Here the user wants native symlinks, but they do not work. We
rem assume that this is because the user does not have UAC
rem elevation. This is a possible cause, but not the only one, the
rem user account may also be configured not to allow symlink creation.
rem We are going to re-lauch this script with UAC elevation by means
rem of a VBScript.
set UAC_ELVT_VERSION=1.1
endlocal
set ELEVATE_APP=%~dpnx0
set ELEVATE_ARGUMENTS=-uac-elevation %*
setlocal
del /Q "%WD%msys-elevate.vbs"
if NOT exist "%WD%msys-elevate.vbs" goto make_elevate
for /F %%i in ('cscript //nologo "%WD%msys-elevate.vbs" --version') do set
elevate_version=%%i
if {%elevate_version%}=={%UAC_ELVT_VERSION%} goto do_elevate_start
:make_elevate
del /Q "%WD%msys-elevate.vbs"
echo Set oShell = CreateObject("Shell.Application")>>"%WD%msys-elevate.vbs"
echo Set oWshShell =
WScript.CreateObject("WScript.Shell")>>"%WD%msys-elevate.vbs"
echo Set oWshProcessEnv =
oWshShell.Environment("PROCESS")>>"%WD%msys-elevate.vbs"
echo.>>"%WD%msys-elevate.vbs"
echo ' Get raw command line agruments from msys2_shell.cmd
passed>>"%WD%msys-elevate.vbs"
echo ' in through an environment variable.>>"%WD%msys-elevate.vbs"
echo sApp = oWshProcessEnv("ELEVATE_APP")>>"%WD%msys-elevate.vbs"
echo sArguments = oWshProcessEnv("ELEVATE_ARGUMENTS")>>"%WD%msys-elevate.vbs"
echo.>>"%WD%msys-elevate.vbs"
echo Select Case WScript.Arguments.Count>>"%WD%msys-elevate.vbs"
echo Case 0 >>"%WD%msys-elevate.vbs"
rem The trick is to start sApp with the Shell.Application ShellExecute
rem method using the "runas" operation.
rem The "runas" operation has the effect of elevating the UAC.
echo oShell.ShellExecute sApp, sArguments, "%WD%", "runas", 1
>>"%WD%msys-elevate.vbs"
echo Case 1 >>"%WD%msys-elevate.vbs"
echo sFlag = WScript.Arguments(0)>>"%WD%msys-elevate.vbs"
echo If (sFlag = "--version") Then>>"%WD%msys-elevate.vbs"
echo
WScript.StdOut.WriteLine("%UAC_ELVT_VERSION%")>>"%WD%msys-elevate.vbs"
echo WScript.Quit>>"%WD%msys-elevate.vbs"
echo Else>>"%WD%msys-elevate.vbs"
echo WScript.Quit 1 >>"%WD%msys-elevate.vbs"
echo End If>>"%WD%msys-elevate.vbs"
echo Case Else>>"%WD%msys-elevate.vbs"
echo WScript.Quit 2 >>"%WD%msys-elevate.vbs"
echo End Select>>"%WD%msys-elevate.vbs"
:do_elevate_start
cscript //nologo "%WD%msys-elevate.vbs"
exit /B %ERRORLEVEL%
:uac_elevation_can_t_make_it
rem Here we have already done UAC elevation. However, native symlink
rem still don't work. This means that the user priviledge are not
rem configured correctly.
echo Your user account priviledge do not allow you to create native
echo MSW symlinks. Please see
echo https://github.com/git-for-windows/git/wiki/Symbolic-Links about
echo ``Allowing non-administrators to create symbolic links''
pause
exit /B 3
:uac_elevation_not_needed
rem Setup proper title
if "%MSYSTEM%" == "MINGW32" (
set "CONTITLE=MinGW x32"
) else if "%MSYSTEM%" == "MINGW64" (
set "CONTITLE=MinGW x64"
) else (
set "CONTITLE=MSYS2 MSYS"
)
if "x%MSYSCON%" == "xmintty.exe" goto startmintty
if "x%MSYSCON%" == "xconsole.exe" goto startconsolez
if "x%MSYSCON%" == "xconemu" goto startconemu
if "x%MSYSCON%" == "xdefterm" goto startsh
if NOT EXIST "%WD%mintty.exe" goto startsh
set MSYSCON=mintty.exe
:startmintty
start "%CONTITLE%" "%WD%mintty" -i /msys2.ico -e /usr/bin/bash --login %1 %2 %3
%4 %5 %6 %7 %8 %9
exit /b %ERRORLEVEL%
:startconsolez
cd %WD%..\lib\ConsoleZ
start "%CONTITLE%" console -t "%CONTITLE%" -r %1 %2 %3 %4 %5 %6 %7 %8 %9
exit /b %ERRORLEVEL%
:startconemu
call :conemudetect || (
echo ConEmu not found. Exiting. 1>&2
exit /b 1
)
start "%CONTITLE%" "%ComEmuCommand%" /Here /Icon "%WD%..\..\msys2.ico" /cmd
"%WD%bash" --login %1 %2 %3 %4 %5 %6 %7 %8 %9
exit /b %ERRORLEVEL%
:startsh
set MSYSCON=
start "%CONTITLE%" "%WD%bash" --login %1 %2 %3 %4 %5 %6 %7 %8 %9
exit /b %ERRORLEVEL%
:exit
exit /b 0
:conemudetect
set ComEmuCommand=
if defined ConEmuDir (
if exist "%ConEmuDir%\ConEmu64.exe" (
set "ComEmuCommand=%ConEmuDir%\ConEmu64.exe"
set MSYSCON=conemu64.exe
) else if exist "%ConEmuDir%\ConEmu.exe" (
set "ComEmuCommand=%ConEmuDir%\ConEmu.exe"
set MSYSCON=conemu.exe
)
)
if not defined ComEmuCommand (
ConEmu64.exe /Exit 2>nul && (
set ComEmuCommand=ConEmu64.exe
set MSYSCON=conemu64.exe
) || (
ConEmu.exe /Exit 2>nul && (
set ComEmuCommand=ConEmu.exe
set MSYSCON=conemu.exe
)
)
)
if not defined ComEmuCommand (
FOR /F "tokens=*" %%A IN ('reg.exe QUERY
"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\ConEmu64.exe" /ve
2^>nul ^| find "REG_SZ"') DO (
set "ComEmuCommand=%%A"
)
if defined ComEmuCommand (
call set "ComEmuCommand=%%ComEmuCommand:*REG_SZ =%%"
set MSYSCON=conemu64.exe
) else (
FOR /F "tokens=*" %%A IN ('reg.exe QUERY
"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\ConEmu.exe" /ve
2^>nul ^| find "REG_SZ"') DO (
set "ComEmuCommand=%%A"
)
if defined ComEmuCommand (
call set "ComEmuCommand=%%ComEmuCommand:*REG_SZ =%%"
set MSYSCON=conemu.exe
)
)
)
if not defined ComEmuCommand exit /b 2
exit /b 0
:printhelp
echo Usage:
echo %~1 [options] [bash parameters]
echo.
echo Options:
echo -mingw32 ^| -mingw64 ^| -msys[2] Set shell type
echo -defterm ^| -mintty ^| -conemu ^| -consolez
echo Set terminal type
echo -here Use current directory as working
echo directory
echo -where DIRECTORY Use specified DIRECTORY as working
echo directory
echo -[use-]full-path Use full current PATH variable
echo instead of triming to minimal
echo -help ^| --help ^| -? ^| /? Display this help and exit
echo.
echo Any parameter that cannot be treated as valid option and all
echo following parameters are passed as bash command parameters.
echo.
exit /b 0
rem check the native symlinks are working
rem after execution we have in variabled named %1:
rem 1 when native symlinks are working
rem 0 otherwise
:check_symlinks
call :mktemp fic1 .\msys2_fic1 .tmp rem
echo Some 1st content>"%fic1%"
call :mktemp fic2 .\msys2_fic2 .tmp rem
set LN_CMD=%WD%ln.exe
"%WD%bash" -c "'%LN_CMD:\=/%' -s '%fic1:\=/%' '%fic2:.\=%'"
echo Some 2nd content>>"%fic1%"
fc "%fic1%" "%fic2%" > NUL
if ERRORLEVEL 1 (set "%1=0") else set "%1=1"
del /q "%fic1%" "%fic2%"
goto :EOF
rem create a temporary file
rem %1 : var name to which output result (w/o double quotes)
rem %2 : file name prefix
rem %3 : file name suffix
rem %4 : optional, set to rem for not creating an empty file.
:mktemp
rem if the prefix contains \ then we don't create the file under %TEMP%
rem but under the specified directory
for /F "tokens=1,2 delims=\\" %%i in ("%~2") do (
if {%%j}=={} (set "mktemp_prefix=%TEMP%\%~2") else set "mktemp_prefix=%~2")
:mktemp_again
set "mktemp_filename=%mktemp_prefix%%RANDOM%%~3"
if EXIST "%mktemp_filename%" goto :mktemp_again
set "%1=%mktemp_filename%"
%4 type NUL > "%mktemp_filename%"
goto :EOF
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Msys2-users mailing list
Msys2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/msys2-users