From 4a4a9dfa0bd0d918482b733fd1026176bb72a356 Mon Sep 17 00:00:00 2001 From: Ilya Shipitsin Date: Mon, 7 Nov 2016 16:14:23 +0000 Subject: [PATCH] check whether service exists (before stopping) --- test-openvpn.ps1 | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/test-openvpn.ps1 b/test-openvpn.ps1 index e1fe3ab..da95eee 100644 --- a/test-openvpn.ps1 +++ b/test-openvpn.ps1 @@ -95,8 +95,19 @@ Function Stop-Gui { } Function Stop-Openvpnservice { - Stop-Service OpenVPNService - Stop-Service OpenVPNServiceLegacy + ('OpenVPNService','OpenVPNServiceLegacy') | % { + [String] $s = $_ + [Int] $c = (get-service).where{ $_.Name -eq $s}.Count + if($c -gt 0){ + # Service exists, stop it + Stop-Service $s -Force + do { + ('Stopping {0} ...' -f $s) + $c = (Get-Service).where({ ($_.Name -eq $s) -and ($_.status -eq 'Stopped') }).Count + sleep -Seconds 1 + } until ($c -eq 1) + } + } } # Stop all openvpn-related processes