From 1e909e5ee11440f84c8441162f603b3e142f9c11 Mon Sep 17 00:00:00 2001 From: Pascal Ott Date: Wed, 17 Jun 2020 20:53:09 +0200 Subject: [PATCH] added Service removal to CleanInstall --- .../DeployWindowsService.ps1 | 19 +++++++++++++++---- .../WindowsServiceManagerV4/task.json | 4 ++-- .../WindowsServiceManagerV4/utility.ps1 | 7 +++---- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/WindowsServiceManager/WindowsServiceManagerV4/DeployWindowsService.ps1 b/WindowsServiceManager/WindowsServiceManagerV4/DeployWindowsService.ps1 index e207de8..586a12e 100644 --- a/WindowsServiceManager/WindowsServiceManagerV4/DeployWindowsService.ps1 +++ b/WindowsServiceManager/WindowsServiceManagerV4/DeployWindowsService.ps1 @@ -98,12 +98,23 @@ $scriptBlock = { Write-Output "[$env:ComputerName]: Instance Name: [$instanceName]" $serviceName = "{0}`${1}" -f $ServiceName.split('$')[0], $instanceName } - + Write-Output "[$env:ComputerName]: Attempting to locate [$ServiceName]" $serviceObject = Get-WindowsService -ServiceName $ServiceName - # If the service does not exist and the installtion path can only be provided if the Install Service flag is passed. - if ($null -eq $serviceObject -and $null -ne $installationPath) { - Write-Output "[$env:ComputerName]: Unable to locate [$ServiceName] creating a new service" + # If the service does not exist or cleanInstall is enabled and the installtion path can only be provided if the Install Service flag is passed. + if (($null -eq $serviceObject -or $CleanInstall) -and $null -ne $installationPath) { + if ($serviceObject) { + Write-Output "[$env:ComputerName]: Clean install set to [$CleanInstall], removing the Service [$ServiceName]" + + $serviceObject = Stop-WindowsService -ServiceName $ServiceName + + $serviceObject.Delete() + Write-Output "[$env:ComputerName]: Removed Service [$ServiceName]" + } + else { + Write-Output "[$env:ComputerName]: Unable to locate [$ServiceName] creating a new service" + } + if ($installTopShelfService) { $parentPath = Get-FullExecuteablePath -StringContainingPath $installationPath -JustParentPath if (-not(Test-Path $parentPath)) { diff --git a/WindowsServiceManager/WindowsServiceManagerV4/task.json b/WindowsServiceManager/WindowsServiceManagerV4/task.json index a9643f3..9e5bbbc 100644 --- a/WindowsServiceManager/WindowsServiceManagerV4/task.json +++ b/WindowsServiceManager/WindowsServiceManagerV4/task.json @@ -14,7 +14,7 @@ "version": { "Major": 4, "Minor": 4, - "Patch": 0 + "Patch": 1 }, "instanceNameFormat": "Deploy Windows Service", "groups": [ @@ -101,7 +101,7 @@ "label": "Service Description", "defaultValue": "", "required": false, - "helpMarkDown": "The Displayname of the windows service." + "helpMarkDown": "The Description of the windows service." }, { "name": "ServiceStartupType", diff --git a/WindowsServiceManager/WindowsServiceManagerV4/utility.ps1 b/WindowsServiceManager/WindowsServiceManagerV4/utility.ps1 index 974b531..fbbee8f 100644 --- a/WindowsServiceManager/WindowsServiceManagerV4/utility.ps1 +++ b/WindowsServiceManager/WindowsServiceManagerV4/utility.ps1 @@ -18,7 +18,7 @@ function Get-WindowsService { ( $ServiceName ) - Get-WmiObject -Class Win32_Service | Where-Object { $PSItem.Name -eq $ServiceName } + return Get-WmiObject -Class Win32_Service | Where-Object { $PSItem.Name -eq $ServiceName } } function Start-WindowsService { @@ -63,7 +63,7 @@ function Stop-WindowsService { if ($StopProcess) { Write-Verbose "[$env:ComputerName]: [$ServiceName] did not respond within [$Timeout] seconds, stopping process." - $fullPath = Get-FullExecuteablePath -StringContainingPath $serviceObject.PathName + $parentPath = Get-FullExecuteablePath -StringContainingPath $serviceObject.PathName -JustParentPath $allProcesses = Get-Process $process = $allProcesses | Where-Object { $_.Path -like "$parentPath\*" } @@ -81,9 +81,8 @@ function Stop-WindowsService { while ($serviceObject.State -ne 'Stopped') Write-Output "[$env:ComputerName]: Stopped Service [$ServiceName]" - - return $serviceObject } + return $serviceObject } function Get-FullExecuteablePath {