[Ex] Les services Windows
Posté : jeu. 16 déc. 2010 11:20
par toto1515
Voici pour ceux que ça intéresse un petit code qui permet de lister les services fonctionnant sous Windows.
Il est possible de sélectionner les éléments à recueillir.
On peut voir le résultat à l'écran ou lancer une application Word, sauvegardant le fichier créé.
Dans le code, changez le répertoire par défaut pour la sauvegarde du fichier Word.
Les remarques sont les bienvenues...
Il est possible de sélectionner les éléments à recueillir.
On peut voir le résultat à l'écran ou lancer une application Word, sauvegardant le fichier créé.
Dans le code, changez le répertoire par défaut pour la sauvegarde du fichier Word.
► Afficher le texte
Code : Tout sélectionner
cs ----------------------------------------------------------------------------
AutoIt Version : 3.3.0.0
Auteur: F. Accard
Fonction du Script :
- Proposer un choix des informations à recueillir,
- Proposer un choix pour la visualisation (Ecran ou Word)
- Établir la liste des services de Windows présents sur l'ordinateur en fonction des choix retenus,
- Afficher les informations à l'écran ou Sauvegarder ces informations dans un fichier Word.
#ce ----------------------------------------------------------------------------
#include <Word.au3>
#include <GuiListView.au3>
#Region définition des variables
$GUI_EVENT_CLOSE = -3
$_ServiceStatus_Success = 0
$_ServiceStatus_GeneralError = 1
$WS_OVERLAPPEDWINDOW = 0x00CF0000
$WS_CLIPSIBLINGS = 0x04000000
dim $chkboxtext[17][3]
dim $tabchoix[17]
$chkboxtext[0][0]="Nom affiché"
$chkboxtext[0][1]="Service Scheduler2 Acronis"
$chkboxtext[1][0]="Description"
$chkboxtext[1][1]="Planification de tâches pour les applications Acronis."
$chkboxtext[2][0]="Etat"
$chkboxtext[2][1]="Démarré, Arrêté, En Pause"
$chkboxtext[3][0]="Type de démarrage"
$chkboxtext[3][1]="Automatique, Manuel, Désactivé"
$chkboxtext[4][0]="Nom"
$chkboxtext[4][1]="AcrSch2Svc"
$chkboxtext[5][0]="Chemin"
$chkboxtext[5][1]='"C:\Program Files\Common Files\Acronis\Schedule2\schedul2.exe"'
$chkboxtext[6][0]="Process ID"
$chkboxtext[6][1]="1388"
$chkboxtext[7][0]="Démarré"
$chkboxtext[7][1]="oui"
$chkboxtext[8][0]="Pause Possible"
$chkboxtext[8][1]="non"
$chkboxtext[9][0]="Arrêt possible"
$chkboxtext[9][1]="oui"
$chkboxtext[10][0]="Interaction avec le bureau"
$chkboxtext[10][1]="non"
$chkboxtext[11][0]="Error Control"
$chkboxtext[11][1]="Normal, Ignoré"
$chkboxtext[12][0]="ExitCode"
$chkboxtext[12][1]="0"
$chkboxtext[13][0]="Type"
$chkboxtext[13][1]="Own Process"
$chkboxtext[14][0]="Compte"
$chkboxtext[14][1]="LocalSystem"
$chkboxtext[15][0]="Nom du Système"
$chkboxtext[15][1]="PC-DE-FRANCOIS"
$chkboxtext[16][0]="Capture"
$chkboxtext[16][1]="Service Scheduler2 Acronis"
$top=45
$left=25
$docdirorg="G:\0perso\Fichiers Docs\"
#EndRegion définition des variables
#Region définition de l'interface
guicreate("SERVICES WINDOWS",505,560)
GUICtrlCreateLabel("Choissisez les infos à recueillir",10,10,150,20)
GUICtrlCreateLabel("Infos :",$left+15,30,150,20)
GUICtrlCreateLabel("Exemples :",$left+160,30,150,20)
for $i=0 to 16
$chkboxtext[$i][2]=GUICtrlCreateCheckbox($chkboxtext[$i][0] ,$left,$top)
GUICtrlCreatelabel($chkboxtext[$i][1] ,$left+150,$top+4)
$top=$top+25
next
#Region valeurs par défaut
GUICtrlSetState($chkboxtext[0][2], 1)
GUICtrlSetState($chkboxtext[1][2], 1)
GUICtrlSetState($chkboxtext[2][2], 1)
GUICtrlSetState($chkboxtext[3][2], 1)
GUICtrlSetState($chkboxtext[5][2], 1)
#EndRegion valeurs par défaut
;$docdir="G:\0perso\Fichiers Docs\"
$mydoc= "mes services " & @year & @MON & @MDAY & " " & @HOUR & @MIN & @SEC & ".doc"
$top=$top+5
$monlabelrep=GUICtrlCreateLabel("Chemin",25,$top,36,20)
$monLabelnom=GUICtrlCreateLabel("Nom",25,$top+25,36,20)
$moninputrep=GUICtrlCreateInput($docdirorg,65,$top,385,20)
$moninputnom=GUICtrlCreateInput($mydoc,65,$top+25,385,20)
$top=$top+45
$btnvaldef=GUICtrlCreateButton("Valeurs par défaut",30,$top+5,100,25)
$btnOkScreen=GUICtrlCreateButton("Voir à l'écran",160,$top+5,90,25)
$btnOkWord=GUICtrlCreateButton("Voir avec Word",260,$top+5,90,25)
$btnquitter=GUICtrlCreateButton("Quitter",375,$top+5,100,25)
guisetstate()
#EndRegion définition de l'interface
#Region traitement des événements
While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE or $msg=$btnquitter Then Exit
If $msg = $btnvaldef Then
for $T=0 to 16
GUICtrlSetState($chkboxtext[$T][2], 4)
next
GUICtrlSetState($chkboxtext[0][2], 1)
GUICtrlSetState($chkboxtext[1][2], 1)
GUICtrlSetState($chkboxtext[2][2], 1)
GUICtrlSetState($chkboxtext[3][2], 1)
GUICtrlSetState($chkboxtext[5][2], 1)
$mydoc= "mes services " & @year & @MON & @MDAY & " " & @HOUR & @MIN & @SEC & ".doc"
GUICtrlSetData($moninputrep,$docdirorg)
GUICtrlSetData($moninputnom,$mydoc)
endif
if $msg=$btnOkWord or $msg=$btnOkScreen Then
$tot=0
; on lit les valeurs des checkbox
for $i=0 to 16
$tabchoix[$i]=guictrlread($chkboxtext[$i][2])
$tot=$tot+$tabchoix[$i]
next
if $tot=68 Then ; si aucune n'a été cochée
msgbox(0,"ERREUR","Vous devez sélectionner au moins 1 case...")
Else
if $msg=$btnOkWord Then
$choixvue="Word"
$docdir=guictrlread($moninputrep)
if stringright($docdir,1) <>"\" then $docdir=$docdir & "\"
if stringlen($docdir)=1 Then _
$docdir=@MyDocumentsDir & "\"
$mydoc = guictrlread($moninputnom)
if stringright($mydoc,4) <>".doc" then $mydoc = $mydoc & ".doc"
if stringlen($mydoc)=4 Then _
$mydoc= "mes services " & @year & @MON & @MDAY & " " & @HOUR & @MIN & @SEC & ".doc"
If FileExists($docdir) Then
exitloop
Else
MsgBox(4096,"ERREUR" , "Le répertoire : " & chr(13) & _
chr(9) & $docdir & chr(13) & _
"n'existe pas ..." )
EndIf
elseif $msg=$btnOkScreen Then
$choixvue="Ecran"
exitloop
endif
endif
endif
WEnd
guidelete()
#EndRegion traitement des événements
#Region création de la liste des services
dim $mesServices[1] = [0]
$Service = ObjGet("winmgmts:\\" & "localhost" & "\root\cimv2")
$sItems = $Service.ExecQuery ("Select * from Win32_Service")
For $objService In $sItems
$i = UBound($mesServices)
ReDim $mesServices[$i+1]
if $tabchoix[0]=1 then
$mesServices[$i] &= $objService.DisplayName & @TAB
endif
if $tabchoix[1]=1 then
$mesServices[$i] &= $objService.Description & @TAB
endif
if $tabchoix[2]=1 then
if $objService.State="Stopped" Then
$mesServices[$i] &= "Arrêté" & @TAB
elseif $objService.State="Running" Then
$mesServices[$i] &= "Démarré" & @TAB
elseif $objService.State="Paused" Then
$mesServices[$i] &= "En pause" & @TAB
endif
endif
if $tabchoix[3]=1 then
if $objService.StartMode="Auto" Then
$mesServices[$i] &= "Automatique" & @TAB
elseif $objService.StartMode="Manual" Then
$mesServices[$i] &= "Manuel" & @TAB
elseif $objService.StartMode="Disabled" Then
$mesServices[$i] &= "Désactivé" & @TAB
endif
endif
if $tabchoix[4]=1 then
$mesServices[$i] &= $objService.Name & @TAB
endif
if $tabchoix[5]=1 then
$mesServices[$i] &= $objService.PathName & @TAB
endif
if $tabchoix[6]=1 then
$mesServices[$i] &= $objService.ProcessID & @TAB
endif
if $tabchoix[7]=1 then
if $objService.Started=-1 Then
$mesServices[$i] &= "oui" & @TAB
else
$mesServices[$i] &= "non" & @TAB
endif
endif
if $tabchoix[8]=1 then
if $objService.AcceptPause=-1 Then
$mesServices[$i] &= "oui" & @TAB
Else
$mesServices[$i] &= "non" & @TAB
endif
endif
if $tabchoix[9]=1 then
if $objService.AcceptStop=-1 Then
$mesServices[$i] &= "oui" & @TAB
Else
$mesServices[$i] &= "non" & @TAB
endif
endif
if $tabchoix[10]=1 then
if $objService.DesktopInteract=-1 Then
$mesServices[$i] &= "oui" & @TAB
Else
$mesServices[$i] &= "non" & @TAB
endif
endif
if $tabchoix[11]=1 then
$objService.ErrorControl
if $objService.ErrorControl="Normal" Then
$mesServices[$i] &= "Normal" & @TAB
elseif $objService.ErrorControl="Ignore" Then
$mesServices[$i] &= "Ignoré" & @TAB
endif
endif
if $tabchoix[12]=1 then
$mesServices[$i] &= $objService.ExitCode & @TAB
endif
if $tabchoix[13]=1 then
$mesServices[$i] &= $objService.ServiceType & @TAB
endif
if $tabchoix[14]=1 then
$mesServices[$i] &= $objService.StartName & @TAB
endif
if $tabchoix[15]=1 then
$mesServices[$i] &= $objService.SystemName & @TAB
endif
if $tabchoix[16]=1 then
$mesServices[$i] &= $objService.Caption & @TAB
endif
$mesServices[0] = $i
Next
;on trie les services par ordre croissant sur la première colonne
$iFin = UBound($mesServices) - 1
$iDeb = 0
monArraySort($mesServices, $iDeb, $iFin)
#EndRegion création de la liste des services
#Region Elaboration du texte final
;on défini la première ligne
$montextedebut=""
for $j=0 to 16
if $tabchoix[$j]=1 then
$montextedebut &=$chkboxtext[$j][0] & @TAB
endif
next
$montexte = $montextedebut & @CRLF
; on défini tout le texte
For $i = 1 To $mesServices[0]
;$txttempo=$mesServices[$i]
$montexte &= $mesServices[$i] & @CRLF
next
#EndRegion Elaboration du texte final
#Region traitement avec Word
if $choixvue="Word" then
;~on lance une application Word
$oWordApp = _WordCreate ("",1,1,1)
$oDoc = _WordDocGetCollection ($oWordApp, 0)
sleep(1000)
;~ on y écrit le texte précédent
$oDoc.Range.Text= $montexte
;~ on supprime les tabulations de fin de ligne
_WordDocFindReplace($oDoc, "^t^p", "^p")
;~ on active la fenêtre
opt("WinTitleMatchMode",2)
WinActivate("Microsoft Word")
sleep(250)
;~ on met en paysage et on coverti le texte en tableau
send("!fm!y{ENTER}^a!bit{HOME}")
;~ élaboration du nom du fichier
$mydocname= $docdir & $mydoc
sleep(1000)
;~ on sauvegarde le fichier
_WordDocSaveas($oDoc,$mydocname)
endif
#EndRegion traitement avec Word
#Region traitement à l'écran
if $choixvue="Ecran" Then
opt("GUIDataSeparatorChar",@TAB)
GuiCreate("Mes services Windows", 810, 487,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
Dim $process_list_view = GUICtrlCreateListView($montextedebut, 10, 10, 790, 440, _
$LVS_SHOWSELALWAYS, $LVS_EX_GRIDLINES )
$btnActu = GuiCtrlCreateButton("Actualiser", 20, 460, 150, 20)
GetServicesInfos($process_list_view)
GuiSetState()
While 1
$msg = GuiGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
ExitLoop
Case $msg = $btnActu
GetServicesInfos($process_list_view)
EndSelect
WEnd
Endif
#EndRegion traitement à l'écran
Exit
#cs ----------------------------------------------------------------------------
Les fonctions utilisées
#ce ----------------------------------------------------------------------------
Func GetServicesInfos(ByRef $ListView)
$Top_Item = _GUICtrlListView_GetTopIndex($ListView)
_GUICtrlListView_DeleteAllItems($ListView)
For $i = 1 To $mesServices[0]
GUICtrlCreateListViewItem($mesServices[$i], $Listview)
Next
_GUICtrlListView_EnsureVisible($ListView, $Top_Item, 1)
EndFunc ;~ --> GetServicesInfos
Func monArraySort(ByRef $leTableau, ByRef $iDebut, ByRef $iFinal)
Local $vTmp
If ($iFinal - $iDebut) < 15 Then
Local $i, $j, $vCur
For $i = $iDebut + 1 To $iFinal
$vTmp = $leTableau[$i]
If IsNumber($vTmp) Then
For $j = $i - 1 To $iDebut Step -1
$vCur = $leTableau[$j]
If ($vTmp >= $vCur And IsNumber($vCur)) Or (Not IsNumber($vCur) And StringCompare($vTmp, $vCur) >= 0) Then ExitLoop
$leTableau[$j + 1] = $vCur
Next
Else
For $j = $i - 1 To $iDebut Step -1
If (StringCompare($vTmp, $leTableau[$j]) >= 0) Then ExitLoop
$leTableau[$j + 1] = $leTableau[$j]
Next
EndIf
$leTableau[$j + 1] = $vTmp
Next
Return
EndIf
Local $L = $iDebut, $R = $iFinal, $vPivot = $leTableau[Int(($iDebut + $iFinal) / 2)], $fNum = IsNumber($vPivot)
Do
If $fNum Then
While ($leTableau[$L] < $vPivot And IsNumber($leTableau[$L])) Or (Not IsNumber($leTableau[$L]) And StringCompare($leTableau[$L], $vPivot) < 0)
$L += 1
WEnd
While ($leTableau[$R] > $vPivot And IsNumber($leTableau[$R])) Or (Not IsNumber($leTableau[$R]) And StringCompare($leTableau[$R], $vPivot) > 0)
$R -= 1
WEnd
Else
While (StringCompare($leTableau[$L], $vPivot) < 0)
$L += 1
WEnd
While (StringCompare($leTableau[$R], $vPivot) > 0)
$R -= 1
WEnd
EndIf
If $L <= $R Then
$vTmp = $leTableau[$L]
$leTableau[$L] = $leTableau[$R]
$leTableau[$R] = $vTmp
$L += 1
$R -= 1
EndIf
Until $L > $R
monArraySort($leTableau, $iDebut, $R)
monArraySort($leTableau, $L, $iFinal)
EndFunc ;~ --> monArraySort