TUTOS.EU

Vérification de l'abscence d'une liste de services

Comment vérifier qu'une liste de services donnée est bien absente

Dim strComputer, objWMIService, colListOfServices, objService
Dim ListeServices, TableauServices, ParseCaracter, UnService, NomService
Dim WSHShell
Dim vAPPDATA, vSystemDrive, vSystemRoot, vSESSIONNAME, vCOMPUTERNAME, vProgramFilesx32, vProgramFilesx64
Dim CheminScriptActuel, objFSO, objFichierLog, objFichierResultat

Const ForReading = 1
Const ForWritting = 2
Const ForAppending = 8

Call DetectExeType()

ListeServices = "dmserver;NMSSvc;NWCWorkstation"

Set WSHShell = CreateObject("WScript.Shell")

vAPPDATA = WSHShell.ExpandEnvironmentStrings("%APPDATA%")
vSystemDrive = WSHShell.ExpandEnvironmentStrings("%SystemDrive%") 'Retourne C: en général (sans le \ à la fin)
vSystemRoot = WSHShell.ExpandEnvironmentStrings("%systemroot%")
vSESSIONNAME = WSHShell.ExpandEnvironmentStrings("%SESSIONNAME%")
vCOMPUTERNAME = WSHShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
vProgramFilesx32 = WSHShell.ExpandEnvironmentStrings("%programfiles(x86)%")
vProgramFilesx64 = WSHShell.ExpandEnvironmentStrings("%programfiles%")

If Left(vProgramFilesx32,1) = "%" Then
	'Si on est sur un système 32bits, alors le chemin du répertoire Program Files est dans vProgramFilesx64 et non vProgramFilesx32.
	'Du coup on recopie l'un dans l'autre et on efface la variable vProgramFilesx64
	vProgramFilesx32 = vProgramFilesx64
	vProgramFilesx64 = ""
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")

CheminScriptActuel = Left(wscript.scriptfullname,Len(wscript.scriptfullname)-Len(wscript.scriptname)-1)
CheminFichierdeLog = CheminScriptActuel & "\" & wscript.scriptname & "_" & vCOMPUTERNAME & "_Log.txt"
CheminFichierResultat = CheminScriptActuel & "\" & wscript.scriptname & "_" & vCOMPUTERNAME & "_Resultat.txt"

Wscript.Echo "Fichier de log dans " & CheminFichierdeLog
Wscript.Echo "Fichier avec les résultats dans " & CheminFichierResultat

Set objFichierLog = objFSO.OpenTextFile(CheminFichierdeLog, ForWritting, True)
Set objFichierResultat = objFSO.OpenTextFile(CheminFichierResultat, ForWritting, True)


ParseCaracter = ";"
TableauServices = Split(ListeServices, ParseCaracter)

strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service")
'("Select * from Win32_Service WHERE Name LIKE 'myservicenamelike%'")
'objService.Description

objFichierLog.WriteLine("Services présent sur la machine :")
For Each objService in colListOfServices
	objFichierLog.WriteLine("Service : " & objService.Name & VbTab & objService.State)
	NomService = objService.Name
	NomService = Lcase(Trim(NomService))

	For Each UnService In TableauServices
		'objFichierLog.WriteLine("Recherche de " & UnService)

		If (NomService = Lcase(Trim(UnService))) Then
			objFichierResultat.WriteLine(vCOMPUTERNAME & VbTab & NomService & VbTab & objService.DisplayName & VbTab & objService.StartMode & VbTab & objService.State & VbTab & objService.PathName & VbTab & "Service trouvé alors qu'il ne devrait pas")
			objFichierLog.WriteLine(UnService & " a été trouvé alors qu'il ne devrait pas.")
			exit for
		End If

	Next	
	
Next

objFichierLog.Close 'Fermeture du fichier
objFichierResultat.Close 'Fermeture du fichier

Set objFichierLog = Nothing
Set objFichierResultat = Nothing

Set objFSO = Nothing
Set WSHShell = Nothing

Wscript.Echo "Terminé"


Sub DetectExeType()
	'Version du 10 juillet 2008

	Dim ScriptHost
	Dim ShellObject

	Dim CurrentPathExt
	Dim EnvObject

	Dim RegCScript
	Dim RegPopupType ' This is used to set the pop-up box flags.
											' I couldn't find the pre-defined names
	RegPopupType = 32 + 4

	On Error Resume Next

	ScriptHost = WScript.FullName
	ScriptHost = Right(ScriptHost, Len(ScriptHost) - InStrRev(ScriptHost, "\"))

	If (UCase(ScriptHost) = "WSCRIPT.EXE") Then
		WScript.Echo ("This script does not work with WScript.")

		' Create a pop-up box and ask if they want to register cscript as the default host.
		Set ShellObject = WScript.CreateObject("WScript.Shell")
		' -1 is the time to wait.  0 means wait forever.
		RegCScript = ShellObject.PopUp("Would you like to register CScript as your default host for VBscript?", 0, "Register CScript", RegPopupType)
		                                                    
		If (Err.Number <> 0) Then
			ReportError ()
			WScript.Echo "To run this script using CScript, type: ""CScript.exe " & WScript.ScriptName & """"
			WScript.Quit (GENERAL_FAILURE)
			WScript.Quit (Err.Number)
		End If

		' Check to see if the user pressed yes or no.  Yes is 6, no is 7
		If (RegCScript = 6) Then
			ShellObject.RegWrite "HKEY_CLASSES_ROOT\VBSFile\Shell\Open\Command\", "%WINDIR%\System32\CScript.exe //nologo ""%1"" %*", "REG_EXPAND_SZ"
			ShellObject.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSFile\Shell\Open\Command\", "%WINDIR%\System32\CScript.exe //nologo ""%1"" %*", "REG_EXPAND_SZ"
			' Check if PathExt already existed
			CurrentPathExt = ShellObject.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PATHEXT")
			If Err.Number = &H80070002 Then
				Err.Clear
				Set EnvObject = ShellObject.Environment("PROCESS")
				CurrentPathExt = EnvObject.Item("PATHEXT")
			End If

			ShellObject.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PATHEXT", CurrentPathExt & ";.VBS", "REG_SZ"

			If (Err.Number <> 0) Then
				ReportError ()
				WScript.Echo "Error Trying to write the registry settings!"
				WScript.Quit (Err.Number)
			Else
				WScript.Echo "Successfully registered CScript"
			End If
		Else
			WScript.Echo "To run this script type: ""CScript.Exe adsutil.vbs <cmd> <params>"""
		End If

		Dim ProcString
		Dim ArgIndex
		Dim ArgObj
		Dim Result

		ProcString = "Cscript //nologo " & WScript.ScriptFullName

		Set ArgObj = WScript.Arguments

		For ArgIndex = 0 To ArgCount - 1
				ProcString = ProcString & " " & Args(ArgIndex)
		Next

		'Now, run the original executable under CScript.exe
		Result = ShellObject.Run(ProcString, 0, True)

		WScript.Quit (Result)
	End If

End Sub
Lien vers le fichier : cliquez ici Copier le code

2