Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Get|Start|Stop|Restart]-DbaService - Add parameter SqlInstance #8159

Merged
merged 3 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions functions/Get-DbaService.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ function Get-DbaService {
Requires Local Admin rights on destination computer(s).

.PARAMETER ComputerName
The target SQL Server instance or instances.
The target computer(s).

.PARAMETER InstanceName
Only returns services that belong to the specific instances.
Only returns services that belong to the specific instances on all target computers.

.PARAMETER SqlInstance
Use a combination of computername and instancename to get the SQL Server related services for specific instances on specific computers.

Parameters ComputerName and InstanceName will be ignored if SqlInstance is used.

.PARAMETER Credential
Credential object used to connect to the computer as a different user.
Expand Down Expand Up @@ -66,6 +71,11 @@ function Get-DbaService {

Gets the SQL Server related services related to the default instance MSSQLSERVER on computers sql1 and sql2.

.EXAMPLE
PS C:\> Get-DbaService -SqlInstance sql1, sql1\test, sql2\test

Gets the SQL Server related services related to the default instance MSSQLSERVER on computers sql1, the named instances test on sql1 and sql2.

.EXAMPLE
PS C:\> Get-DbaService -ComputerName $MyServers -Type SSRS

Expand Down Expand Up @@ -97,6 +107,8 @@ function Get-DbaService {
[Parameter(ParameterSetName = "Search")]
[Alias("Instance")]
[string[]]$InstanceName,
[Parameter(ParameterSetName = "Search")]
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$Credential,
[Parameter(ParameterSetName = "Search")]
[ValidateSet("Agent", "Browser", "Engine", "FullText", "SSAS", "SSIS", "SSRS", "PolyBase", "Launchpad")]
Expand All @@ -108,6 +120,11 @@ function Get-DbaService {
)

begin {
if ($SqlInstance) {
# If SqlInstance is used, we select the list of computers for ComputerName
$ComputerName = $SqlInstance | Select-Object -ExpandProperty ComputerName -Unique
}

#Dictionary to transform service type IDs into the names from Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer.Services.Type
$ServiceIdMap = @(
@{ Name = "Engine"; Id = 1 },
Expand Down Expand Up @@ -147,6 +164,11 @@ function Get-DbaService {
}
process {
foreach ($computer in $ComputerName.ComputerName) {
if ($SqlInstance) {
# If SqlInstance is used, we select the list of instances for the current computer
$InstanceName = $SqlInstance | Where-Object ComputerName -eq $computer | Select-Object -ExpandProperty InstanceName
}

try {
$resolvedComputerName = (Resolve-DbaNetworkName -ComputerName $computer -Credential $Credential -EnableException).FullComputerName
$null = Get-DbaCmObject -ComputerName $resolvedComputerName -Credential $Credential -Namespace root\Microsoft -ClassName __NAMESPACE -EnableException
Expand Down
8 changes: 8 additions & 0 deletions functions/Restart-DbaService.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ function Restart-DbaService {
.PARAMETER InstanceName
Only affects services that belong to the specific instances.

.PARAMETER SqlInstance
Use a combination of computername and instancename to get the SQL Server related services for specific instances on specific computers.

Parameters ComputerName and InstanceName will be ignored if SqlInstance is used.

.PARAMETER Credential
Credential object used to connect to the computer as a different user.

Expand Down Expand Up @@ -87,6 +92,8 @@ function Restart-DbaService {
[DbaInstanceParameter[]]$ComputerName = $env:COMPUTERNAME,
[Alias("Instance")]
[string[]]$InstanceName,
[Parameter(ParameterSetName = "Server")]
[DbaInstanceParameter[]]$SqlInstance,
[ValidateSet("Agent", "Browser", "Engine", "FullText", "SSAS", "SSIS", "SSRS", "PolyBase", "Launchpad")]
[string[]]$Type,
[parameter(ValueFromPipeline, Mandatory, ParameterSetName = "Service")]
Expand All @@ -102,6 +109,7 @@ function Restart-DbaService {
if ($PsCmdlet.ParameterSetName -eq "Server") {
$serviceParams = @{ ComputerName = $ComputerName }
if ($InstanceName) { $serviceParams.InstanceName = $InstanceName }
if ($SqlInstance) { $serviceParams.SqlInstance = $SqlInstance }
if ($Type) { $serviceParams.Type = $Type }
if ($Credential) { $serviceParams.Credential = $Credential }
if ($EnableException) { $serviceParams.EnableException = $EnableException }
Expand Down
8 changes: 8 additions & 0 deletions functions/Start-DbaService.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ function Start-DbaService {
.PARAMETER InstanceName
Only affects services that belong to the specific instances.

.PARAMETER SqlInstance
Use a combination of computername and instancename to get the SQL Server related services for specific instances on specific computers.

Parameters ComputerName and InstanceName will be ignored if SqlInstance is used.

.PARAMETER Credential
Credential object used to connect to the computer as a different user.

Expand Down Expand Up @@ -79,6 +84,8 @@ function Start-DbaService {
[DbaInstanceParameter[]]$ComputerName = $env:COMPUTERNAME,
[Alias("Instance")]
[string[]]$InstanceName,
[Parameter(ParameterSetName = "Server")]
[DbaInstanceParameter[]]$SqlInstance,
[ValidateSet("Agent", "Browser", "Engine", "FullText", "SSAS", "SSIS", "SSRS")]
[string[]]$Type,
[parameter(ValueFromPipeline, Mandatory, ParameterSetName = "Service")]
Expand All @@ -93,6 +100,7 @@ function Start-DbaService {
if ($PsCmdlet.ParameterSetName -eq "Server") {
$serviceParams = @{ ComputerName = $ComputerName }
if ($InstanceName) { $serviceParams.InstanceName = $InstanceName }
if ($SqlInstance) { $serviceParams.SqlInstance = $SqlInstance }
if ($Type) { $serviceParams.Type = $Type }
if ($Credential) { $serviceParams.Credential = $Credential }
if ($EnableException) { $serviceParams.EnableException = $EnableException }
Expand Down
8 changes: 8 additions & 0 deletions functions/Stop-DbaService.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ function Stop-DbaService {
.PARAMETER InstanceName
Only affects services that belong to the specific instances.

.PARAMETER SqlInstance
Use a combination of computername and instancename to get the SQL Server related services for specific instances on specific computers.

Parameters ComputerName and InstanceName will be ignored if SqlInstance is used.

.PARAMETER Credential
Credential object used to connect to the computer as a different user.

Expand Down Expand Up @@ -87,6 +92,8 @@ function Stop-DbaService {
[DbaInstanceParameter[]]$ComputerName = $env:COMPUTERNAME,
[Alias("Instance")]
[string[]]$InstanceName,
[Parameter(ParameterSetName = "Server")]
[DbaInstanceParameter[]]$SqlInstance,
[ValidateSet("Agent", "Browser", "Engine", "FullText", "SSAS", "SSIS", "SSRS")]
[string[]]$Type,
[parameter(ValueFromPipeline, Mandatory, ParameterSetName = "Service")]
Expand All @@ -103,6 +110,7 @@ function Stop-DbaService {
if ($PsCmdlet.ParameterSetName -eq "Server") {
$serviceParams = @{ ComputerName = $ComputerName }
if ($InstanceName) { $serviceParams.InstanceName = $InstanceName }
if ($SqlInstance) { $serviceParams.SqlInstance = $SqlInstance }
if ($Type) { $serviceParams.Type = $Type }
if ($Credential) { $serviceParams.Credential = $Credential }
if ($EnableException) { $serviceParams.EnableException = $EnableException }
Expand Down
10 changes: 9 additions & 1 deletion tests/Get-DbaService.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'ComputerName', 'InstanceName', 'Credential', 'Type', 'ServiceName', 'AdvancedProperties', 'EnableException'
[object[]]$knownParameters = 'ComputerName', 'InstanceName', 'SqlInstance', 'Credential', 'Type', 'ServiceName', 'AdvancedProperties', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Expand Down Expand Up @@ -73,4 +73,12 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
$results.StartMode | Should Be 'Automatic'
}
}
Context "Command actually works with SqlInstance" {
$results = @( )
$results += Get-DbaService -SqlInstance $script:instance2 -Type Engine

It "shows exactly one service" {
$results.Count | Should Be 1
}
}
}
2 changes: 1 addition & 1 deletion tests/Restart-DbaService.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'ComputerName', 'InstanceName', 'Type', 'InputObject', 'Timeout', 'Credential', 'Force', 'EnableException'
[object[]]$knownParameters = 'ComputerName', 'InstanceName', 'SqlInstance', 'Type', 'InputObject', 'Timeout', 'Credential', 'Force', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Expand Down
2 changes: 1 addition & 1 deletion tests/Start-DbaService.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'ComputerName', 'InstanceName', 'Type', 'InputObject', 'Timeout', 'Credential', 'EnableException'
[object[]]$knownParameters = 'ComputerName', 'InstanceName', 'SqlInstance', 'Type', 'InputObject', 'Timeout', 'Credential', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Expand Down
2 changes: 1 addition & 1 deletion tests/Stop-DbaService.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'ComputerName', 'InstanceName', 'Type', 'InputObject', 'Timeout', 'Credential', 'Force', 'EnableException'
[object[]]$knownParameters = 'ComputerName', 'InstanceName', 'SqlInstance', 'Type', 'InputObject', 'Timeout', 'Credential', 'Force', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Expand Down
4 changes: 2 additions & 2 deletions tests/Test-DbaInstanceName.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') }
[object[]]$knownParameters = 'Blockers', 'ComputerName', 'InstanceName', 'NewServerName', 'RenameRequired', 'ServerName', 'SqlInstance', 'Updatable', 'Warnings'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'ExcludeSsrs', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0
Expand All @@ -21,7 +21,7 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
}

It "returns the correct properties" {
$ExpectedProps = 'ComputerName,ServerName,RenameRequired,Updatable,Warnings,Blockers,SqlInstance,InstanceName'.Split(',')
$ExpectedProps = 'ComputerName,InstanceName,SqlInstance,ServerName,NewServerName,RenameRequired,Updatable,Warnings,Blockers'.Split(',')
($results.PsObject.Properties.Name | Sort-Object) | Should -Be ($ExpectedProps | Sort-Object)
}
}
Expand Down