functions/Stop-DbaSqlService.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function Stop-DbaSqlService {
    <#
    .SYNOPSIS
    Stops SQL Server services on a computer.
 
    .DESCRIPTION
    Stops the SQL Server related services on one or more computers. Will follow SQL Server service dependencies.
 
    Requires Local Admin rights on destination computer(s).
 
    .PARAMETER ComputerName
    The SQL Server (or server in general) that you're connecting to. This command handles named instances.
 
    .PARAMETER InstanceName
    Only affects services that belong to the specific instances.
 
    .PARAMETER Credential
    Credential object used to connect to the computer as a different user.
 
    .PARAMETER Type
    Use -Type to collect only services of the desired SqlServiceType.
    Can be one of the following: "Agent","Browser","Engine","FullText","SSAS","SSIS","SSRS"
 
    .PARAMETER Timeout
    How long to wait for the start/stop request completion before moving on. Specify 0 to wait indefinitely.
 
    .PARAMETER ServiceCollection
    A collection of services from Get-DbaSqlService
 
    .PARAMETER Force
    Use this switch to stop dependent services before proceeding with the specified service
 
    .PARAMETER EnableException
    By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
    This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
    Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
 
    .PARAMETER WhatIf
    Shows what would happen if the cmdlet runs. The cmdlet is not run.
 
    .PARAMETER Confirm
    Prompts you for confirmation before running the cmdlet.
 
    .PARAMETER Force
    Will stop dependent SQL Server agents when stopping Engine services.
 
    .NOTES
    Author: Kirill Kravtsov( @nvarscar )
    Tags:
    dbatools PowerShell module (https://dbatools.io)
    Copyright (C) 2017 Chrissy LeMaire
    License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
 
    .LINK
    https://dbatools.io/Stop-DbaSqlService
 
    .EXAMPLE
    Stop-DbaSqlService -ComputerName sqlserver2014a
 
    Stops the SQL Server related services on computer sqlserver2014a.
 
    .EXAMPLE
    'sql1','sql2','sql3'| Get-DbaSqlService | Stop-DbaSqlService
 
    Gets the SQL Server related services on computers sql1, sql2 and sql3 and stops them.
 
    .EXAMPLE
    Stop-DbaSqlService -ComputerName sql1,sql2 -Instance MSSQLSERVER
 
    Stops the SQL Server services related to the default instance MSSQLSERVER on computers sql1 and sql2.
 
    .EXAMPLE
    Stop-DbaSqlService -ComputerName $MyServers -Type SSRS
 
    Stops the SQL Server related services of type "SSRS" (Reporting Services) on computers in the variable MyServers.
 
    .EXAMPLE
    Stop-DbaSqlService -ComputerName sql1 -Type Engine -Force
 
    Stops SQL Server database engine services on sql1 forcing dependent SQL Server Agent services to stop as well.
 
#>

    [CmdletBinding(DefaultParameterSetName = "Server", SupportsShouldProcess = $true)]
    Param (
        [Parameter(ParameterSetName = "Server", Position = 1)]
        [Alias("cn", "host", "Server")]
        [DbaInstanceParameter[]]$ComputerName = $env:COMPUTERNAME,
        [Alias("Instance")]
        [string[]]$InstanceName,
        [ValidateSet("Agent", "Browser", "Engine", "FullText", "SSAS", "SSIS", "SSRS")]
        [string[]]$Type,
        [parameter(ValueFromPipeline = $true, Mandatory = $true, ParameterSetName = "Service")]
        [object[]]$ServiceCollection,
        [int]$Timeout = 30,
        [PSCredential]$Credential,
        [switch]$Force,
        [switch][Alias('Silent')]$EnableException
    )
    begin {
        $processArray = @()
        if ($PsCmdlet.ParameterSetName -eq "Server") {
            $serviceParams = @{ ComputerName = $ComputerName }
            if ($InstanceName) { $serviceParams.InstanceName = $InstanceName }
            if ($Type) { $serviceParams.Type = $Type }
            if ($Credential) { $serviceParams.Credential = $Credential }
            if ($EnableException) { $serviceParams.Silent = $EnableException }
            $serviceCollection = Get-DbaSqlService @serviceParams
        }
    }
    process {
        #Get all the objects from the pipeline before proceeding
        $processArray += $serviceCollection
    }
    end {
        $processArray = [array]($processArray | Where-Object { (!$InstanceName -or $_.InstanceName -in $InstanceName) -and (!$Type -or $_.ServiceType -in $Type) })
        foreach ($service in $processArray) {
            if ($Force -and $service.ServiceType -eq 'Engine' -and !($processArray | Where-Object { $_.ServiceType -eq 'Agent' -and $_.InstanceName -eq $service.InstanceName -and $_.ComputerName -eq $service.ComputerName })) {
                #Construct parameters to call Get-DbaSqlService
                $serviceParams = @{
                    ComputerName = $service.ComputerName
                    InstanceName = $service.InstanceName
                    Type         = 'Agent'
                }
                if ($Credential) { $serviceParams.Credential = $Credential }
                if ($EnableException) { $serviceParams.Silent = $EnableException }
                $processArray += @(Get-DbaSqlService @serviceParams)
            }
        }
        if ($processArray) {
            Update-ServiceStatus -ServiceCollection $processArray -Action 'stop' -Timeout $Timeout -EnableException $EnableException
        }
        else { Stop-Function -EnableException $EnableException -Message "No SQL Server services found with current parameters." -Category ObjectNotFound }
    }
}