Skip to content

Commit 2d4f98a

Browse files
committed
Enhance integration tests by adding session termination for low-privilege login before removal
1 parent f457b87 commit 2d4f98a

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

tests/Integration/Commands/Restore-SqlDscDatabase.Integration.Tests.ps1

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,9 +1023,6 @@ WITH NOINIT, NOSKIP, REWIND, NOUNLOAD, STATS = 10;
10231023

10241024
# Create the login
10251025
$script:lowPrivLoginObject = New-SqlDscLogin -ServerObject $script:serverObject -Name $script:lowPrivLoginName -SqlLogin -SecurePassword $script:lowPrivPassword -PassThru -Force -ErrorAction 'Stop'
1026-
1027-
# Grant VIEW ANY DATABASE permission
1028-
$null = Grant-SqlDscServerPermission -Login $script:lowPrivLoginObject -Permission ViewAnyDatabase -Force -ErrorAction 'Stop'
10291026
}
10301027

10311028
BeforeEach {
@@ -1047,6 +1044,21 @@ WITH NOINIT, NOSKIP, REWIND, NOUNLOAD, STATS = 10;
10471044

10481045
if ($loginObject)
10491046
{
1047+
# Kill any active sessions for this login before dropping using SMO
1048+
$processes = $script:serverObject.EnumProcesses($script:lowPrivLoginName)
1049+
1050+
foreach ($process in $processes)
1051+
{
1052+
try
1053+
{
1054+
$script:serverObject.KillProcess($process.Spid)
1055+
}
1056+
catch
1057+
{
1058+
# Ignore errors if process already terminated
1059+
}
1060+
}
1061+
10501062
$null = Remove-SqlDscLogin -LoginObject $loginObject -Force -ErrorAction 'SilentlyContinue'
10511063
}
10521064
}

0 commit comments

Comments
 (0)