Bonjour a tous, j'aimerais partager avec vous un petit script qui m'est bien utile pour élever les droits automatiquement en mode admin avant de lancer un batch. Ce script fonctionne bien jusque la mais n’étant pas un grand programmeur j'aimerais vos avis sur ce qui peut être amélioré.
Le script se présente suivant cet arborescence :
.\batchs\var_admin\logger.txt
.\batchs\var_admin\adm_runas.vbs
.\batchs\ntbackup_job_systemstate\ntbackup_job_systemstate.bat
- Le fichier logger.txt contient les codes d’accès administrateur tel que DOMAIN,USER,PASSWORD
- Le fichier adm_runas.vbs contient un script reserve a relancer le batch avec les droits administrateur.
- Le fichier ntbackup_job_systemstate.bat est un exemple de batch pour voir comment passer les arguments.
Fichier logger.txt:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 DOMAIN=WORKGROUP USER=administrator PASSWORD=password
Fichier adm_runas.vbs:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' DEFINITION DES VARIABLES ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' dim fso, WshShell, WshEnv, WinPath, CRLF, oFich, tx, tb, domain, user, password, sCmd1 set fso = CreateObject("Scripting.FileSystemObject") set WshShell = CreateObject("WScript.Shell") set WshEnv = WshShell.Environment("Process") WinPath = WshEnv("SystemRoot")&"\System32\runas.exe" CLRF = chr(10) & chr(13) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' RECCUPERER LES IDENTIFIANTS ADMIN DEPUIS LE FICHIER LOGGER.TXT ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' set oFich = fso.OpenTextFile("../var_admin/logger.txt",1,True) tx = oFich.ReadAll tb = Split(tx,VbNewline) domain = replace(tb(0),"DOMAIN=","") user = replace(tb(1),"USER=","") password = replace(tb(2),"PASSWORD=","")&CHR(13) oFich.Close ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' RELANCER LE BATCH EN MODE ADMIN ET PASSER UN ARGUMENT AU BATCH POUR L'AUTORISER ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' sCmd1 = WScript.Arguments(0)&" modeAdmin" rc = WshShell.Run("runas /user:" & user & " " & CHR(34) & sCmd1 & CHR(34), 2, FALSE) Wscript.Sleep 90 WshShell.AppActivate(WinPath) WshShell.SendKeys password & CRLF WScript.Sleep 300 Set fso = Nothing Set WshShell = Nothing Set WshEnv = Nothing Set WinPath = Nothing Set CRLF = Nothing Set oFich = Nothing Set tx = Nothing Set tb = Nothing Set domain = Nothing Set user = Nothing Set password = Nothing Set sCmd1 = Nothing wscript.quit ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ' FIN ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Et le fichier ntbackup_job_systemstate.bat :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 @ECHO OFF @IF "%1"=="modeAdmin" GOTO code ELSE GOTO setadmin REM .................................................................................................................. REM . REM . Si argument modeAdmin absent je lance le vbs pour elevation et je lui passe le chemin du batch pour le relancer REM . REM .................................................................................................................. :setadmin @SET runbatch=%~sf0 @cscript ../var_admin/adm_runas.vbs %runbatch% //nologo @EXIT REM .................................................................................................................. REM . REM . Si argument modeAdmin present droits admin ok je lance mon code REM . REM .................................................................................................................. :code @FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B @FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B @FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B @FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B @SET fulldate=%date:~6,6%-%date:~3,2%-%date:~0,2% @SET "servername=SERVER001" @IF NOT EXIST D:\Backup MD D:\Backup @IF NOT EXIST D:\Backup\%date:~6,6%-%date:~3,2%_Backup_%servername% MD D:\Backup\%date:~6,6%-%date:~3,2%_Backup_%servername% NTBACKUP backup systemstate /m normal /f D:\Backup\%date:~6,6%-%date:~3,2%_Backup_%servername%\%fulldate%_%servername%_SystemState_Backup.bkf /V:yes /L:f
Je pensais a voir une methode pour crypter les fichiers logger.txt et les stocker sur des supports amovible peut etre un peu plus secure![]()
Partager