Script arret d'un service / reboot machine. pouvez-vous me le valider ?

kerberos69 Messages postés 21 Date d'inscription mercredi 6 avril 2005 Statut Membre Dernière intervention 9 juillet 2008 - 20 avril 2005 à 10:53
crenaud76 Messages postés 4172 Date d'inscription mercredi 30 juillet 2003 Statut Membre Dernière intervention 9 juin 2006 - 20 avril 2005 à 12:55
bonjour, j'ai crée ce script afin d'arreter un service et de rebooter la machine.
mon probleme est que je ne sais pas si le service s'arrete proprement car le reboot fonctionne, et du coup au redemarrage le service est démarré.

l'ai-je bien construit ? (dois-je mettre un séparateur entre les 2 scripts ?)
si je veux arreter 2 services, quelle ligne dois-je rajoueter et ou svp ?

merci beaucoup !!!

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


Set colServiceList = objWMIService.ExecQuery("Associators of " _
& "{Win32_Service.Name='spooler'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Antecedent" )


For Each objService in colServiceList
objService.StopService()
Next


Wscript.Sleep 20000


Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where Name='spooler'")
For Each objService in colServiceList
errReturn = objService.StopService()
Next


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


Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")


For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Reboot()
Next

6 réponses

cboulas Messages postés 2641 Date d'inscription mercredi 2 juin 2004 Statut Membre Dernière intervention 8 janvier 2014 16
20 avril 2005 à 11:16
Salut, pour stopper un service, il y a plus simple tu envoi une commande DOS :

net stop [NomDuService]
shutdown -r -t 00

La 1ère ligne stop le service, et la 2nd relance le pc

Chris...
Web : Firstruner - eMail : [mailto:support@firstruner.com Support]&nbs
0
kerberos69 Messages postés 21 Date d'inscription mercredi 6 avril 2005 Statut Membre Dernière intervention 9 juillet 2008
20 avril 2005 à 11:28
merci chris.

en fait, je cherche a me perfectionner sur VB, donc j'essaie d'utiliser ce langage pour arriver a mes fins.

Ok, je suis un peu compliqué et c'est juste pour ma connaissance personnelle. comme tu dis je pourrais utiliser un batch classique, mais j'ai envie de faire plus tordu ;-)

merci pour ta reponse
0
cboulas Messages postés 2641 Date d'inscription mercredi 2 juin 2004 Statut Membre Dernière intervention 8 janvier 2014 16
20 avril 2005 à 11:31
Je comprend, je vais essayé de le tester quand j'aurais un moment sur mon pc et te dirais la réponse.


Chris...
Web : Firstruner - eMail : [mailto:support@firstruner.com Support]&nbs
0
kerberos69 Messages postés 21 Date d'inscription mercredi 6 avril 2005 Statut Membre Dernière intervention 9 juillet 2008
20 avril 2005 à 11:42
merci

le reboot local fonctionne super bien, en revanche aucune idee pour savoir si le service est correctement stoppé. c'est un peu ça mon probleme en fait
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cboulas Messages postés 2641 Date d'inscription mercredi 2 juin 2004 Statut Membre Dernière intervention 8 janvier 2014 16
20 avril 2005 à 11:53
Si tu le test en local, il te suffit d'aller dans le gestionnaire de service afin de voir s'il est en pause, en marche, ou à l'arrêt, s'il n'y a rien de noter, c'est qu'il est à l'arrêt


Chris...
Web : Firstruner - eMail : [mailto:support@firstruner.com Support]&nbs
0
crenaud76 Messages postés 4172 Date d'inscription mercredi 30 juillet 2003 Statut Membre Dernière intervention 9 juin 2006 28
20 avril 2005 à 12:55
voici un bout de code à mettre dans un module VB pour arrêter un service :

Private Const SC_MANAGER_CONNECT As Long = &H1
Private Const SC_MANAGER_CREATE_SERVICE As Long = &H2
Private Const SC_MANAGER_ENUMERATE_SERVICE As Long = &H4
Private Const SC_MANAGER_LOCK As Long = &H8
Private Const SC_MANAGER_MODIFY_BOOT_CONFIG As Long = &H20
Private Const SC_MANAGER_QUERY_LOCK_STATUS As Long = &H10
Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Private Const SC_MANAGER_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT Or SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVICE Or SC_MANAGER_LOCK Or SC_MANAGER_QUERY_LOCK_STATUS Or SC_MANAGER_MODIFY_BOOT_CONFIG)


Private Const SERVICE_CONTROL_STOP As Long = &H1
Private Const SERVICE_STOP As Long = &H20


Private Type SERVICE_STATUS
dwServiceType As Long
dwCurrentState As Long
dwControlsAccepted As Long
dwWin32ExitCode As Long
dwServiceSpecificExitCode As Long
dwCheckPoint As Long
dwWaitHint As Long
End Type


Private Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Private Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
Private Declare Function ControlService Lib "advapi32.dll" (ByVal hService As Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long
Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
'---------------------------------------------------------------------------------------
' Procédure : Stop_Service
' Auteur : Christophe RENAUD
' Objet : Arrête un service sur une machine
' Retour : True si le service a été arrêté et False sinon
' Arguments : - sServiceName (Entrée) : Nom du service à arrêter
' - sComputername (Entrée) : Nom NETBIOS de la machine sur laquelle doit être
' arrêté le service
'---------------------------------------------------------------------------------------
'
Public Function Stop_Service(sServiceName As String, sComputername As String) As Boolean
Dim hSCM As Long, hService As Long, RetVal As Long, ServiceStatus As SERVICE_STATUS

hSCM = OpenSCManager("\" & sComputername, vbNullString, SC_MANAGER_ALL_ACCESS)
If hSCM = 0 Then
Stop_Service = False
Exit Function
End If
hService = OpenService(hSCM, sServiceName, SERVICE_STOP)
If hService = 0 Then
Stop_Service = False
Exit Function
End If
RetVal = ControlService(hService, SERVICE_CONTROL_STOP, ServiceStatus)
If RetVal = 0 Then
Stop_Service = False
Exit Function
End If
CloseServiceHandle (hService)
CloseServiceHandle (hSCM)
Stop_Service = True
End Function

Christophe R
0
Rejoignez-nous