@@ -26,10 +26,10 @@ Describe $CommandName -Tag IntegrationTests {
2626 # We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
2727 $PSDefaultParameterValues [" *-Dba*:EnableException" ] = $true
2828
29- $computerName = Resolve-DbaComputerName - ComputerName $TestConfig.InstanceRestart - Property ComputerName
29+ $computerName = Resolve-DbaComputerName - ComputerName $TestConfig.InstanceSingle - Property ComputerName
3030
31- # Enable xp_cmdshell for test process creation
32- $null = Invoke-DbaQuery - SqlInstance $TestConfig.InstanceRestart - Query "
31+ # Setup xp_cmdshell to create external processes for testing
32+ $null = Invoke-DbaQuery - SqlInstance $TestConfig.InstanceSingle - Query "
3333 -- To allow advanced options to be changed.
3434 EXECUTE sp_configure 'show advanced options', 1;
3535 GO
@@ -48,7 +48,7 @@ Describe $CommandName -Tag IntegrationTests {
4848 Set-Content - Path $sqlFile - Value " xp_cmdshell 'powershell -command "" sleep 5"" '"
4949
5050 # Run sql file to start external process
51- Start-Process - FilePath sqlcmd - ArgumentList " -S $ ( $TestConfig.InstanceRestart ) -i $sqlFile " - NoNewWindow - RedirectStandardOutput null
51+ Start-Process - FilePath sqlcmd - ArgumentList " -S $ ( $TestConfig.InstanceSingle ) -i $sqlFile " - NoNewWindow - RedirectStandardOutput null
5252
5353 # We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
5454 $PSDefaultParameterValues.Remove (" *-Dba*:EnableException" )
@@ -58,8 +58,8 @@ Describe $CommandName -Tag IntegrationTests {
5858 # We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
5959 $PSDefaultParameterValues [" *-Dba*:EnableException" ] = $true
6060
61- # Disable xp_cmdshell after tests
62- $null = Invoke-DbaQuery - SqlInstance $TestConfig.InstanceRestart - Query "
61+ # Cleanup: Disable xp_cmdshell for security
62+ $null = Invoke-DbaQuery - SqlInstance $TestConfig.InstanceSingle - Query "
6363 EXECUTE sp_configure 'xp_cmdshell', 0;
6464 GO
6565 RECONFIGURE;
@@ -69,20 +69,26 @@ Describe $CommandName -Tag IntegrationTests {
6969 RECONFIGURE;
7070 GO"
7171
72- # We need to restart the instance to ensure that the xp_cmdshell process has ended before we can remove the sql file.
73- $null = Restart-DbaService - SqlInstance $TestConfig.InstanceRestart - Type Engine - Force
74-
75- # Remove sql file
76- Remove-Item - Path $sqlFile
72+ 1 .. 20 | ForEach-Object {
73+ try {
74+ Remove-Item - Path $sqlFile
75+ break
76+ } catch {
77+ Start-Sleep - Seconds 1
78+ }
79+ }
80+ if (Test-Path - Path $sqlFile ) {
81+ Write-Warning - Message " Could not delete temporary SQL file at $sqlFile . Please check and delete manually."
82+ }
7783
7884 $PSDefaultParameterValues.Remove (" *-Dba*:EnableException" )
7985 }
8086
8187 Context " Can stop an external process" {
8288 It " returns results" {
8389 1 .. 10 | ForEach-Object {
84- $results = Get-DbaExternalProcess - ComputerName $computerName | Where-Object Name -eq " cmd.exe " | Stop-DbaExternalProcess
85- if ($results ) { return }
90+ $results = Get-DbaExternalProcess - ComputerName $computerName | Stop-DbaExternalProcess
91+ if ($results ) { break }
8692 Start-Sleep - Milliseconds 500
8793 }
8894 $results.ComputerName | Should - Be $computerName
0 commit comments