{"id":208654,"date":"2023-10-09T06:48:08","date_gmt":"2023-10-09T06:48:08","guid":{"rendered":"https:\/\/www.ninjaone.com\/script-hub\/hoe-een-service-herstarten-met-powershell\/"},"modified":"2024-02-28T22:51:07","modified_gmt":"2024-02-28T22:51:07","slug":"hoe-een-service-herstarten-met-powershell","status":"publish","type":"script_hub","link":"https:\/\/www.ninjaone.com\/nl\/script-hub\/hoe-een-service-herstarten-met-powershell\/","title":{"rendered":"Hoe een Service Herstarten met PowerShell: Een Stap-voor-Stap Handleiding"},"content":{"rendered":"<p><span class=\"TextRun SCXW249427078 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW249427078 BCX0\">Als IT-professionals en<\/span><\/span> <a class=\"Hyperlink SCXW249427078 BCX0\" href=\"https:\/\/www.ninjaone.com\/nl\/wat-is-een-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\">Managed Service Providers (MSP&#8217;s)<\/span><\/span><\/a> <span class=\"TextRun SCXW249427078 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW249427078 BCX0\">is het beheren en<\/span> <span class=\"NormalTextRun SCXW249427078 BCX0\">optimaliseren van<\/span> <span class=\"NormalTextRun SCXW249427078 BCX0\">systeemservices een integraal onderdeel van onze<\/span> <span class=\"NormalTextRun ContextualSpellingAndGrammarErrorV2Themed SCXW249427078 BCX0\">rollen<\/span><span class=\"NormalTextRun SCXW249427078 BCX0\">. PowerShell, een robuuste scripttaal,<\/span> <span class=\"NormalTextRun SCXW249427078 BCX0\">biedt<\/span> <span class=\"NormalTextRun SCXW249427078 BCX0\">een effici\u00ebnte manier om deze taken uit te voeren. Het maakt taakautomatisering mogelijk, vereenvoudigt complexe bewerkingen met slechts een paar regels code en<\/span> <span class=\"NormalTextRun SCXW249427078 BCX0\">vergemakkelijkt<\/span> <span class=\"NormalTextRun SCXW249427078 BCX0\">het beheer van services op meerdere machines tegelijk.<\/span><\/span><span class=\"EOP SCXW249427078 BCX0\" data-ccp-props=\"{}\">\u00a0<\/span><\/p>\n<h2>Het 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>Toegang tot meer dan 700 scripts in de NinjaOne Dojo<\/p>\n<p><a href=\"https:\/\/www.ninjaone.com\/nl\/gratis-proefformulier\/\">Toegang krijgen<\/a><\/p>\n<\/div><\/p>\n<h2>Het Restart-Service Cmdlet verkennen<\/h2>\n<p><span class=\"TextRun SCXW74170526 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW74170526 BCX0\">Het cmdlet `Restart-Service` in PowerShell is een krachtig hulpmiddel waarmee je een service op een lokale of externe computer opnieuw kunt starten. Dit commando<\/span> <span class=\"NormalTextRun SCXW74170526 BCX0\">consolideert<\/span> <span class=\"NormalTextRun SCXW74170526 BCX0\">de processen van het stoppen en starten van een service, wat het servicebeheer vereenvoudigt.<\/span><\/span><\/p>\n<h2>Het gebruik van ons Restart-Service Script uitbreiden<\/h2>\n<p><span class=\"TextRun SCXW67305974 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW67305974 BCX0\">Ons PowerShell-script, ontworpen om<\/span> <span class=\"NormalTextRun SCXW67305974 BCX0\">gebruik te maken van<\/span> <span class=\"NormalTextRun SCXW67305974 BCX0\">het cmdlet `Restart-Service`,<\/span> <span class=\"NormalTextRun SCXW67305974 BCX0\">biedt<\/span> <span class=\"NormalTextRun SCXW67305974 BCX0\">een uitgebreide oplossing voor het opnieuw opstarten van een of meer services. Dit script herstart niet alleen een service, maar doet ook drie pogingen om een service te starten als deze<\/span> <span class=\"NormalTextRun SCXW67305974 BCX0\">niet<\/span> <span class=\"NormalTextRun SCXW67305974 BCX0\">meteen<\/span> <span class=\"NormalTextRun SCXW67305974 BCX0\">initialiseert<\/span><span class=\"NormalTextRun SCXW67305974 BCX0\">, met een pauze van 15 seconden tussen elke poging<\/span><\/span><\/p>\n<h2>Toepassingen van het Script<\/h2>\n<p><span class=\"TextRun SCXW221271371 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW221271371 BCX0\">Ons Restart-Service-script is niet alleen handig voor het direct herstarten van services, maar kan ook worden gebruikt in verschillende scenario&#8217;s, zoals:<\/span><\/span><\/p>\n<h3>Geplande Restarts<\/h3>\n<p><span class=\"TextRun SCXW175456835 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW175456835 BCX0\">Je kunt Task Scheduler gebruiken in combinatie met ons script om services opnieuw te starten volgens een schema.<\/span><span class=\"NormalTextRun SCXW175456835 BCX0\">Dit kan voordelig zijn voor diensten die periodiek opnieuw opgestart moeten worden om bronnen vrij te maken of om<\/span> <span class=\"NormalTextRun SCXW175456835 BCX0\">optimale<\/span> <span class=\"NormalTextRun SCXW175456835 BCX0\">prestaties te behouden.<\/span><\/span><\/p>\n<h3>Opnieuw opstarten na een update<\/h3>\n<p><span class=\"TextRun SCXW18560881 BCX0\" lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW18560881 BCX0\">Na een systeemupdate moeten sommige services mogelijk opnieuw worden opgestart. Ons script kan worden gebruikt in een post-update script om ervoor te zorgen dat alle benodigde services opnieuw worden gestart en correct draaien.<\/span><\/span><\/p>\n<h2>Problemen met het script oplossen<\/h2>\n<p>Ondanks de robuustheid van PowerShell scripts kan het voorkomen dat dingen niet gaan zoals gepland. Hier zijn enkele manieren om problemen met ons Restart-Service-script op te lossen:<\/p>\n<p><strong>Error Logs<\/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\">biedt<\/span> <span class=\"NormalTextRun SCXW56490188 BCX0\">gedetailleerde error logs die kunnen worden gebruikt om problemen op te lossen. Als ons script<\/span> <span class=\"NormalTextRun SCXW56490188 BCX0\">er niet in slaagt om<\/span> <span class=\"NormalTextRun SCXW56490188 BCX0\">een service te herstarten, controleer dan de error logs op foutmeldingen of uitzonderingen die<\/span> <span class=\"NormalTextRun SCXW56490188 BCX0\">inzicht<\/span> <span class=\"NormalTextRun SCXW56490188 BCX0\">kunnen geven in wat er fout ging.<\/span><\/span><\/p>\n<p><strong>Het script debuggen<\/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\">biedt<\/span> <span class=\"NormalTextRun SCXW197375251 BCX0\">debugfuncties die kunnen worden gebruikt om door het script te stappen en<\/span> <span class=\"NormalTextRun SCXW197375251 BCX0\">te identificeren<\/span><span class=\"NormalTextRun SCXW197375251 BCX0\">waar het probleem optreedt. Gebruik het `Set-<\/span><span class=\"NormalTextRun SpellingErrorV2Themed SCXW197375251 BCX0\">PSDebug<\/span><span class=\"NormalTextRun SCXW197375251 BCX0\">-Trace 1` commando om scripttracering in te schakelen en voer vervolgens het script uit om een regel-voor-regel een weergave te zien van de commando&#8217;s die worden uitgevoerd.<\/span><\/span><\/p>\n<h2>Slotbeschouwingen:<\/h2>\n<p><span data-contrast=\"auto\">Kortom, PowerShell biedt een dynamisch en effici\u00ebnt platform voor het beheren van services op Windows-systemen. Met ons PowerShell-script kun je serviceherstartingen automatiseren, complexe bewerkingen uitvoeren met minimale code en services beheren op meerdere systemen. Blijf op de hoogte voor meer inzichten in het gebruik van PowerShell voor effici\u00ebnte IT-operaties.<\/span><span data-ccp-props=\"{}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Met NinjaOne&#8217;s<\/span> <span data-contrast=\"none\">uitgebreide <a href=\"https:\/\/www.ninjaone.com\/nl\/\">IT-platform voor operationeel beheer<\/a><\/span> <span data-contrast=\"auto\">kunt u het gebruik van aangepaste scripts, zoals ons PowerShell-serviceherstartscript, integreren en schalen over meerdere eindpunten. De mogelijkheid van het platform om scripts centraal in te zetten en te beheren maakt het uitvoeren van complexe taken op meerdere apparaten naadloos. Dit betekent dat je ons script kunt gebruiken om services in je hele netwerk te beheren vanaf \u00e9\u00e9n dashboard, <a href=\"https:\/\/www.ninjaone.com\/nl\/efficientie\/\">waardoor de effici\u00ebntie toeneemt<\/a> en je IT-activiteiten consistenter worden.<\/span><\/p>\n","protected":false},"author":35,"featured_media":206744,"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":[4300],"class_list":["post-208654","script_hub","type-script_hub","status-publish","has-post-thumbnail","hentry","script_hub_category-windows"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ninjaone.com\/nl\/wp-json\/wp\/v2\/script_hub\/208654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ninjaone.com\/nl\/wp-json\/wp\/v2\/script_hub"}],"about":[{"href":"https:\/\/www.ninjaone.com\/nl\/wp-json\/wp\/v2\/types\/script_hub"}],"author":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/nl\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/nl\/wp-json\/wp\/v2\/comments?post=208654"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/nl\/wp-json\/wp\/v2\/media\/206744"}],"wp:attachment":[{"href":"https:\/\/www.ninjaone.com\/nl\/wp-json\/wp\/v2\/media?parent=208654"}],"wp:term":[{"taxonomy":"script_hub_category","embeddable":true,"href":"https:\/\/www.ninjaone.com\/nl\/wp-json\/wp\/v2\/operating_system?post=208654"},{"taxonomy":"use_cases","embeddable":true,"href":"https:\/\/www.ninjaone.com\/nl\/wp-json\/wp\/v2\/use_cases?post=208654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}