| 12
 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
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 
 | ' --------------------------------------------------------------------
' Script d'affichage détaillé des processus en cours
' sur une machine locale ou distante
' Fait appel à Internet Explorer pour la saisie
' des paramètres et l'affichage des résultats
'Jean-Claude BELLAMY © 2002
' --------------------------------------------------------------------
Dim args, network, computer, fso, ts, tparam, tPrint, oIE, fExec, user,domain, system,process
Set network  = Wscript.CreateObject("WScript.Network")
Set Shell = WScript.CreateObject("WScript.Shell")
' Création du fichier HTML qui va servir de formulaire
fichtml=GetPath() & "processlist.html"
ficparam=GetPath() & "processlist.cfg"
ficprint=GetPath() & "processlist.txt"
 
' Autoriser le contenu actif à s'exécuter dans les fichiers de la zone Ordinateur local
LockDown="HKLM\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LOCALMACHINE_LOCKDOWN\"
Keysec1=LockDown & "iexplore.exe"
itemtype = "REG_DWORD"
Shell.RegWrite Keysec1,0,itemtype
 
local=lcase(network.ComputerName)
Set args=Wscript.Arguments
If args.count>0 Then
	computer=lcase(args(0))
Else
	computer=local  
	End If
If computer=local Then typeordi="local" else typeordi="distant" 
RootTitle="Liste des processus sur " & computer & " (" & typeordi & ")"
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set ts  = fso.CreateTextFile(fichtml, True)
isparam=false
If fso.FileExists(ficparam)  Then
	Set tparam=fso.OpenTextFile(ficparam)
	isparam=true
	End If
'================ Choix des paramètres ================
WriteHTMLHeader 
header="Paramètres à afficher dans "
header=header & "<input type=""checkbox"" name=""ie"" value=""ie"" checked >IE"
' test d'existence de Excel
On Error Resume Next
ReadKey=shell.RegRead("HKEY_CLASSES_ROOT\.xls\")
If Err.Number=0 Then header=header & "<input type=""checkbox"" name=""excel"" value=""excel"" >Excel"
On error goto 0
header=header & "<input type=""checkbox"" name=""notepad"" value=""notepad"" >Bloc-notes"
ts.writeline "<b>" & header & "   :</b><p>"
ts.writeline "<table>"
objet="Win32_Process"
Set System = GetObject("winmgmts:" & objet)
n=0
ncol=3
dim prop()
for each Property in System.Properties_
	AddProp Property.Name
	next
' Ajout des méthodes GetOwner et GetOwnerSid"
AddProp "GetOwner"
AddProp "GetOwnerSid"
r=ncol-(n mod ncol)
If r<>ncol Then 
	For i = 1 To r
		ts.writeline "<td></td>"
		Next
	ts.writeline "</tr>"
	end if
ts.writeline "</table>"
WriteHTMLBottom "Afficher","Fermer"
 
If isParam Then	tParam.Close
RunIE 600,600,true
 
DisplayIE=false
DisplayExcel=false
DisplayNotepad=false
if oIE.Document.processlistForm.IE.Checked then DisplayIE=true
'if oIE.Document.processlistForm.Excel.Checked then DisplayExcel=true
if oIE.Document.processlistForm.Notepad.Checked then DisplayNotepad=true
 
Set tParam = fso.CreateTextFile(ficparam, True)
' Utilisation de la fonction execute afin de créer dynamiquement 
' des commandes faisant intervenir des noms de champs variables
dim f(), res()
redim f(n), res(n)
lmax=0
for i = 0 to n-1
	f(i) = "function testparam() " & vbcrlf
	f(i) = f(i) & "testparam=0" & vbcrlf
	f(i) = f(i) & "if oIE.Document.processlistForm.param" & i &".Checked then testparam=1" & vbcrlf
	f(i) = f(i) & "end function" & vbcrlf
	execute f(i)
	res(i)=testparam()
	state=""
	If res(i)=1 Then 
		state="Checked"
		l=len(prop(i))
		If lmax<l then lmax=l
		end if
	tParam.writeline state
	next
oIE.Quit
tParam.close
 
'================ Affichage des résultats ================
Set ts  = fso.CreateTextFile(fichtml, True)
If DisplayExcel Then
	Dim objXL
	Set objXL = WScript.CreateObject("Excel.Application")
	objXL.Workbooks.Add
	objXL.Cells(1,1).Value = Titre
	objXL.Visible = True
	End If
 
If DisplayNotepad Then 
	Set tPrint = fso.CreateTextFile(ficprint, True)
	tprint.writeline Titre
	tprint.WriteBlankLines(1)
	end if
WriteHTMLHeader
ts.writeline "<b>Liste des processus<b> (" & date & " " & time & ")<br>"
ts.writeline "<table border=""1"" cellspacing=""1"" cellpadding=""2"" style=""border-collapse: collapse"" bordercolor=""#111111"">"
ts.writeline "<tr>"
first=true
nl=2
nc=0
ts.writeline "<td bgcolor=""blue"" valign=""top"">Terminer</td>"
For i= 0 To n-1
	If res(i)=1 Then
		If DisplayNotepad then
			If not first Then tprint.write chr(9) else first=false
			tprint.write prop(i) 
			end if
		nc=nc+1
		If DisplayExcel Then objXL.Cells(nl,nc).Value = prop(i)
		comment=""
		If lcase(prop(i))="executablepath" Then 
			comment="<br><i>Cliquer sur un lien pour afficher<br>le fichier dans l'explorateur</i>"
			end if
		ts.writeline "<td bgcolor=""blue"" valign=""top"">" & prop(i) & comment & "</td>"
		End If
	Next
ts.writeline "</tr>"
If DisplayNotepad Then tprint.WriteBlankLines(2)
 
Set System=GetObject ("winmgmts:{impersonationLevel=impersonate}!//" & Computer).InstancesOf(objet)
nl=2
nc=0
nproc=0
dim ProcState(),ProcNum()
for each Process in System
	nl=nl+1
	first=true
	SetKill Process.Handle
	ts.writeline "<tr><a name=""" & Process.Handle & """>"
	ts.writeline "<td align=""center""><input type=""button"" value=""M"" style=""font-family: Wingdings"" name=""" & Process.Handle & """ onClick='Kill("""& Process.Handle & """)'></td>" 
	nc=0
	For i= 0 To n-3
		If res(i)=1 Then
			var ="Process." & prop(i)
			valeur=eval(var)
			If IsNull(valeur) Then valeur=""
			link=""
			If prop(i)="ExecutablePath" and valeur<>"" Then
				filename=valeur
				If computer<>local Then	filename="\\" & computer & "\" & Replace(filename, ":", "$")
				link="<a href=""#" & Process.Handle & """ onClick='Explore("""& filename & """)'>"
				End If
			addProcess 0,valeur,link
			End If
		Next
	If res(n-2)=1 Then
		result=Process.GetOwner(user,domain)
		addProcess result, user & "/" & domain,""
		end if
	If res(n-1)=1 Then
		result=Process.GetOwnerSid(SID)
		addProcess result, SID,""
		end if
	ts.writeline "</tr>"
	If DisplayNotepad Then tprint.writeline ""
	next
 
If DisplayExcel Then
	objXL.Rows("2:2").Select
	objXL.Selection.Font.Bold = True
	d1=int((nc-1)/26)
	d2=((nc-1) mod 26)
	If d1=0 Then l1max="" else l1max=chr(Asc("A")+d1-1)
	l2max=chr(Asc("A")+d2)
	objXL.Columns("A:" & l1max & l2max).Select
	objXL.Selection.Columns.AutoFit
	objXL.Rows("1:1").Select
	objXL.Selection.Font.Bold = True
	objXL.Selection.Font.Size = 12
	end if
 
ts.writeline "</table>"
 
If DisplayNotepad Then 
	tprint.close
	commande=shell.ExpandEnvironmentStrings("%windir%\notepad.exe " & chr(34) & ficprint & chr(34)) 
	shell.Run commande, 1
	end if
 
If DisplayIE Then
	WriteHTMLBottom "","Fermer"
	RunIE 600,400,false
	End If
 
Wscript.quit
 
'------------------------------------------------------------
Function FormatStr(ch,lmax)
l=len(ch)
If l<lmax Then 
	For k = l+1 To lmax
		ch=ch & " "
		Next
	End If
FormatStr=ch
End Function
'------------------------------------------------------------
' Fonction de récupération du répertoire courant
Function GetPath()
Dim path
path = WScript.ScriptFullName
GetPath = Left(path, InStrRev(path, "\"))
End Function
'------------------------------------------------------------
Sub WriteHTMLHeader
ts.writeline "<html>"
ts.writeline "<head>"
ts.writeline "<title>" & Titre & "</title>"
ts.writeline "<STYLE TYPE=""text/css"">"
ts.writeline "	body {"
ts.writeline "	font-family: Verdana;"
ts.writeline "	font-size: 8 pt }"
ts.writeline "	h1, h2, h3, h4, h5, h6 { font-family: Verdana }"
ts.writeline "table {"
ts.writeline "	table-border-color-light: rgb(102,204,204);" 
ts.writeline "	table-border-color-dark:  rgb(0,102,102);" 
ts.writeline "	font-size: 8 pt;"
ts.writeline "	font-family: Verdana }"
ts.writeline "</STYLE>"
ts.writeline "</head>"
ts.writeline "<body bgcolor=#000000 text=#Green>" '<body bgcolor=""#FFFFD2"">
ts.writeline "<script language=""VBScript""> "
ts.writeline "<!--"
ts.writeline "Dim ready,flagfile,file,flagkill,ID"
ts.writeline "Sub B0_OnClick"
ts.writeline "ready=-1"
ts.writeline "End Sub"
ts.writeline "Sub B1_OnClick"
ts.writeline "ready=1"
ts.writeline "End Sub"
 
ts.writeline "Sub Window_OnLoad()"
ts.writeline "ready=0"
ts.writeline "flagfile=0"
ts.writeline "flagkill=0"
ts.writeline "file="""""
ts.writeline "ID=0"
ts.writeline "End Sub"
 
ts.writeline "Public Function CheckVal()"
ts.writeline "CheckVal=ready"
ts.writeline "End function"
 
ts.writeline "Public Function CheckFile()"
ts.writeline "CheckFile=flagfile"
ts.writeline "End function"
 
ts.writeline "Public Function CheckID()"
ts.writeline "CheckID=flagkill"
ts.writeline "End function"
 
ts.writeline "Public Sub ResetFile()"
ts.writeline "flagFile=0"
ts.writeline "End sub"
 
ts.writeline "Public Sub ResetID()"
ts.writeline "flagkill=0"
ts.writeline "End sub"
 
ts.writeline "Public Function GetFile()"
ts.writeline "GetFile=file"
ts.writeline "End function"
 
ts.writeline "Public Function GetID()"
ts.writeline "GetID=ID"
ts.writeline "End function"
 
ts.writeline "function Explore(filename)"
ts.writeline "flagfile=1"
ts.writeline "file=filename" 
ts.writeline "End function" 
 
ts.writeline "function Kill(handle)"
ts.writeline "flagkill=1"
ts.writeline "ID=handle" 
ts.writeline "End function" 
 
ts.writeline "'-->"
ts.writeline "</script>"
ts.writeline "<form name=""processlistForm"">"
ts.writeline "<h3><center>Ordinateur " & typeordi & " " & Computer & "</center></h3><hr>"
End Sub
' -------------------------------------
Sub WriteHTMLBottom(B1,B0)
ts.writeline "<br>"
If B1<>"" Then ts.writeline "<center><input type=""button"" value=""" & B1 & """ name=""B1""></center>"
If B0<>"" Then ts.writeline "<center><input type=""button"" value=""" & B0 & """ name=""B0""></center>"
ts.writeline "</form>"
ts.writeline "</body>"
ts.writeline "</html>"
ts.Close
End Sub
' -------------------------------------
Sub RunIE(W,H,testclose)
' Ouverture d'Internet Explorer
Set oIE = WScript.CreateObject("InternetExplorer.Application", "IE_")
oIE.Left = 300        
oIE.Top = 100 
oIE.Height = H
oIE.Width = W
oIE.MenuBar = 0         
oIE.ToolBar = 0
oIE.StatusBar = 1
oIE.navigate fichtml  
oIE.Visible = 2      
Do While (oIE.Busy)    
	WScript.Sleep 200  
	Loop
shell.AppActivate Titre
 
' Attente d'action sur le bouton ou fermeture de la fenêtre
On Error Resume Next 
Do                   
	WScript.Sleep 100
	If oIE.Document.Script.CheckFile()<>0 Then
		path=oIE.Document.Script.GetFile()
		oIE.Document.Script.ResetFile
		If path<>"" Then Shell.run "explorer /select," & path,1
		End If
	If oIE.Document.Script.CheckID()<>0 Then
		ID=oIE.Document.Script.GetID()
		oIE.Document.Script.ResetID
		If ID<>0 Then 
			If GetStateKill(ID)=0 Then
				MsgBox "Le processus " & ID & " n'existe plus", vbExclamation,"Terminaison de processus"
			Else
				rep=MsgBox("Etes-vous sûr de terminer le processus " & ID & "?", vbYesNo + vbQuestion,"Terminaison de processus")
				If rep=vbYes Then 
					result=killprocess(ID)
					If result=0 Then CancelKill(ID)
					end if
				End If
			end if
		End If
	Loop While (oIE.Document.Script.CheckVal() = 0)
' Si on ferme directement IE sans passer par un bouton, 
' cela provoque une erreur qui est détectée et alors
' on quitte le script
If Err <> 0 Then 
	If testclose Then 
		Wscript.quit 
	else 
		On Error goto 0
		exit sub
		end if
	end if
test=oIE.Document.Script.CheckVal()
If test=-1 Then 
	oIE.Quit
	If testclose Then Wscript.quit
	end if
On Error goto 0
End Sub
' -------------------------------------
Sub AddProp(ch)
n=n+1
redim preserve prop(n)
prop(n-1)=ch
state=""
If isParam then If not tParam.AtEndOfStream Then state=tParam.ReadLine
If ((n-1) mod ncol)=0 Then ts.writeline "<tr>"
ts.writeline "<td><input type=""checkbox"" name=""param" & n-1 &""" value=""" & prop(n-1) & """ " & state & ">" & prop(n-1) & "</td>"
If (n mod ncol)=0 Then ts.writeline "</tr>"
End Sub
' -------------------------------------
Sub AddProcess(result,ch, chlink)
tag=""
If IsNumeric(ch) Then tag=" align=""right"""
ts.writeline "<td" & tag & ">" 
If result=0 Then 
	If chlink<>"" Then ts.writeline chlink
	ts.writeline ch
	If chlink<>"" Then ts.writeline "</a>" 
	end if
ts.writeline "</td>"
If DisplayNotepad Then 
	If not first Then tprint.write chr(9) else first=false
	If result=0 Then tprint.write ch  
	end if
nc=nc+1	
If DisplayExcel and result=0 Then objXL.Cells(nl,nc).Value = ch
End Sub
' -------------------------------------
Function Titre
Titre=rootTitle & " " & Date & " " & Time
End Function
' -------------------------------------
Function KillProcess(ID)
Set System=GetObject("winmgmts:{impersonationLevel=impersonate}!//" & Computer).ExecQuery("select * from " _
& objet & " where Handle=" & ID)
result=-1
for each Process in System
	result=Process.terminate(0)
	If result<>0 Then 
		MsgBox "Erreur code " & result, vbExclamation,"Terminaison processus " & ID
	Else
		MsgBox "Processus correctement terminé", vbInformation,"Terminaison processus " & ID
		End If
	next
KillProcess=result
End Function
' -------------------------------------
Sub SetKill(Handle)
nproc=nproc+1
redim preserve ProcNum(nproc),ProcState(nproc)
ProcNum(nproc-1)=handle
ProcState(nproc)=1
End Sub
' -------------------------------------
Function GetStateKill(ID)
GetStateKill=0
For i = 0 To nproc-1
	If ProcNum(i)=ID Then
		GetStateKill=ProcState(i)
		exit function
		End If
	Next
End Function
' -------------------------------------
Sub CancelKill(ID)
For i = 0 To nproc-1
	If ProcNum(i)=ID Then
		ProcState(i)=0
		exit sub
		End If
	Next
End Sub
' ------------------------------------- | 
Partager