(VBS) Conseil pour minimiser le code

Bonjour,

Je voudrais savoir comment reduire le code suivant :


' Requete WMI pour obtenir la liste des process en cours
	Set objWMIService  = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & strComputer) 
	Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process") 
	
	Affichage = "<center><b><u>Liste des processus</b></u><p>"
	
	For Each Proc in colProcessList
		If Proc.Name = "amqsvc.exe" Then AMQSVC=1
		If Proc.Name = "amqzmuc0.exe" Then AMQZMUC0=1
		If Proc.Name = "amqzxma0.exe" Then AMQZXMA0=1
		If Proc.Name = "amqzfuma.exe" Then AMQZFUMA=1
		If Proc.Name = "amqzlaa0.exe" Then AMQZLAA0=1
		If Proc.Name = "amqzmgr0.exe" Then AMQZMGR0=1
		If Proc.Name = "amqzmur0.exe" Then AMQZMUR0=1
		If Proc.Name = "amqrrmfa.exe" Then AMQRRMFA=1
		If Proc.Name = "amqzdmaa.exe" Then AMQZDMAA=1
		If Proc.Name = "amqpcsea.exe" Then AMQPCSEA=1
		If Proc.Name = "amqxssvn.exe" Then AMQXSSVN=1
		If Proc.Name = "runmqchi.exe" Then RUNMQCHI=1
		If Proc.Name = "runmqlsr.exe" Then RUNMQLSR=1
	Next	
		
	If AMQSVC = 1 Then 
		Affichage = Affichage & "<font color=green>amqsvc.exe</font> <br>"
	Else 
		Affichage = Affichage & "<font color=red>amqsvc.exe</font><br>"
	End If
	
	If AMQZMUC0 = 1 Then 
		Affichage = Affichage & "<font color=green>amqzmuc0.exe</font> <br>"
	Else 
		Affichage = Affichage & "<font color=red>amqzmuc0.exe</font><br>"
	End If

Et ainsi de suite pour tous les processus présent dans la boucle.
Aujourd’hui mon script marche mais n’est pas très esthétique et pas pratique en cas de rajout d’autre processus.
J’aimerais pouvoir les mettre dans une variable puis boucler dessus mais je sais pas comment faire.
Sa doit être surement très con mais je sèche là.

Merci de vos suggestion
Edité le 26/07/2007 à 11:23

Tu peux faire avec une fonction :

function couleur (colProcessList as Variant, process as String) as String
For Each Proc in colProcessList
If Proc.Name = process Then
couleur = “green”
exit function
endif
Next
couleur = “red”
end function

Et pour le programme principal :
Affichage = Affichage & “<font color=” & couleur(colProcessList, “amqsvc.exe”) & ">amqsvc.exe
"
Affichage = Affichage & “<font color=” & couleur(colProcessList, “amqsvc.exe”) & ">amqzmuc0.exe
"
etc

Je n’ai pas testé, mais l’idée est là

Merci pour ta réponse mais finalement j’ai fait la chose suivante :


	' Requete WMI pour obtenir la liste des process en cours
	Set objWMIService  = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & strComputer) 
	Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process") 
	
	Affichage = "<center><b><u>Liste des processus</b></u><p>"
	
	ListeProc="amqsvc.exe;amqzmuc0.exe;amqzxma0.exe;amqzfuma.exe;amqzlaa0.exe;amqzmgr0.exe;amqzmur0.exe;"
	ListeProc=ListeProc & "amqrrmfa.exe;amqzdmaa.exe;amqpcsea.exe;amqxssvn.exe;runmqchi.exe;runmqlsr.exe"
	
	TabList = Split(ListeProc , ";")
	Redim TabList2(CInt(UBound(TabList)))
	
	For Each Proc in colProcessList
		For i=LBound(TabList) to UBound(TabList)
			If Proc.Name = TabList(i) Then TabList2(i) = 1
		Next
	Next	
	
	For i=LBound(TabList) to UBound(TabList)
		If TabList2(i) = 1 Then
			Affichage = Affichage & "<font color=green>" & TabList(i) & "</font><br>"
		Else 
			Affichage = Affichage & "<font color=red>" & TabList(i) & "</font><br>"
		End If
	Next

Et pour l’avenir, je n’ai plus que la chaine initiale à modifier.

Tu peux faire aussi

For Each Proc in colProcessList
For i=LBound(TabList) to UBound(TabList)
If Proc.Name = TabList(i) Then TabList2(i) = “green” else TabList2(i) = “red”
Next
Next

For i=LBound(TabList) to UBound(TabList)
Affichage = Affichage & “” & TabList(i) & “

Next