Skip to content

Commit 92868d9

Browse files
Added StopAfter functionality
Added StopAfter parameter and functionality.
1 parent 17529ef commit 92868d9

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

SQLTrace/SQLTrace.ps1

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ param
3030
[Parameter(ParameterSetName = 'Start', Mandatory=$true)]
3131
[switch] $Start,
3232

33-
# [Parameter(ParameterSetName = 'Start', Mandatory=$false)]
34-
# [int] $StopAfter = [int]::Parse("0"),
33+
[Parameter(ParameterSetName = 'Start', Mandatory=$false)]
34+
[int] $StopAfter = [int]::Parse("0"),
3535

3636
[Parameter(ParameterSetName = 'Stop', Mandatory=$true)]
3737
[switch] $Stop,
@@ -61,6 +61,7 @@ param
6161

6262
$global:EventSourceName = "MSSQL-SQLTrace" # For logging to the Application Event log
6363
$global:INISettings = $null # set in ReadINIFile
64+
$global:StopAfterMinutes = 0 # set in StartTraces
6465

6566
$PathsToClean = @{} # for DeleteOldFiles
6667

@@ -95,7 +96,7 @@ LogRaw "
9596
/_______ /\_____\ \_/|_______ \|____| |__| (____ / \___ >\___ >
9697
\/ \__> \/ \/ \/ \/
9798
98-
SQLTrace.ps1 version 1.0.0152.0
99+
SQLTrace.ps1 version 1.0.0167.0
99100
by the Microsoft SQL Server Networking Team
100101
"
101102

@@ -140,7 +141,7 @@ Usage:
140141
141142
.\SQLTrace.ps1 -Help
142143
.\SQLTrace.ps1 -Setup [-INIFile SQLTrace.ini]
143-
.\SQLTrace.ps1 -Start [-INIFile SQLTrace.ini] [-LogFolder folderpath]
144+
.\SQLTrace.ps1 -Start [-INIFile SQLTrace.ini] [-LogFolder folderpath] [-StopAfter minutes]
144145
.\SQLTrace.ps1 -Stop [-INIFile SQLTrace.ini]
145146
.\SQLTrace.ps1 -Cleanup [-INIFile SQLTrace.ini]
146147
"
@@ -418,6 +419,24 @@ Function StartTraces
418419

419420
LogInfo "Traces have started..."
420421
Write-EventLog -LogName Application -Source $global:EventSourceName -EventID 3002 -Message "SQLTrace has started."
422+
423+
# StopAfter logic
424+
425+
if ($global:StopAfter -gt 0)
426+
{
427+
$global:StopAfterMinutes = $global:StopAfter
428+
while ($global:StopAfterMinutes -gt 0)
429+
{
430+
LogInfo "The trace will stop after $($global:StopAfterMinutes) minutes."
431+
Start-Sleep -Seconds 60
432+
$global:StopAfterMinutes--
433+
}
434+
StopTraces
435+
}
436+
else
437+
{
438+
LogInfo "The trace will run until manually terminated with: .\SqlTrace.ps1 -stop"
439+
}
421440
}
422441

423442
Function FlushExistingTraces

0 commit comments

Comments
 (0)