{"id":208350,"date":"2023-09-27T07:30:54","date_gmt":"2023-09-27T07:30:54","guid":{"rendered":"https:\/\/www.ninjaone.com\/script-hub\/comment-redemarrer-un-service-avec-powershell\/"},"modified":"2024-03-04T20:44:38","modified_gmt":"2024-03-04T20:44:38","slug":"comment-redemarrer-un-service-avec-powershell","status":"publish","type":"script_hub","link":"https:\/\/www.ninjaone.com\/fr\/script-hub\/comment-redemarrer-un-service-avec-powershell\/","title":{"rendered":"Guide : Comment red\u00e9marrer un service avec PowerShell"},"content":{"rendered":"<p><span class=\"TextRun SCXW249427078 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW249427078 BCX0\">En tant que professionnels de l&rsquo;informatique et <\/span><\/span><a class=\"Hyperlink SCXW249427078 BCX0\" href=\"https:\/\/www.ninjaone.com\/fr\/quest-ce-quun-msp\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span class=\"TextRun Underlined SCXW249427078 BCX0\" lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW249427078 BCX0\" data-ccp-charstyle=\"Hyperlink\">fournisseurs de services g\u00e9r\u00e9s (MSP)<\/span><\/span><\/a><span class=\"TextRun SCXW249427078 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW249427078 BCX0\">, la gestion et <\/span><span class=\"NormalTextRun SCXW249427078 BCX0\">l&rsquo;optimisation<\/span><span class=\"NormalTextRun SCXW249427078 BCX0\"> des services syst\u00e8me font partie int\u00e9grante de notre <\/span><span class=\"NormalTextRun ContextualSpellingAndGrammarErrorV2Themed SCXW249427078 BCX0\">r\u00f4le<\/span><span class=\"NormalTextRun SCXW249427078 BCX0\">. PowerShell, un langage de script robuste, <\/span><span class=\"NormalTextRun SCXW249427078 BCX0\">fournit<\/span><span class=\"NormalTextRun SCXW249427078 BCX0\"> un moyen efficace d&rsquo;effectuer ces t\u00e2ches. Il permet l&rsquo;automatisation des t\u00e2ches, simplifie les op\u00e9rations complexes en quelques lignes de code et <\/span><span class=\"NormalTextRun SCXW249427078 BCX0\">facilite<\/span><span class=\"NormalTextRun SCXW249427078 BCX0\"> la gestion des services sur plusieurs machines simultan\u00e9ment.<\/span><\/span><span class=\"EOP SCXW249427078 BCX0\" data-ccp-props=\"{}\">\u00a0<\/span><\/p>\n<h2>Le script<\/h2>\n<p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\">&lt;#\r\n.SYNOPSIS\r\n    Restart one or more services.\r\n.DESCRIPTION\r\n    Restart one or more services. This also try three more times to get the service(s) to start, all the while waiting 15 seconds between each attempt.\r\n.EXAMPLE\r\n     -Name \"ServiceName\"\r\n    Restarts a service with the name ServiceName\r\n.EXAMPLE\r\n     -Name \"ServiceName\",\"AnotherServiceName\" -WaitTimeInSecs 15\r\n    Restarts two services with the names ServiceName and AnotherServiceName and waits 15 Seconds for them all to start\r\n.EXAMPLE\r\n    PS C:&gt; Restart-Service.ps1 -Name \"ServiceName\"\r\n    Restarts a service with the name ServiceName\r\n.EXAMPLE\r\n    PS C:&gt; Restart-Service.ps1 -Name \"ServiceName\",\"AnotherServiceName\" -WaitTimeInSecs 15\r\n    Restarts two services with the names ServiceName and AnotherServiceName and waits 15 Seconds for them all to start\r\n.NOTES\r\n    Exit Code 0: All service(s) restarted\r\n    Exit Code 1: Some or all service(s) failed to restart\r\n    Release Notes:\r\n    Initial Release\r\nBy using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https:\/\/www.ninjaone.com\/terms-of-use.\r\n    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. \r\n    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. \r\n    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. \r\n    Warranty Disclaimer: The script is provided \u201cas is\u201d and \u201cas available\u201d, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. \r\n    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. \r\n    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. \r\n    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).\r\n#&gt;\r\n[CmdletBinding()]\r\nparam (\r\n    # Name of service(s), either Name or DisplayName from Get-Service cmdlet\r\n    [Parameter(Mandatory = $true)]\r\n    [String[]]\r\n    $Name,\r\n    # The number of attempts to restart the service before giving up\r\n    [Parameter()]\r\n    [int]\r\n    $Attempts = 3,\r\n    # Duration in Seconds to wait for service(s) to start between each attempt\r\n    [Parameter()]\r\n    [int]\r\n    $WaitTimeInSecs = 15\r\n)\r\n\r\nbegin {\r\n    function Test-Service {\r\n        [CmdletBinding()]\r\n        param (\r\n            [Parameter()]\r\n            [String[]]\r\n            $Services\r\n        )\r\n        if ((Get-Service | Where-Object { ($_.Name -in $Services -or $_.DisplayName -in $Services) -and $_.Status -like \"Running\" }).Count -gt 0) {\r\n            $true\r\n        }\r\n        else {\r\n            $false\r\n        }\r\n    }\r\n    $FailedToStart = 0\r\n}\r\nprocess {\r\n    # Get service(s)\r\n    $Services = Get-Service | Where-Object { $_.Name -in $Name -or $_.DisplayName -in $Name }\r\n    if ($Services.Count -eq 0) {\r\n        Write-Error \"No service(s) found.\"\r\n        exit 1\r\n    }\r\n\r\n    # Restart service(s)\r\n    $Services | ForEach-Object {\r\n        $AttemptCounter = $Attempts\r\n        # Restart the service\r\n        $Service = $_ | Restart-Service -PassThru\r\n        # Wait till status of service reaches Running, timeout after $WaitTimeInSecs seconds\r\n        $Service.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running, [timespan]::FromSeconds($WaitTimeInSecs)) | Out-Null\r\n        # Loop till either the service is in a running state or our $AttemptCounter reaches 0 or less\r\n        while ($(Get-Service -Name $Service.ServiceName).Status -ne [System.ServiceProcess.ServiceControllerStatus]::Running -or $AttemptCounter -le 0) {\r\n            # Start service\r\n            Start-Service -Name $Service.ServiceName\r\n            # Wait $WaitTimeInSecs seconds\r\n            Start-Sleep -Seconds $WaitTimeInSecs\r\n            $AttemptCounter = $AttemptCounter - 1\r\n        }\r\n        if ($((Get-Service -Name $Service.ServiceName).Status -ne [System.ServiceProcess.ServiceControllerStatus]::Running)) {\r\n            # Add 1 to later show the count of services that failed to reach the running state\r\n            $FailedToStart = $FailedToStart + 1\r\n            Write-Error -Message \"Failed to start service( $($Service.ServiceName) ) after $Attempts attempts.\"\r\n        }\r\n    }\r\n\r\n    # Print out services with their status\r\n    Get-Service | Where-Object { $_.Name -in $Name -or $_.DisplayName -in $Name }\r\n\r\n    # Check if service(s) have started\r\n    if ($FailedToStart -eq 0) {\r\n        # All service(s) have been restarted\r\n        Write-Host \"All Service(s) restarted.\"\r\n        exit 0\r\n    }\r\n    else {\r\n        # Some or all Service(s) failed to restart\r\n        Write-Error -Message \"Failed to start $FailedToStart service(s).\"\r\n        exit 1\r\n    }\r\n}\r\nend {}<\/pre>\n<p>&nbsp;<\/p>\n<br \/>\n<div class=\"in-context-cta\"><p>Acc\u00e9dez \u00e0 plus de 700 scripts dans le Dojo NinjaOne<\/p>\n<p><a href=\"https:\/\/www.ninjaone.com\/fr\/phase-de-test-gratuit\/\">Obtenez l&rsquo;acc\u00e8s<\/a><\/p>\n<\/div><\/p>\n<h2>Exploration de la cmdlet Restart-Service<\/h2>\n<p><span class=\"TextRun SCXW74170526 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW74170526 BCX0\">La cmdlet `Restart-Service` de PowerShell est un outil puissant qui vous permet de red\u00e9marrer un service sur un ordinateur local ou distant. Cette commande <\/span><span class=\"NormalTextRun SCXW74170526 BCX0\">consolide<\/span><span class=\"NormalTextRun SCXW74170526 BCX0\"> les processus d&rsquo;arr\u00eat et de d\u00e9marrage d&rsquo;un service, ce qui simplifie la gestion des services.<\/span><\/span><\/p>\n<h2>Extension de l&rsquo;utilisation de notre script de service de red\u00e9marrage<\/h2>\n<p><span class=\"TextRun SCXW67305974 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW67305974 BCX0\">Notre script PowerShell, con\u00e7u pour <\/span><span class=\"NormalTextRun SCXW67305974 BCX0\">exploiter<\/span><span class=\"NormalTextRun SCXW67305974 BCX0\"> la cmdlet `Restart-Service`, <\/span><span class=\"NormalTextRun SCXW67305974 BCX0\">offre<\/span><span class=\"NormalTextRun SCXW67305974 BCX0\"> une solution compl\u00e8te pour red\u00e9marrer un ou plusieurs services. Ce script, outre le red\u00e9marrage d&rsquo;un service, fait trois tentatives de d\u00e9marrage d&rsquo;un service si celui-ci <\/span><span class=\"NormalTextRun SCXW67305974 BCX0\">ne<\/span><span class=\"NormalTextRun SCXW67305974 BCX0\"> s&rsquo;initialise pas <\/span><span class=\"NormalTextRun SCXW67305974 BCX0\">imm\u00e9diatement<\/span><span class=\"NormalTextRun SCXW67305974 BCX0\">, en incorporant une pause de 15 secondes entre chaque tentative.<\/span><\/span><\/p>\n<h2>Applications du script<\/h2>\n<p><span class=\"TextRun SCXW221271371 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW221271371 BCX0\">Notre script \u00ab\u00a0Restart-Service\u00a0\u00bb n&rsquo;est pas seulement pratique pour le red\u00e9marrage imm\u00e9diat d&rsquo;un service, mais peut \u00e9galement \u00eatre utilis\u00e9 dans diff\u00e9rents sc\u00e9narios, tels que :<\/span><\/span><\/p>\n<h3>Red\u00e9marrages programm\u00e9s<\/h3>\n<p><span class=\"TextRun SCXW175456835 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW175456835 BCX0\">Vous pouvez utiliser le planificateur de t\u00e2ches en conjonction avec notre script pour red\u00e9marrer les services selon un calendrier.<\/span><span class=\"NormalTextRun SCXW175456835 BCX0\"> Cela peut \u00eatre utile pour les services qui doivent \u00eatre red\u00e9marr\u00e9s p\u00e9riodiquement pour lib\u00e9rer des ressources ou pour <\/span><span class=\"NormalTextRun SCXW175456835 BCX0\">maintenir<\/span> <span class=\"NormalTextRun SCXW175456835 BCX0\">des performances optimales<\/span><span class=\"NormalTextRun SCXW175456835 BCX0\">.<\/span><\/span><\/p>\n<h3>Red\u00e9marrage apr\u00e8s la mise \u00e0 jour<\/h3>\n<p><span class=\"TextRun SCXW18560881 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW18560881 BCX0\">Apr\u00e8s une mise \u00e0 jour du syst\u00e8me, il peut \u00eatre n\u00e9cessaire de red\u00e9marrer certains services. Notre script peut \u00eatre utilis\u00e9 dans un script post-mise \u00e0 jour pour s&rsquo;assurer que tous les services n\u00e9cessaires sont red\u00e9marr\u00e9s et fonctionnent correctement.<\/span><\/span><\/p>\n<h2>D\u00e9pannage du script<\/h2>\n<p>Malgr\u00e9 la robustesse des scripts PowerShell, il peut arriver que les choses ne se passent pas comme pr\u00e9vu. Voici quelques pistes pour r\u00e9soudre les probl\u00e8mes li\u00e9s \u00e0 notre script Restart-Service :<\/p>\n<p><strong>Journaux d&rsquo;erreurs<\/strong><br \/>\n<span class=\"TextRun SCXW56490188 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW56490188 BCX0\">PowerShell <\/span><span class=\"NormalTextRun SCXW56490188 BCX0\">fournit<\/span><span class=\"NormalTextRun SCXW56490188 BCX0\"> des journaux d&rsquo;erreurs d\u00e9taill\u00e9s qui peuvent \u00eatre utilis\u00e9s pour r\u00e9soudre les probl\u00e8mes. Si notre script <\/span><span class=\"NormalTextRun SCXW56490188 BCX0\">ne parvient pas \u00e0<\/span><span class=\"NormalTextRun SCXW56490188 BCX0\"> red\u00e9marrer un service, v\u00e9rifiez les journaux d&rsquo;erreur pour voir s&rsquo;il y a des messages d&rsquo;erreur ou des exceptions qui peuvent <\/span><span class=\"NormalTextRun SCXW56490188 BCX0\">fournir<\/span><span class=\"NormalTextRun SCXW56490188 BCX0\"> des indications sur ce qui n&rsquo;a pas fonctionn\u00e9.<\/span><\/span><\/p>\n<p><strong>D\u00e9bogage du script<\/strong><br \/>\n<span class=\"TextRun SCXW197375251 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW197375251 BCX0\">PowerShell <\/span><span class=\"NormalTextRun SCXW197375251 BCX0\">offre<\/span><span class=\"NormalTextRun SCXW197375251 BCX0\"> des fonctions de d\u00e9bogage qui peuvent \u00eatre utilis\u00e9es pour parcourir le script et <\/span><span class=\"NormalTextRun SCXW197375251 BCX0\">identifier<\/span><span class=\"NormalTextRun SCXW197375251 BCX0\"> o\u00f9 le probl\u00e8me se produit. Utilisez la commande`Set-<\/span><span class=\"NormalTextRun SpellingErrorV2Themed SCXW197375251 BCX0\">PSDebug<\/span><span class=\"NormalTextRun SCXW197375251 BCX0\"> -Trace 1` pour activer la tra\u00e7abilit\u00e9 du script, puis ex\u00e9cutez le script pour voir un affichage ligne par ligne des commandes ex\u00e9cut\u00e9es.<\/span><\/span><\/p>\n<h2>Conclusions<\/h2>\n<p><span data-contrast=\"auto\">En conclusion, PowerShell fournit une plateforme dynamique et efficace pour la gestion des services sur les syst\u00e8mes Windows. Gr\u00e2ce \u00e0 notre script PowerShell, vous pouvez automatiser le red\u00e9marrage des services, effectuer des op\u00e9rations complexes avec un minimum de code et g\u00e9rer les services sur plusieurs syst\u00e8mes. Restez \u00e0 l&rsquo;\u00e9coute pour en savoir plus sur l&rsquo;utilisation de PowerShell pour des op\u00e9rations informatiques efficaces.<\/span><span data-ccp-props=\"{}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Avec la plateforme <\/span><span data-contrast=\"none\">compl\u00e8te <a href=\"https:\/\/www.ninjaone.com\/fr\/\">de gestion des op\u00e9rations informatiques de NinjaOne<\/a><\/span><span data-contrast=\"auto\">, vous pouvez int\u00e9grer et \u00e9tendre l&rsquo;utilisation de scripts personnalis\u00e9s tels que notre script de red\u00e9marrage de service PowerShell sur de nombreux terminaux. La capacit\u00e9 de la plateforme \u00e0 d\u00e9ployer et \u00e0 g\u00e9rer des scripts de fa\u00e7on centralis\u00e9e permet d&rsquo;ex\u00e9cuter des t\u00e2ches complexes sur plusieurs appareils de fa\u00e7on optimale. Cela signifie que vous pouvez utiliser notre script pour g\u00e9rer les services sur l&rsquo;ensemble de votre r\u00e9seau, \u00e0 partir d&rsquo;un tableau de bord unique, afin <a href=\"https:\/\/www.ninjaone.com\/fr\/efficacite\/\">d&rsquo;am\u00e9liorer l&rsquo;efficacit\u00e9<\/a> et de garantir la coh\u00e9rence de vos op\u00e9rations informatiques.<\/span><\/p>\n","protected":false},"author":35,"featured_media":206745,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","_lmt_disableupdate":"no","_lmt_disable":""},"operating_system":[4212],"use_cases":[4287],"class_list":["post-208350","script_hub","type-script_hub","status-publish","has-post-thumbnail","hentry","script_hub_category-windows"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/script_hub\/208350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/script_hub"}],"about":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/types\/script_hub"}],"author":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/comments?post=208350"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/media\/206745"}],"wp:attachment":[{"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/media?parent=208350"}],"wp:term":[{"taxonomy":"script_hub_category","embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/operating_system?post=208350"},{"taxonomy":"use_cases","embeddable":true,"href":"https:\/\/www.ninjaone.com\/fr\/wp-json\/wp\/v2\/use_cases?post=208350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}