Bonjour,

Voici mon code :

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
 ' ==========================================================================
'  Nom             : SYS_CMD_ar_services.vbs
'  Projet          : Industrialisation
'
'  Repertoires     : script -> /production/home/${user}/sh}
'  Cree le         : 27/09/06
'  Version         : 1.0
'  Cree par        :
'  Objet           : Arret d un service systeme windows
'  Appel(s)        :
'  Environnements  : WINDOWS Y2K, Y2K3
'  Parametre(s)    : Commande à lancer avec ses arguments
'                    Lancer <Commande> [<arg1> <arg2> ...]
'
'  Sortie          : retourne code erreur / Sortie si pas d'argument
'  Codes retour    : 0 -> ok
'                    1 -> erreur de syntaxe
' ==========================================================================
'  Modification(s)
' --------------------------------------------------------------------------
'  JJ/MM/AA - QUI
'    QUOI
'
' ==========================================================================
'
' =============================================================================
'  Assignation des variables globales
' =============================================================================
 
 
' =============================================================================
'  Assignation des variables pour Post message Tivoli ou Log
' =============================================================================
 
Const PID = "_"
Const terminal = "not_a_TTY"
Const gravite = "WARNING"
Const valeur  = "OK"
Const EVENT_SUCCESS = 0
Private FSO, FicLog, FicLogBis
Dim ETAT_SERVICE
Dim WIN_SERVICE1
 
' =============================================================================
'  Main          :
'  Auteur        : 
'  Descriptif    :
' =============================================================================
'  Paramètre(s) entree :
'
'  Sortie : (fichiers, tables, codes erreur, ...)
' -----------------------------------------------------------------------------
'
'
' -----------------------------------------------------------------------------
' Action principale
' --------------------
 
'On instancie l'objet
Set FSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
 
'On instancie le fichier texte
Set Ftxt = FSO.CreateTextFile("D:\production\home\tws\log\SYS_LOG_systeme.log", False)
 
'On récupère les variables environnement
 
Function getSystemVar(VarName)
    Dim wss, env
    Set wss = CreateObject("WScript.Shell")
    Set env = wss.environment("process")
    getSystemVar = env(VarName)
End Function
 
'On recupere une partie dela chaine de caractere
 
resultat1 = Mid(getSystemVar("UNISON_SCHED"),2,3)
resultat2 = Mid(getSystemVar("UNISON_SCHED"),7,3)
resultat3 = Mid(getSystemVar("UNISON_SCHED"),5,3)
resultat4 = Mid(getSystemVar("UNISON_JOB"),14,43)
resultat5 = Mid(getSystemVar("USERNAME"),13.6)
 
'Fonction remontant le service et son etat
 
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, intSleep
strComputer = "."
intSleep = 5000
 
' NB strService is case sensitive.
Set args  = Wscript.Arguments
ETAT_SERVICE = args(0)
WIN_SERVICE1 = args(1)
 
message = "The service" & (WIN_SERVICE1)  & "is in state :" & (ETAT_SERVICE)
 
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name ="_
& WIN_SERVICE1 & " ")
For Each objService in colListOfServices
 
On Error Resume Next
If (ETAT_SERVICE) = "Startservice" Then
  objService.StartService(WIN_SERVICE1)
  Else If (ETAT_SERVICE) = "Stopservice" Then
   objService.StopService(WIN_SERVICE1)
 
 End If
 erreur = err.number 
End If
 
WSCript.Sleep intSleep
Next
 
'----------------------------------------------------------------------------------------------------------------------------------
'Creation dy fichier Log
 
If Err.Number = 0 Then
  Set Ftxt = FSO.GetFile("D:\production\home\tws\log\SYS_LOG_systeme.log") 
  Set FtxtBis = Ftxt.OpenAsTextStream(8, -2)
  FtxtBis.writeline (Year(date)) & vbTab & (Month(date)) & vbTab & (Day(date)) & vbTab & (time) & vbTab & (getSystemVar("COMPUTERNAME")) & vbTab & resultat1 & vbTab & resultat2 & vbTab & (PID) & vbTab & (terminal) & vbTab & resultat5 & vbTab & resultat4 & vbTab & (gravite) & vbTab & resultat3 & vbTab & (getSystemVar("UNISON_JOBNUM")) & vbTab & (valeur) & vbTab & (message) & vbTab & (erreur)
  FtxtBis.Close 
  Set FtxtBis = Nothing
  Ftxt.Close 
  Set Ftxt = Nothing
 
Else
  Set Ftxt = FSO.CreateTextFile("D:\production\home\tws\log\SYS_LOG_systeme.log", False)
  Set Ftxt = FSO.GetFile("D:\production\home\tws\log\SYS_LOG_systeme.log") 
  Set FtxtBis = Ftxt.OpenAsTextStream(8, -2) 
  FtxtBis.writeline (Year(date)) & vbTab & (Month(date)) & vbTab & (Day(date)) & vbTab & (time) & vbTab & (getSystemVar("COMPUTERNAME")) & vbTab & resultat1 & vbTab & resultat2 & vbTab & (PID) & vbTab & (terminal) & vbTab & resultat5 & vbTab & resultat4 & vbTab & (gravite) & vbTab & resultat3 & vbTab & (getSystemVar("UNISON_JOBNUM")) & vbTab & (valeur) & vbTab & (message) & vbTab & (erreur)
  FtxtBis.Close 'Fermeture
  Set FtxtBis = Nothing
  Ftxt.Close
  Set Ftxt = Nothing
End If
FSO.Close
 
'----------------------------------------------------------------------------------------------------------------------------------------
'Ecrire dans l'event Log
 
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, _
(now) & chr(10) & "Les services" &  chr(1) & (strService) & "sur le serveur" & chr(1) & (getSystemVar ("COMPUTERNAME")) & chr(1) & "ont étés" & chr(1) & "par l'utilisateur" & chr(1) & (getSystemVar("username"))' & chr(1) & (sh.IsServiceRunning("Alerter"))
 
'  Fin du Main {}
' -----------------------------------------------------------------------------
'
'  eof
'
=============================================================================
Ce script permet de creer un fichier log avec differentes informations.
Il fonctionne tres bien mais le problème est le suivant:
je voudrais recupere l'erreur level du service que j 'arrete ou je demarre.
Je m'explique si le service s 'arrete bien il me rmeonte l erreur 0 sinon il remonte une autre numero d erreur 1.

Merci de votre aide