IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Scripts/Batch Discussion :

Optimisation Script RunAs Admin


Sujet :

Scripts/Batch

  1. #1
    Nouveau candidat au Club
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Février 2013
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Technicien Help Desk
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2013
    Messages : 2
    Par défaut Optimisation Script RunAs Admin
    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

  2. #2
    Nouveau candidat au Club
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Février 2013
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Technicien Help Desk
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2013
    Messages : 2
    Par défaut
    Une petite modification du script ntbackup_job_systemstate.bat avec un petit rapport d'erreur du gestionnaire d’événements et d’envoi du fichier .log, ca ouvre des perspectives, lol

    Le fichier ntbackup_job_systemstate\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
    @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
    
    @CD %~sf0\..
    
    @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 begintime=%date:~6,6%%date:~3,2%%date:~0,2%%time:~0,2%%time:~3,2%%time:~6,2%.000000+060
    @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
    
    @cscript report_backup.vbs %servername% %begintime% //nologo

    plus le complément de script ntbackup_job_systemstate\report_backup.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
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    ' DEBUT DE SCRIPT
    '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    On Error Resume Next
    
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    ' DEFINITION DES VARIABLES
    '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    ' COMPTER LES ERREURS NTBACKUP DANS L'OBSERVATEUR D'EVENEMENT DEPUIS LA DERNIERE SAUVEGARDE
    '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Set colResult = objWMIService.ExecQuery("Select Type from Win32_NTLogEvent WHERE EventType<3 AND Logfile='Application' AND SourceName='NTBackup' AND TimeGenerated > '" & WScript.Arguments(1) & "'")
    
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    ' SI ERREUR DE SAUVEGARDE
    '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    If colResult.Count>0 Then
    
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    ' RETROUVER FICHIER LOG
    '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Const Path = "C:\Documents and Settings\operateur\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\"
    
    Dim fso, Dossiers, fic, fichiers, strListe, f, r 
    Dim Valeur, imax, z, Cible, liste, fichierlog, Message
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set Dossiers = fso.GetFolder(Path)
    Set fic = Dossiers.Files
    
        imax = 0
        For Each fichiers In fic
            Set f = fso.GetFile(fichiers)
            imax = imax + 1
            ReDim Preserve Tableau(2, imax)
            Tableau(1, imax) = f.Name
            Tableau(2, imax) = f.DateLastModified
            
            Valeur = 0
            For imax = 1 To imax - 1
                If CDate(Tableau(2, imax)) < CDate(Tableau(2, imax + 1)) Then
                   For z = 1 To 2
                       Cible = Tableau(z, imax)
                       Tableau(z, imax) = Tableau(z, imax + 1)
                       Tableau(z, imax + 1) = Cible
                   Next
                   Valeur = 1
                End If
            Next
        Next
    
        liste = ""
        liste = Tableau(1, 1)
        
        Set fso      = Nothing          
        Set Dossiers = Nothing
        Set fic      = Nothing
        Set f        = Nothing
    
        fichierlog = ""
        fichierlog = Path&liste
    
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    ' RECHERCHE DES ERREURS NTBACKUP DANS L'OBSERVATEUR D'EVENEMENT
    '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Message = ""
    Message = "Erreur de sauvegarde sur serveur " & WScript.Arguments(0) & VbCrLf
    
    Set colItems = objWMIService.ExecQuery("Select * from Win32_NTLogEvent WHERE EventType<3 AND Logfile='Application' AND SourceName='NTBackup' AND TimeGenerated > '" & WScript.Arguments(1) & "'",,48)
    For Each objItem in colItems
        Message = Message & "Type: " & objItem.Type
        Message = Message & VbCrLf
        Message = Message & "EventCode: " & objItem.EventCode
        Message = Message & VbCrLf
        Message = Message & "Message: " & objItem.Message
        Message = Message & VbCrLf
    Next
    
    Wscript.Echo Message
    
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    ' ENVOYER EMAIL ERREUR
    '
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Dim oCDO
     
    Set oCDO = CreateObject("CDO.Message")
    With oCdo
        .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
        .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.free.fr"
        .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25
        .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = "1"
        .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "monmail@free.fr"
        .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
        .Configuration.Fields.Update
        .Subject = "Erreur de sauvegarde sur " & WScript.Arguments(0)
        .From = "monmail@free.fr"
        .To = "monmail@free.fr"
        .TextBody = Message
        .AddAttachment fichierlog
        .Send
    End With
    
    
    
    End If
    Je sors un peu du script runasadmin mais ça fournit un script plus complet...

  3. #3
    Invité de passage
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Février 2014
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux

    Informations forums :
    Inscription : Février 2014
    Messages : 1
    Par défaut
    Salut,

    merci pour ton astuce j'essai de la mettre en place mais j'ai une erreur sur le fichier = adm_runas.vbs il me dit =
    Indice en dehors de la plage
    Ligne 47
    Caractère 1

    Je comprend pas le problème!!!

    Merci de votre aide.

    Aegirson

Discussions similaires

  1. Optimisation script de redimension d'images
    Par jazono dans le forum Langage
    Réponses: 5
    Dernier message: 19/03/2010, 09h42
  2. Optimisation script de validation FTP
    Par Leeloo_Multiboot dans le forum Shell et commandes GNU
    Réponses: 3
    Dernier message: 18/12/2008, 17h07
  3. [MySQL] Optimiser script ; jointures
    Par Guigui13 dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 22/10/2008, 20h28
  4. Optimisation script pour réordonner des N° de Lots
    Par polemoss dans le forum MySQL
    Réponses: 1
    Dernier message: 06/06/2007, 18h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo