|
| 1 | +$server = $env:COMPUTERNAME |
| 2 | +$sqlinstance= $server |
| 3 | +#find the actively running SQL Server services |
| 4 | +$SqlTaskList = Tasklist /SVC /FI "imagename eq sqlservr*" /FO CSV | ConvertFrom-Csv |
| 5 | +$SqlTaskList = $SqlTaskList | Select-Object PID, "Image name", Services |
| 6 | + |
| 7 | +$instnaceArray= @() |
| 8 | +foreach ($sqlinstance in $SqlTaskList.Services) |
| 9 | +{ |
| 10 | + #in the case of a default instance, just use MSSQLSERVER which is the instance name |
| 11 | + |
| 12 | + if ($sqlinstance.IndexOf("$") -lt 1) |
| 13 | + { |
| 14 | + $SqlInstance = $sqlinstance |
| 15 | + } |
| 16 | + |
| 17 | + #for named instance, strip the part after the "$" |
| 18 | + else |
| 19 | + { |
| 20 | + $SqlInstance = $server + "\" + $sqlinstance.Substring($sqlinstance.IndexOf("$") + 1) |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + #add each instance name to the array |
| 25 | + $instnaceArray+=$SqlInstance |
| 26 | +} |
| 27 | + |
| 28 | +Write-Host "" |
| 29 | +Write-Host "==============================================================================================================================" |
| 30 | +Write-Host "This script is designed to clean up SQL LogScout processes that may have been left behind if SQL LogScout was closed incorrectly`n" |
| 31 | +Write-Host "==============================================================================================================================" |
| 32 | +Write-Host "" |
| 33 | + |
| 34 | +#print out the instance names |
| 35 | + |
| 36 | +Write-Host "Discovered the following SQL Server instance(s)`n" |
| 37 | +Write-Host "" |
| 38 | +Write-Host "ID SQL Instance Name" |
| 39 | +Write-Host "-- ----------------" |
| 40 | +# sort the array by instance name |
| 41 | +$instnaceArray = $instnaceArray | Sort-Object |
| 42 | + |
| 43 | +for($i=0; $i -lt $instnaceArray.Count;$i++) |
| 44 | +{ |
| 45 | + Write-Host $i " " $instnaceArray[$i] |
| 46 | +} |
| 47 | + |
| 48 | +Write-Host "" |
| 49 | +$j = Read-Host "Please select the ID for SQL instance." |
| 50 | +$SelectedSQLinstnace = $instnaceArray[$j] |
| 51 | +$sql_instance_conn_str = $SelectedSQLinstnace |
| 52 | + |
| 53 | +$xevent_session = "xevent_SQLLogScout" |
| 54 | +$xevent_target_file = "xevent_LogScout_target" |
| 55 | +$xevent_alwayson_session = "SQLLogScout_AlwaysOn_Data_Movement" |
| 56 | + |
| 57 | +function HandleCatchBlock ([string] $function_name, [System.Management.Automation.ErrorRecord] $err_rec, [bool]$exit_logscout = $false) |
| 58 | +{ |
| 59 | + $error_msg = $err_rec.Exception.Message |
| 60 | + $error_linenum = $err_rec.InvocationInfo.ScriptLineNumber |
| 61 | + $error_offset = $err_rec.InvocationInfo.OffsetInLine |
| 62 | + $error_script = $err_rec.InvocationInfo.ScriptName |
| 63 | + Write-Host "Function '$function_name' failed with error: $error_msg (line: $error_linenum, offset: $error_offset, file: $error_script)" |
| 64 | + |
| 65 | + if ($exit_logscout) |
| 66 | + { |
| 67 | + Write-Host "Exiting CleanupIncomplete Shutdown script ..." |
| 68 | + exit |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +try |
| 73 | +{ |
| 74 | + Write-Host "" |
| 75 | + Write-Host "Launching cleanup routine... please wait" |
| 76 | + |
| 77 | + $query = " |
| 78 | + declare curSession |
| 79 | + CURSOR for select 'kill ' + cast( session_id as varchar(max)) from sys.dm_exec_sessions where host_name = 'sqllogscout' and program_name='SQLCMD' and session_id <> @@spid |
| 80 | + open curSession |
| 81 | + declare @sql varchar(max) |
| 82 | + fetch next from curSession into @sql |
| 83 | + while @@FETCH_STATUS = 0 |
| 84 | + begin |
| 85 | + exec (@sql) |
| 86 | + fetch next from curSession into @sql |
| 87 | + end |
| 88 | + close curSession; |
| 89 | + deallocate curSession; |
| 90 | + " |
| 91 | + |
| 92 | + $executable = "sqlcmd.exe" |
| 93 | + $argument_list ="-S" + $sql_instance_conn_str + " -E -Hsqllogscout_cleanup -w8000 -Q`""+ $query + "`" " |
| 94 | + Start-Process -FilePath $executable -ArgumentList $argument_list -WindowStyle Hidden |
| 95 | + |
| 96 | + Write-Host "Executing STOP_SQLLogScout_Xevent session. It will stop the Xevent trace in case it was found to be running" "..." |
| 97 | + $query = "IF HAS_PERMS_BY_NAME(NULL, NULL, 'ALTER ANY EVENT SESSION') = 1 BEGIN ALTER EVENT SESSION [$xevent_session] ON SERVER STATE = STOP; DROP EVENT SESSION [$xevent_session] ON SERVER; END" |
| 98 | + $executable = "sqlcmd.exe" |
| 99 | + $argument_list = "-S" + $server + " -E -w8000 -Q`"" + $query + "`"" |
| 100 | + Start-Process -FilePath $executable -ArgumentList $argument_list -WindowStyle Hidden |
| 101 | + |
| 102 | + |
| 103 | + $query = "IF HAS_PERMS_BY_NAME(NULL, NULL, 'ALTER ANY EVENT SESSION') = 1 BEGIN ALTER EVENT SESSION [$xevent_alwayson_session] ON SERVER STATE = STOP; DROP EVENT SESSION [$xevent_alwayson_session] ON SERVER; END" |
| 104 | + $executable = "sqlcmd.exe" |
| 105 | + $argument_list = "-S" + $server + " -E -w8000 -Q`"" + $query + "`"" |
| 106 | + Start-Process -FilePath $executable -ArgumentList $argument_list -WindowStyle Hidden |
| 107 | + |
| 108 | + $xevent_session = "xevent_SQLLogScout" |
| 109 | + $query = "ALTER EVENT SESSION [$xevent_session] ON SERVER STATE = STOP; DROP EVENT SESSION [$xevent_session] ON SERVER;" |
| 110 | + $executable = "sqlcmd.exe" |
| 111 | + $argument_list ="-S" + $sql_instance_conn_str + " -E -w8000 -Q`""+ $query + "`" " |
| 112 | + Start-Process -FilePath $executable -ArgumentList $argument_list -WindowStyle Hidden |
| 113 | + |
| 114 | + Write-Host "Executing STOP_SQLLogScout_AlwaysOn_Data_Movement. It will stop the Xevent trace in case it was found to be running" "..." |
| 115 | + $xevent_session = "SQLLogScout_AlwaysOn_Data_Movement" |
| 116 | + $query = "ALTER EVENT SESSION [$xevent_session] ON SERVER STATE = STOP; DROP EVENT SESSION [$xevent_session] ON SERVER;" |
| 117 | + $executable = "sqlcmd.exe" |
| 118 | + $argument_list ="-S" + $sql_instance_conn_str + " -E -w8000 -Q`""+ $query + "`" " |
| 119 | + Start-Process -FilePath $executable -ArgumentList $argument_list -WindowStyle Hidden |
| 120 | + |
| 121 | + $collector_name = "Disable_BackupRestore_Trace_Flags" |
| 122 | + Write-Host "Executing" $collector_name "It will disable the trace flags they were found to be enabled..." |
| 123 | + $query = "DBCC TRACEOFF(3004,3212,3605,-1)" |
| 124 | + $executable = "sqlcmd.exe" |
| 125 | + $argument_list ="-S" + $sql_instance_conn_str + " -E -w8000 -Q`""+ $query + "`" " |
| 126 | + Start-Process -FilePath $executable -ArgumentList $argument_list -WindowStyle Hidden |
| 127 | + |
| 128 | + |
| 129 | + $collector_name = "PerfmonStop" |
| 130 | + Write-Host "Executing $collector_name. It will stop Perfmon started by SQL LogScout in case it was found to be running ..." |
| 131 | + $argument_list = "/C logman stop logscoutperfmon & logman delete logscoutperfmon" |
| 132 | + $executable = "cmd.exe" |
| 133 | + Start-Process -FilePath $executable -ArgumentList $argument_list -WindowStyle Hidden |
| 134 | + |
| 135 | + $collector_name = "NettraceStop" |
| 136 | + Write-Host "Executing $collector_name. It will stop the network trace in case it was found to be running..." |
| 137 | + $argument_list = "/C title Stopping Network trace... & echo This process may take a few minutes. Do not close this window... & StopNetworkTrace.bat" |
| 138 | + $executable = "cmd.exe" |
| 139 | + Start-Process -FilePath $executable -ArgumentList $argument_list -WindowStyle Normal |
| 140 | + |
| 141 | + #---------------------- |
| 142 | + Write-Host "Executing WPR -cancel. This will stop all WPR traces in case any was found running..." |
| 143 | + $executable = "cmd.exe" |
| 144 | + $argument_list = $argument_list = "/C wpr.exe -cancel " |
| 145 | + Start-Process -FilePath $executable -ArgumentList $argument_list -WindowStyle Hidden |
| 146 | + #----------------------------- |
| 147 | + Write-Host "Executing STOP storport. It will stop a stoport trace if it was found to be running..." |
| 148 | + $argument_list = "/C logman stop ""storport"" -ets" |
| 149 | + $executable = "cmd.exe" |
| 150 | + Start-Process -FilePath $executable -ArgumentList $argument_list -WindowStyle Hidden |
| 151 | + #------------------------------------- |
| 152 | +} |
| 153 | +catch |
| 154 | +{ |
| 155 | + HandleCatchBlock -function_name $($MyInvocation.MyCommand) -err_rec $PSItem |
| 156 | +} |
| 157 | + |
| 158 | + |
0 commit comments