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

VBScript Discussion :

Tutoriel Vbscript et Excel


Sujet :

VBScript

  1. #1
    Expert éminent
    Avatar de hackoofr
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2009
    Messages
    3 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Juin 2009
    Messages : 3 839
    Points : 9 222
    Points
    9 222
    Par défaut Tutoriel Vbscript et Excel

    Qui a un tutoriel sur Vbscript et Excel ? je cherche à faire un vbscript qui pilote Excel pour écrire tous les processus en cours d'exécution dans une feuille1, la liste des services dans une feuille2 et les éléments à démarrage automatique dans feuille3 ?

  2. #2
    Modérateur
    Avatar de l_autodidacte
    Homme Profil pro
    Retraité : Directeur de lycée/Professeur de sciences physiques
    Inscrit en
    Juillet 2009
    Messages
    2 415
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Retraité : Directeur de lycée/Professeur de sciences physiques
    Secteur : Enseignement

    Informations forums :
    Inscription : Juillet 2009
    Messages : 2 415
    Points : 5 806
    Points
    5 806
    Par défaut
    Avec VbScript, je n'en connais pas mais avec VB6, il y a ce TUTO.

    Les instructions dans VB6 n'étant pas très différentes de celles de VbScript, tu dois pouvoir t'en tirer facilement.
    En VbScript, à la différence de VB6, les variables ne sont pas typées et je présume que c'est connu pour toi.
    Une seule chose à considérer : les objets doivent être créés par le code, pas comme en VB6 où on utilise des contrôles dont on place une instance sur un objet Form.

    Bon courage
    Ne pas oublier le tag si satisfait.
    Voter pour toute réponse satisfaisante avec pour encourager les intervenants.
    Balises CODE indispensables. Regardez ICI
    Toujours utiliser la clause Option Explicit(VBx, VBS ou VBA) et Ne jamais typer variables et/ou fonctions en VBS.
    Vous pouvez consulter mes contributions
    Ne pas oublier de consulter les différentes FAQs et les Cours/Tutoriels VB6/VBScript
    Ne pas oublier L'Aide VBScript et MSDN VB6 Fr

  3. #3
    Expert éminent
    Avatar de hackoofr
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2009
    Messages
    3 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Juin 2009
    Messages : 3 839
    Points : 9 222
    Points
    9 222
    Par défaut
    Citation Envoyé par l_autodidacte Voir le message
    Avec VbScript, je n'en connais pas mais avec VB6, il y a ce TUTO.
    Les instructions dans VB6 n'étant pas très différentes de celles de VbScript, tu dois pouvoir t'en tirer facilement.
    En VbScript, à la différence de VB6, les variables ne sont pas typées et je présume que c'est connu pour toi.
    Une seule chose à considérer : les objets doivent être créés par le code, pas comme en VB6 où on utilise des contrôles dont on place une instance sur un objet Form.
    Bon courage
    pour ce lien
    Pour le moment j'ai ceci séparément :

    ListSevices.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
    ' Create a new and blank spreadsheet:
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = True
    objExcel.Workbooks.Add
    ' Format the cell A1 and add the text: Service
    objExcel.Cells(1, 1).Value = "Nom du Service"
    objExcel.Cells(1, 1).Font.Bold = TRUE
    objExcel.Cells(1, 1).Interior.ColorIndex = 43
    objExcel.Cells(1, 1).Font.ColorIndex = 2
    ' Format the cell A2 and add the text: Status
    objExcel.Cells(1, 2).Value = "Etat du service"
    objExcel.Cells(1, 2).Font.Bold = TRUE
    objExcel.Cells(1, 2).Interior.ColorIndex = 43
    objExcel.Cells(1, 2).Font.ColorIndex = 2
    '*************************************************
    ' Format the cell A3 and add the text: Chemin Executable
    objExcel.Cells(1, 3).Value = "Chemin Executable"
    objExcel.Cells(1, 3).Font.Bold = TRUE
    objExcel.Cells(1, 3).Interior.ColorIndex = 43
    objExcel.Cells(1, 3).Font.ColorIndex = 2
    '*************************************************
    ' Format the cell A4 and add the text: Description
    objExcel.Cells(1, 4).Value = "Description du service"
    objExcel.Cells(1, 4).Font.Bold = TRUE
    objExcel.Cells(1, 4).Interior.ColorIndex = 43
    objExcel.Cells(1, 4).Font.ColorIndex = 2
    ' Find the Windows services on this computer
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colServices = objWMIService.ExecQuery("Select * From Win32_Service")
    ' Write each service to Excel, starting in A2
    x = 1
    For Each objService in colServices
        x = x + 1
        objExcel.Cells(x, 1) = objService.Name
        objExcel.Cells(x, 2) = objService.State
        objExcel.Cells(x, 3) = objService.PathName
        objExcel.Cells(x, 4) = objService.Description
        Etat = objService.Started
        If Etat Then 
        Cellule x,2,"Démarré" 
            ELSE
        Cellule X,2,"Arrêté"
            objExcel.Cells(x, 2).Font.ColorIndex = 3
            objExcel.Cells(x, 3).Font.ColorIndex = 3
            objExcel.Cells(x, 4).Font.ColorIndex = 3
        end if
    Next
    ' Autofit the first column to fit the longest service name
    objExcel.Columns("A:A").EntireColumn.AutoFit
    objExcel.Columns("B:B").EntireColumn.AutoFit
    objExcel.Columns("C:C").EntireColumn.AutoFit
    objExcel.Columns("D:D").EntireColumn.AutoFit
    Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Network = WScript.CreateObject("WScript.Network")
    Computer = Network.ComputerName
    objExcel.ActiveWorkbook.SaveAs fso.GetAbsolutePathName(".") & "\Liste-Services_" & Computer & ".xls"
    objExcel.DisplayAlerts = True
    '--------------------------------------------------------------------
    Sub Cellule(X,NC,chaine)
    objExcel.Cells(X,NC).Value = Chaine
    End Sub
    '--------------------------------------------------------------------
    Listprocess.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
    ' test d'existence de Excel
    'On Error Resume Next
    'ReadKey=shell.RegRead("HKEY_CLASSES_ROOT\.xls\")
    'If Err.Number=0 
     
        Dim objXL
        Set objXL = WScript.CreateObject("Excel.Application")
        objXL.Workbooks.Add
        'objXL.ActiveWorkbook.Sheets.Add
        objXL.Cells(1,4).Value = Titre
        objXL.Visible = True
        objXL.Sheets("Feuil1").Name = "Processus"
        objXL.ActiveSheet.Tab.ColorIndex = 3
        objXL.Sheets("Feuil2").Name = "Services"
        objXL.ActiveSheet.Tab.ColorIndex = 4
        objXL.Sheets("Feuil3").Name = "Startup"
        objXL.ActiveSheet.Tab.ColorIndex = 3
        'objXL.Sheets("Feuil2").Activate
        'Set classeur = xlapp.Workbooks.add 
    'Set feuille = objXL.ActiveSheet(2) 
    nl=2
    nc=0
        objXL.Selection.Columns.AutoFit
        objXL.Rows("1:150").Select
        objXL.Selection.Font.Bold = True
        objXL.Selection.Font.Size = 12
     
    objXL.Cells(2, 1).Value = "Nom du Processus"
    objXL.Cells(2, 1).Font.Bold = TRUE
    objXL.Cells(2, 1).Interior.ColorIndex = 43
    objXL.Cells(2, 1).Font.ColorIndex = 2
    objXL.Cells(2,1).Select
    'objXL.Selection.Columns.AutoFit
    '*************************************************
    objXL.Cells(2, 2).Value = "Ligne de Commande"
    objXL.Cells(2, 2).Font.Bold = TRUE
    objXL.Cells(2, 2).Interior.ColorIndex = 43
    objXL.Cells(2, 2).Font.ColorIndex = 2
    objXL.Cells(2,2).Select
    'objXL.Selection.Columns.AutoFit
    '*************************************************
    'objXL.Cells(2, 3).Value = "Ligne de Commande"
    'objXL.Cells(2, 3).Font.Bold = TRUE
    'objXL.Cells(2, 3).Interior.ColorIndex = 43
    'objXL.Cells(2, 3).Font.ColorIndex = 2
    'objXL.Cells(2,3).Select
    'objXL.Selection.Columns.AutoFit
    '************************************************
    Dim Computer : Computer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _ 
    & Computer & "\root\cimv2") 
    Set ColProcess = objWMIService.ExecQuery ("Select * from Win32_Process")
    x=2
    nc=0
    nproc=0
    for each Process in ColProcess
    x = x + 1
        objXL.Cells(x, 1) = Process.Name
        objXL.Cells(x, 2) = Process.CommandLine
    Next    
    objXL.Columns("A:A").EntireColumn.AutoFit
    objXL.Columns("B:B").EntireColumn.AutoFit
    ' -------------------------------------
    Function Titre
    Set network  = Wscript.CreateObject("WScript.Network")
    computer=Ucase(network.ComputerName)
    RootTitle="Liste des processus sur " & computer
    Titre=rootTitle & " " & Date & " " & Time
    End Function
    ' -------------------------------------
    '--------------------------------------------------------------------
    Sub Cellule(NL,NC,chaine)
    objXL.Cells(X,NC).Value = Chaine
    End Sub
    '--------------------------------------------------------------------
    ListProcessCmdLine + Startup items.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
    'Les éléments à démarrage automatique + ListProcessCmdLine.vbs © Hackoo © 2011
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set Ws = CreateObject("WScript.Shell")
    Set ProcessEnv = Ws.Environment("Process")
    NomMachine = ProcessEnv("COMPUTERNAME") 
    NomUtilisateur = ProcessEnv("USERNAME") 
    NomFichierLog="Liste_Processus.txt"
    temp = Ws.ExpandEnvironmentStrings("%temp%")
    PathNomFichierLog = temp & "\" & NomFichierLog
    Set OutPut = fso.CreateTextFile(temp & "\" & NomFichierLog,2)
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _ 
    & strComputer & "\root\cimv2") 
    Set colProcesses = objWMIService.ExecQuery ("Select * from Win32_Process")
    count=0 
    'Dim StartTime : StartTime = Timer
    Call Infosys
    OutPut.WriteLine "[COLOR=""Red""]" & String(14,"*")& "Liste des Processus en cours d'exécution le " & date & " à " & time & " sur Le PC "& NomMachine &" connecté en tant que " & NomUtilisateur & String(14,"*")& vbNewline & String(80,"*") & "[/COLOR]"
    For Each objProcess in colProcesses
    ProcessName = objProcess.Name
    ProcessID = objProcess.ProcessID
    CommandLine = objProcess.CommandLine	
    count=count+1
    Texte = "Numéro PID = "& objProcess.ProcessID & VbNewLine & "Nom du Processus = " & objProcess.Name & VbNewLine &"Ligne de Commande = "& objProcess.CommandLine &_
    VbNewLine & String(100,"*")
    OutPut.WriteLine Texte
    Next
     
    OutPut.WriteLine  "Il y a "& Count &" Processus en cours d'exécution le " & date & " à " & time & " sur Le PC "& NomMachine &" connecté en tant que " & NomUtilisateur & vbNewline
    Call StartupCommand
    'OutPut.WriteLine FormatNumber(Timer - StartTime, 0) & " seconds."
    Function StartupCommand()
    strComputer = "."
    resultat=""
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colStartupCommands = objWMIService.ExecQuery ("Select * from Win32_StartupCommand")
     
    For Each objStartupCommand in colStartupCommands
    resultat=resultat & "Nom: " & objStartupCommand.Name & vbNewline
    resultat=resultat & "Description: " & objStartupCommand.Description & vbNewline
    resultat=resultat & "Emplacement: " & objStartupCommand.Location & vbNewline
    resultat=resultat & "Commande: " & objStartupCommand.Command & vbNewline
    resultat=resultat & "Utilisateur: " & objStartupCommand.User & vbNewline
    resultat=resultat & String(100,"*") & vbNewline 
    Next
    OutPut.WriteLine "[COLOR=""Red""]" & String(50,"*") &" Les éléments à démarrage automatique "& String(40,"*") &"[/COLOR]"
    OutPut.WriteLine resultat & "[/quote]"
    end Function
     
    Explorer(PathNomFichierLog)
     
    Function Explorer(File)
        Set ws=CreateObject("wscript.shell")
        ws.run "Notepad "& File,1,True
    end Function
     
    Function InfoSys
    strComputer = "."
    strMessage=""
    Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colSettings = objWMIService.ExecQuery  ("Select * from Win32_ComputerSystem")
    Set colSettings2 = objWMIService.ExecQuery ("Select * from Win32_BIOS")
    Set colSettings3 = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
    For Each objBIOS in colSettings2 
          strMessage=strMessage & "[quote]BIOS " & objBIOS.Version & vbNewline & vbNewline
    Next
    For Each objComputer in colSettings 
          strMessage=strMessage & "Nom de l'ordinateur : " & objComputer.Name & vbNewline & "Fabriquant: " & objComputer.Manufacturer & vbNewline & "Modèle : " & objComputer.Model & vbNewline & vbNewline
     
    Next
    For Each objOperatingSystem in colSettings3
          strMessage=strMessage &  objOperatingSystem.Name & vbNewline
          strMessage=strMessage &  "Version " & objOperatingSystem.Version & vbNewline
          strMessage=strMessage &  "Service Pack " & objOperatingSystem.ServicePackMajorVersion & "." & objOperatingSystem.ServicePackMinorVersion &vbNewline
          strMessage=strMessage &  "Dossier de Windows: " & objOperatingSystem.WindowsDirectory &vbNewline
    Next
    OutPut.WriteLine strMessage & ""
    end Function
    Donc, je veux faire un Vbscript 3 en 1

    1. Sheet1 = List of Process
    2. Sheet2 = List of Services
    3. Sheet3 = List of Startup items


    encore de votre éventuelle aide

  4. #4
    Modérateur
    Avatar de l_autodidacte
    Homme Profil pro
    Retraité : Directeur de lycée/Professeur de sciences physiques
    Inscrit en
    Juillet 2009
    Messages
    2 415
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Retraité : Directeur de lycée/Professeur de sciences physiques
    Secteur : Enseignement

    Informations forums :
    Inscription : Juillet 2009
    Messages : 2 415
    Points : 5 806
    Points
    5 806
    Par défaut
    Bonjour hackoofr

    Quand tu lances le fichier ListProcess.vbs en premier, tu as déjà 3 feuilles dénommées Process, Services et Startup. Si tu modifies le début du fichier ListServices.vbs de la façon suivante tu auras ce que tu cherches :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Set objExcel = GetObject(,"Excel.Application")
    objExcel.Visible = True
    'objExcel.Workbooks.Add
    objExcel.WorkSheets("Services").Activate
    Ceci est valable quand Excel est déjà lancé(visible ou non).

    A toi de voir si tu fais un seul fichier vbs englobant le tout ou tu lances les 2 autres à partir de ListProcess.vbs
    [EDIT] Le fichier Starup.vbs doit être modifié pour que la sortie soit vers le fichier Excel avec un code comme le précédent en remplaçant Services par Startup
    Ne pas oublier le tag si satisfait.
    Voter pour toute réponse satisfaisante avec pour encourager les intervenants.
    Balises CODE indispensables. Regardez ICI
    Toujours utiliser la clause Option Explicit(VBx, VBS ou VBA) et Ne jamais typer variables et/ou fonctions en VBS.
    Vous pouvez consulter mes contributions
    Ne pas oublier de consulter les différentes FAQs et les Cours/Tutoriels VB6/VBScript
    Ne pas oublier L'Aide VBScript et MSDN VB6 Fr

Discussions similaires

  1. VBSCRIPT : Processus EXCEL persistant
    Par datastep dans le forum VBScript
    Réponses: 6
    Dernier message: 27/08/2010, 13h39
  2. créer vbscript avec excel
    Par thechosenone5949 dans le forum VBScript
    Réponses: 7
    Dernier message: 14/01/2010, 14h27
  3. Utilisation de VBScript avec Excel
    Par aldapal dans le forum VBScript
    Réponses: 5
    Dernier message: 17/01/2008, 21h18
  4. Vbscript lecture excel
    Par eaque49 dans le forum Windows
    Réponses: 1
    Dernier message: 06/12/2007, 22h00

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