Tasks/Tasks.WindowsServices.ps1
#region Copyright & License # Copyright © 2012 - 2021 François Chabot # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #endregion Set-StrictMode -Version Latest # Synopsis: Deploy Windows Services task Deploy-WindowsServices Undeploy-WindowsServices, { $Resources | ForEach-Object -Process { Write-Build DarkGreen $_.Path $arguments = @{ Name = $_.Name BinaryPathName = $_.Path Credential = $_.Credential StartupType = if ($_.StartupType -eq 'AutomaticDelayedStart') { 'Automatic' } else { $_.StartupType } } if (![string]::IsNullOrWhiteSpace($_.DisplayName)) { $arguments.DisplayName = $_.DisplayName } if (![string]::IsNullOrWhiteSpace($_.Description)) { $arguments.Description = $_.Description } New-Service @arguments if ($_.StartupType -eq 'AutomaticDelayedStart') { # see https://serverfault.com/a/919676/336600 sc.exe config $_.Name --% start=delayed-auto } } } # Synopsis: Undeploy Windows Services task Undeploy-WindowsServices -If { -not $SkipUndeploy } { $Resources | ForEach-Object -Process { Write-Build DarkGreen $_.Path # https://stackoverflow.com/questions/4967496/check-if-a-windows-service-exists-and-delete-in-powershell $service = Get-CimInstance -ClassName Win32_Service -Filter "Name='$($_.Name)'" if ($service) { Remove-CimInstance -InputObject $service -ErrorAction Stop } } } # Synopsis: Start Windows Services task Start-WindowsServices { $Resources | ForEach-Object -Process { Write-Build DarkGreen $_.Path if ($_.StartupType -in ('Automatic', 'AutomaticDelayedStart') -and (Test-WindowsService -Name $_.Name)) { Start-Service -Name $_.Name } } } # Synopsis: Stop Windows Services task Stop-WindowsServices -If { -not $SkipUndeploy } { $Resources | ForEach-Object -Process { Write-Build DarkGreen $_.Path if (Test-WindowsService -Name $_.Name) { Stop-Service -Name $_.Name -Force } } } function Test-WindowsService { [CmdletBinding()] [OutputType([bool])] param ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $Name ) # https://stackoverflow.com/questions/4967496/check-if-a-windows-service-exists-and-delete-in-powershell [bool] (Get-CimInstance -ClassName Win32_Service -Filter "Name='$($_.Name)'") } # SIG # Begin signature block # MIIJEgYJKoZIhvcNAQcCoIIJAzCCCP8CAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUVAeW7mHgnGT5SNyivi23P+pM # W0ygggWhMIIFnTCCA1GgAwIBAgIQKBOAjgMDO55A7UJ/k/g5nTBBBgkqhkiG9w0B # AQowNKAPMA0GCWCGSAFlAwQCAQUAoRwwGgYJKoZIhvcNAQEIMA0GCWCGSAFlAwQC # AQUAogMCASAwJjEkMCIGA1UEAwwbaWNyYWZ0c29mdHdhcmVAc3RhdGVsZXNzLmJl # MB4XDTIwMDYyMzExNDM1NloXDTIxMDYyMzEyMDM1NlowJjEkMCIGA1UEAwwbaWNy # YWZ0c29mdHdhcmVAc3RhdGVsZXNzLmJlMIICIjANBgkqhkiG9w0BAQEFAAOCAg8A # MIICCgKCAgEAmQcb0GwlBHBHBJZ9vNM8EewN7T+nhsWVU0WBoWnIw6UAT99Rw9x5 # RcfOQU2hxqKmR1k+iI6B+qddpTC3VLSChA/mh1P4pCDDsZeyR/0nn/r/DezhDe8x # 5jckjR88KSRcgDoh0kLjgfrToDpx9EvBcwXmNJKDwBIWu5SBvk04beU4XO7OHjBo # g0kMaHxCZc9HcWfdzBefP+fbVzu6f1j1WgEqZn9sr1ML2ulHRdu26+56xGq9RZGJ # vXyY1mY+K5mqBcET+1bV2pZnBrM3Gc/hlmvTkwrC0ZGBALLZWZqqpLVrDCY5eoHP # w2C0kA4JzK4Q1o218s+wXbuDcjYRIZqBSwI8f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