@@ -26,26 +26,29 @@ 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.InstanceSingle - Property ComputerName
30+
2931 # Enable xp_cmdshell for test process creation
3032 $null = Invoke-DbaQuery - SqlInstance $TestConfig.InstanceSingle - Query "
31- -- To allow advanced options to be changed.
32- EXECUTE sp_configure 'show advanced options', 1;
33- GO
34- -- To update the currently configured value for advanced options.
35- RECONFIGURE;
36- GO
37- -- To enable the feature.
38- EXECUTE sp_configure 'xp_cmdshell', 1;
39- GO
40- -- To update the currently configured value for this feature.
41- RECONFIGURE;
42- GO"
33+ -- To allow advanced options to be changed.
34+ EXECUTE sp_configure 'show advanced options', 1;
35+ GO
36+ -- To update the currently configured value for advanced options.
37+ RECONFIGURE;
38+ GO
39+ -- To enable the feature.
40+ EXECUTE sp_configure 'xp_cmdshell', 1;
41+ GO
42+ -- To update the currently configured value for this feature.
43+ RECONFIGURE;
44+ GO"
45+
46+ # Create sql file with code to start an external process
47+ $sqlFile = " $ ( $TestConfig.Temp ) \sleep.sql"
48+ Set-Content - Path $sqlFile - Value " xp_cmdshell 'powershell -command "" sleep 5"" '"
4349
44- # Create a test process to stop
45- $query = @"
46- xp_cmdshell 'powershell -command ""sleep 20""'
47- "@
48- Start-Process - FilePath sqlcmd - ArgumentList " -S $ ( $TestConfig.InstanceSingle ) -Q `" $query `" " - NoNewWindow - RedirectStandardOutput null
50+ # Run sql file to start external process
51+ Start-Process - FilePath sqlcmd - ArgumentList " -S $ ( $TestConfig.InstanceSingle ) -i $sqlFile " - NoNewWindow - RedirectStandardOutput null
4952
5053 # We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
5154 $PSDefaultParameterValues.Remove (" *-Dba*:EnableException" )
@@ -57,24 +60,29 @@ Describe $CommandName -Tag IntegrationTests {
5760
5861 # Disable xp_cmdshell after tests
5962 $null = Invoke-DbaQuery - SqlInstance $TestConfig.InstanceSingle - Query "
60- EXECUTE sp_configure 'xp_cmdshell', 0;
61- GO
62- RECONFIGURE;
63- GO
64- EXECUTE sp_configure 'show advanced options', 0;
65- GO
66- RECONFIGURE;
67- GO" - ErrorAction SilentlyContinue
63+ EXECUTE sp_configure 'xp_cmdshell', 0;
64+ GO
65+ RECONFIGURE;
66+ GO
67+ EXECUTE sp_configure 'show advanced options', 0;
68+ GO
69+ RECONFIGURE;
70+ GO"
71+
72+ # remove sql file
73+ Remove-Item - Path $sqlFile
6874
6975 $PSDefaultParameterValues.Remove (" *-Dba*:EnableException" )
7076 }
7177
7278 Context " Can stop an external process" {
7379 It " returns results" {
74- $results = Get-DbaExternalProcess - ComputerName localhost | Select-Object - First 1 | Stop-DbaExternalProcess
75- $results.ComputerName | Should - Be " localhost"
80+ Start-Sleep - Seconds 1
81+ $results = Get-DbaExternalProcess - ComputerName $computerName | Select-Object - First 1 | Stop-DbaExternalProcess
82+ Start-Sleep - Seconds 5
83+ $results.ComputerName | Should - Be $computerName
7684 $results.Name | Should - Be " cmd.exe"
77- $results.ProcessId | Should -Not - Be $null
85+ $results.ProcessId | Should -Not - BeNullOrEmpty
7886 $results.Status | Should - Be " Stopped"
7987 }
8088 }
0 commit comments