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
Remove-DbaLinkedServer: Add option to drop logins along with the linked server #7952
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = 'SqlInstance', 'SqlCredential', 'LinkedServer', 'InputObject', 'EnableException' | ||
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'LinkedServer', 'InputObject', '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 | ||
|
@@ -17,56 +17,88 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { | |
BeforeAll { | ||
$random = Get-Random | ||
$instance2 = Connect-DbaInstance -SqlInstance $script:instance2 | ||
$instance3 = Connect-DbaInstance -SqlInstance $script:instance3 | ||
|
||
$instance2.Query("EXEC sp_addlinkedserver @server=N'LS1_$random'") | ||
$instance2.Query("EXEC sp_addlinkedserver @server=N'LS2_$random'") | ||
$instance2.Query("EXEC sp_addlinkedserver @server=N'LS3_$random'") | ||
$linkedServerName1 = "dbatoolscli_LS1_$random" | ||
$linkedServerName2 = "dbatoolscli_LS2_$random" | ||
$linkedServerName3 = "dbatoolscli_LS3_$random" | ||
$linkedServerName4 = "dbatoolscli_LS4_$random" | ||
|
||
$ls1 = Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer "LS1_$random" | ||
$ls2 = Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer "LS2_$random" | ||
$ls3 = Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer "LS3_$random" | ||
$linkedServer1 = New-DbaLinkedServer -SqlInstance $instance2 -LinkedServer $linkedServerName1 | ||
$linkedServer2 = New-DbaLinkedServer -SqlInstance $instance2 -LinkedServer $linkedServerName2 | ||
$linkedServer3 = New-DbaLinkedServer -SqlInstance $instance2 -LinkedServer $linkedServerName3 | ||
$linkedServer4 = New-DbaLinkedServer -SqlInstance $instance2 -LinkedServer $linkedServerName4 | ||
|
||
$securePassword = ConvertTo-SecureString -String 's3cur3P4ssw0rd?' -AsPlainText -Force | ||
$loginName = "dbatoolscli_test_$random" | ||
New-DbaLogin -SqlInstance $instance2, $instance3 -Login $loginName -SecurePassword $securePassword | ||
|
||
$newLinkedServerLogin = New-Object Microsoft.SqlServer.Management.Smo.LinkedServerLogin | ||
$newLinkedServerLogin.Parent = $linkedServer4 | ||
$newLinkedServerLogin.Name = $loginName | ||
$newLinkedServerLogin.RemoteUser = $loginName | ||
$newLinkedServerLogin.SetRemotePassword(($securePassword | ConvertFrom-SecurePass)) | ||
$newLinkedServerLogin.Create() | ||
} | ||
AfterAll { | ||
if ($instance2.LinkedServers.Name -contains "LS1_$random") { | ||
$instance2.LinkedServers["LS1_$random"].Drop() | ||
if ($instance2.LinkedServers.Name -contains $linkedServerName1) { | ||
$instance2.LinkedServers[$linkedServerName1].Drop() | ||
} | ||
|
||
if ($instance2.LinkedServers.Name -contains $linkedServerName2) { | ||
$instance2.LinkedServers[$linkedServerName2].Drop() | ||
} | ||
|
||
if ($instance2.LinkedServers.Name -contains "LS2_$random") { | ||
$instance2.LinkedServers["LS2_$random"].Drop() | ||
if ($instance2.LinkedServers.Name -contains $linkedServerName3) { | ||
$instance2.LinkedServers[$linkedServerName3].Drop() | ||
} | ||
|
||
if ($instance2.LinkedServers.Name -contains "LS3_$random") { | ||
$instance2.LinkedServers["LS3_$random"].Drop() | ||
if ($instance2.LinkedServers.Name -contains $linkedServerName4) { | ||
$instance2.LinkedServers[$linkedServerName4].Drop($true) | ||
} | ||
|
||
Remove-DbaLogin -SqlInstance $instance2, $instance3 -Login $loginName -Confirm:$false | ||
} | ||
|
||
Context "ensure command works" { | ||
|
||
It "Removes a linked server" { | ||
$results = Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer "LS1_$random" | ||
$results = Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer $linkedServerName1 | ||
$results.Length | Should -Be 1 | ||
Remove-DbaLinkedServer -SqlInstance $script:instance2 -LinkedServer "LS1_$random" -Confirm:$false | ||
$results = Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer "LS1_$random" | ||
Remove-DbaLinkedServer -SqlInstance $script:instance2 -LinkedServer $linkedServerName1 -Confirm:$false | ||
$results = Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer $linkedServerName1 | ||
$results | Should -BeNullOrEmpty | ||
} | ||
|
||
It "Tries to remove a non-existent linked server" { | ||
Remove-DbaLinkedServer -SqlInstance $script:instance2 -LinkedServer "LS1_$random" -Confirm:$false -WarningVariable warnings | ||
$warnings | Should -BeLike "*Linked server LS1_$random does not exist on $($instance2.Name)" | ||
Remove-DbaLinkedServer -SqlInstance $script:instance2 -LinkedServer $linkedServerName1 -Confirm:$false -WarningVariable warnings | ||
$warnings | Should -BeLike "*Linked server $linkedServerName1 does not exist on $($instance2.Name)" | ||
} | ||
|
||
It "Removes a linked server using a server from a pipeline and a linked server from a pipeline" { | ||
$results = Get-DbaLinkedServer -SqlInstance $script:instance2 -LinkedServer "LS2_$random" | ||
It "Removes a linked server passed in via pipeline" { | ||
$results = Get-DbaLinkedServer -SqlInstance $script:instance2 -LinkedServer $linkedServerName2 | ||
$results.Length | Should -Be 1 | ||
Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer "LS2_$random" | Remove-DbaLinkedServer -WarningVariable warn -Confirm:$false | ||
$warn | Should -BeNullOrEmpty | ||
$results = Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer "LS2_$random" | ||
Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer $linkedServerName2 | Remove-DbaLinkedServer -Confirm:$false | ||
$results = Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer $linkedServerName2 | ||
$results | Should -BeNullOrEmpty | ||
} | ||
|
||
$results = Get-DbaLinkedServer -SqlInstance $script:instance2 -LinkedServer "LS3_$random" | ||
It "Removes a linked server using a server passed in via pipeline" { | ||
$results = Get-DbaLinkedServer -SqlInstance $script:instance2 -LinkedServer $linkedServerName3 | ||
$results.Length | Should -Be 1 | ||
$instance2 | Remove-DbaLinkedServer -LinkedServer "LS3_$random" -Confirm:$false | ||
$results = Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer "LS3_$random" | ||
$instance2 | Remove-DbaLinkedServer -LinkedServer $linkedServerName3 -Confirm:$false | ||
$results = Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer $linkedServerName3 | ||
$results | Should -BeNullOrEmpty | ||
} | ||
|
||
It "Tries to remove a linked server that still has logins" { | ||
Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer $linkedServerName4 | Remove-DbaLinkedServer -Confirm:$false -WarningVariable warnings | ||
$warnings | Should -BeLike "*There are still remote logins or linked logins for the server*" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be removed, or put it a unique it block for testing the warning message. If this fails it will appear in the results that the use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved this into its own test. |
||
} | ||
|
||
It "Removes a linked server that requires the -Force param" { | ||
Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer $linkedServerName4 | Remove-DbaLinkedServer -Confirm:$false -Force | ||
$results = Get-DbaLinkedServer -SqlInstance $instance2 -LinkedServer $linkedServerName4 | ||
$results | Should -BeNullOrEmpty | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems a bit excessive to make so many calls in one It block. Is there anything wrong with just having it test the below line. Not sure why we need to perform this test twice either (piping the command or the object via a variable is the same thing).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal is to test both of the input object types. I split this into two separate tests.