Comment puis-je auto-elever mon fichier batch pour qu'il demande les droits administrateur UAC si necessaire ?

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Comment puis-je auto-elever mon fichier batch pour qu'il demande les droits administrateur UAC si necessaire ?

Message par ForumBot »

Comment puis-je auto-elever mon fichier batch pour qu’il demande les droits administrateur UAC si necessaire ?

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Comment puis-je auto-elever mon fichier batch pour qu'il demande les droits administrateur UAC si necessaire ?

Message par ayi »

Il existe un moyen simple sans avoir besoin d’utiliser un outil externe - cela fonctionne parfaitement avec Windows 7, 8, 8.1, 10 et 11 et est egalement retrocompatible (Windows XP n’a pas d’UAC, donc l’elevation n’est pas necessaire).


Consultez ce code (je me suis inspire du code de NIronwolf publie dans le fil Batch File - “Access Denied” On Windows 7? 1), mais je l’ai ameliore - dans ma version, aucun repertoire n’est cree ni supprime pour verifier les privileges administrateur) :


@echo off
REM :::::::::::::::::::::::::::::::::::::::::
REM Elevate.cmd - Version 9
REM Automatically check & get admin rights
REM see "https://stackoverflow.com/a/12264592/1016343" for description
REM :::::::::::::::::::::::::::::::::::::::::

CLS
ECHO/
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:init
setlocal EnableExtensions DisableDelayedExpansion
set cmdInvoke=1
set winSysFolder=System32
set "batchPath=%~f0"
rem this works also from cmd shell, other than %~0
for %%k in (%0) do set batchName=%%~nk
set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
setlocal EnableDelayedExpansion

:checkPrivileges
%SystemRoot%\System32\whoami.exe /groups /nh | %SystemRoot%\System32\find.exe "S-1-16-12288" 1>nul
if errorlevel 1 goto getPrivileges

:checkPrivileges2
%SystemRoot%\System32\net.exe session 1>nul 2>NUL
if not errorlevel 1 goto gotPrivileges

: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!", ar

*(Reponse tronquee)*
Répondre

Revenir à « Windows 11 »