Skip to content

Commit 56aaa5d

Browse files
author
Ajay Dwivedi
committed
Updating Scripts
Updating Scripts
1 parent fa5b6a6 commit 56aaa5d

18 files changed

+633
-57
lines changed
-1 KB
Binary file not shown.

LogShipping/LogShipping.ssmssqlproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@
164164
<FullPath>v1.3 - [usp_GetLogWalkJobHistoryAlert_Suppress].sql</FullPath>
165165
</FileNode>
166166
<FileNode Name="v1.4 - [usp_GetLogWalkJobHistoryAlert_Suppress].sql">
167-
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:TUL1CIPRDB1:True</AssociatedConnectionMoniker>
168-
<AssociatedConnSrvName>TUL1CIPRDB1</AssociatedConnSrvName>
167+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:TUL1CIPXDB20:True</AssociatedConnectionMoniker>
168+
<AssociatedConnSrvName>TUL1CIPXDB20</AssociatedConnSrvName>
169169
<AssociatedConnUserName />
170170
<FullPath>v1.4 - [usp_GetLogWalkJobHistoryAlert_Suppress].sql</FullPath>
171171
</FileNode>

LogShipping/v1.4 - [usp_GetLogWalkJobHistoryAlert_Suppress].sql

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ BEGIN
3131
29-Apr-2019 - Add logic to send Blocking info to Slack Email
3232
13-May-2019 - Modify the Blocking Mail Query with procedure DBA.dbo.usp_WhoIsActive_Blocking
3333
20-Jun-2019 - @p_PerformAutoExecutionOfLogWalkJob - Add logic to run Log Walk Job if last execution was a failure due to blocking issue, and there are no Blockers
34+
27-Jul-2019 - Adding JobSchedule & NextRunTime in Mailer Output
3435
*/
3536
SET NOCOUNT ON;
3637

@@ -51,6 +52,8 @@ BEGIN
5152
DECLARE @IsBlockingIssue BIT;
5253
DECLARE @AreInDirectConnections BIT;
5354
DECLARE @_SendMailRequired BIT;
55+
DECLARE @JobSchedule varchar(255),
56+
@NextRunTime datetime;
5457
DECLARE @_mailSubject VARCHAR(255)
5558
,@_mailBody VARCHAR(4000);
5659
IF OBJECT_ID('DBA..LogWalkThresholdInstance') IS NULL
@@ -273,6 +276,90 @@ BEGIN
273276
IF @p_Verbose = 1
274277
SELECT [@_collection_time_start] = @_collection_time_start, [@_collection_time_end] = @_collection_time_end;
275278

279+
-- Find job schedule & NextRunTime
280+
;WITH T_Schedules AS
281+
(
282+
select S.name AS JobName,
283+
SS.name AS ScheduleName,
284+
CASE(SS.freq_type)
285+
WHEN 1 THEN 'Once'
286+
WHEN 4 THEN 'Daily'
287+
WHEN 8 THEN (case when (SS.freq_recurrence_factor > 1) then 'Every ' + convert(varchar(3),SS.freq_recurrence_factor) + ' Weeks' else 'Weekly' end)
288+
WHEN 16 THEN (case when (SS.freq_recurrence_factor > 1) then 'Every ' + convert(varchar(3),SS.freq_recurrence_factor) + ' Months' else 'Monthly' end)
289+
WHEN 32 THEN 'Every ' + convert(varchar(3),SS.freq_recurrence_factor) + ' Months' -- RELATIVE
290+
WHEN 64 THEN 'SQL Startup'
291+
WHEN 128 THEN 'SQL Idle'
292+
ELSE '??'
293+
END AS Frequency,
294+
CASE
295+
WHEN (freq_type = 1) then 'One time only'
296+
WHEN (freq_type = 4 and freq_interval = 1) then 'Every Day'
297+
WHEN (freq_type = 4 and freq_interval > 1) then 'Every ' + convert(varchar(10),freq_interval) + ' Days'
298+
WHEN (freq_type = 8) then (select 'Weekly Schedule' = MIN(D1+ D2+D3+D4+D5+D6+D7 )
299+
from (select SS.schedule_id,
300+
freq_interval,
301+
'D1' = CASE WHEN (freq_interval & 1 <> 0) then 'Sun ' ELSE '' END,
302+
'D2' = CASE WHEN (freq_interval & 2 <> 0) then 'Mon ' ELSE '' END,
303+
'D3' = CASE WHEN (freq_interval & 4 <> 0) then 'Tue ' ELSE '' END,
304+
'D4' = CASE WHEN (freq_interval & 8 <> 0) then 'Wed ' ELSE '' END,
305+
'D5' = CASE WHEN (freq_interval & 16 <> 0) then 'Thu ' ELSE '' END,
306+
'D6' = CASE WHEN (freq_interval & 32 <> 0) then 'Fri ' ELSE '' END,
307+
'D7' = CASE WHEN (freq_interval & 64 <> 0) then 'Sat ' ELSE '' END
308+
from msdb..sysschedules ss
309+
where freq_type = 8
310+
) as F
311+
where schedule_id = SJ.schedule_id
312+
)
313+
WHEN (freq_type = 16) then 'Day ' + convert(varchar(2),freq_interval)
314+
WHEN (freq_type = 32) then (select freq_rel + WDAY
315+
from (select SS.schedule_id,
316+
'freq_rel' = CASE(freq_relative_interval)
317+
WHEN 1 then 'First'
318+
WHEN 2 then 'Second'
319+
WHEN 4 then 'Third'
320+
WHEN 8 then 'Fourth'
321+
WHEN 16 then 'Last'
322+
ELSE '??'
323+
END,
324+
'WDAY' = CASE (freq_interval)
325+
WHEN 1 then ' Sun'
326+
WHEN 2 then ' Mon'
327+
WHEN 3 then ' Tue'
328+
WHEN 4 then ' Wed'
329+
WHEN 5 then ' Thu'
330+
WHEN 6 then ' Fri'
331+
WHEN 7 then ' Sat'
332+
WHEN 8 then ' Day'
333+
WHEN 9 then ' Weekday'
334+
WHEN 10 then ' Weekend'
335+
ELSE '??'
336+
END
337+
from msdb..sysschedules SS
338+
where SS.freq_type = 32
339+
) as WS
340+
where WS.schedule_id = SS.schedule_id
341+
)
342+
END AS Interval,
343+
CASE (freq_subday_type)
344+
WHEN 1 then left(stuff((stuff((replicate('0', 6 - len(active_start_time)))+ convert(varchar(6),active_start_time),3,0,':')),6,0,':'),8)
345+
WHEN 2 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' seconds'
346+
WHEN 4 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' minutes'
347+
WHEN 8 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' hours'
348+
ELSE '??'
349+
END AS [Time],
350+
CASE SJ.next_run_date
351+
WHEN 0 THEN cast('n/a' as char(10))
352+
ELSE convert(char(10), convert(datetime, convert(char(8),SJ.next_run_date)),120) + ' ' + left(stuff((stuff((replicate('0', 6 - len(next_run_time)))+ convert(varchar(6),next_run_time),3,0,':')),6,0,':'),8)
353+
END AS NextRunTime
354+
from msdb.dbo.sysjobs S
355+
left join msdb.dbo.sysjobschedules SJ on S.job_id = SJ.job_id
356+
left join msdb.dbo.sysschedules SS on SS.schedule_id = SJ.schedule_id
357+
where s.name = @p_JobName
358+
)
359+
SELECT @JobSchedule = Frequency + ' (' + Interval + ') - ' + [Time]
360+
,@NextRunTime = NextRunTime
361+
FROM T_Schedules;
362+
276363
-- Find Job Session along with its Blockers
277364
IF OBJECT_ID('tempdb..#JobSessionBlockers') IS NOT NULL
278365
DROP TABLE #JobSessionBlockers;
@@ -441,6 +528,8 @@ SQL Agent Job '+QUOTENAME(@p_JobName)+' has been failing for '+cast(@NoOfContino
441528
LAST JOB RUN: '+CAST(jh.RunDateTime AS varchar(50))+'
442529
DURATION: '+CAST(jh.RunDurationMinutes AS varchar(10))+' Minutes
443530
STATUS: Failed
531+
SCHEDULE: '+ISNULL(@JobSchedule,'')+'
532+
NextRunTime: '+ISNULL(CAST(@NextRunTime AS VARCHAR(40)),'')+'
444533
MESSAGES: Job '+QUOTENAME(@p_JobName)+' COULD NOT obtain EXCLUSIVE access of underlying database to start its activity.
445534
RCA: Kindly execute below query to find out details of Blockers.
446535
@@ -541,6 +630,8 @@ SQL Agent Job '+QUOTENAME(@p_JobName)+' has been failing for '+cast(@NoOfContino
541630
LAST JOB RUN: '+CAST(jh.RunDateTime AS varchar(50))+'
542631
DURATION: '+CAST(jh.RunDurationMinutes AS varchar(10))+' Minutes
543632
STATUS: Failed
633+
SCHEDULE: '+ISNULL(@JobSchedule,'')+'
634+
NextRunTime: '+ISNULL(CAST(@NextRunTime AS VARCHAR(40)),'')+'
544635
545636
Kindly check Job Step Error Message'
546637
FROM @T_JobHistory as jh

Misscellaneous Queries/Misscellaneous Queries.ssmssqlproj

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,30 @@
7575
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
7676
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
7777
</ConnectionNode>
78+
<ConnectionNode Name="sqlcluster\sql2017:sa">
79+
<Created>2019-07-13T17:49:54.7905871+05:30</Created>
80+
<Type>SQL</Type>
81+
<Server>sqlcluster\sql2017</Server>
82+
<UserName>sa</UserName>
83+
<Authentication>SQL</Authentication>
84+
<InitialDB />
85+
<LoginTimeout>30</LoginTimeout>
86+
<ExecutionTimeout>0</ExecutionTimeout>
87+
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
88+
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
89+
</ConnectionNode>
90+
<ConnectionNode Name="SqlCluster2016:sa">
91+
<Created>2019-07-15T12:28:55.7283251+05:30</Created>
92+
<Type>SQL</Type>
93+
<Server>SqlCluster2016</Server>
94+
<UserName>sa</UserName>
95+
<Authentication>SQL</Authentication>
96+
<InitialDB />
97+
<LoginTimeout>30</LoginTimeout>
98+
<ExecutionTimeout>0</ExecutionTimeout>
99+
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
100+
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
101+
</ConnectionNode>
78102
<ConnectionNode Name="tul1cipcnpdb1:CORPORATE\adwivedi">
79103
<Created>2019-04-21T22:12:15.4400118+05:30</Created>
80104
<Type>SQL</Type>
@@ -247,6 +271,24 @@
247271
<AssociatedConnUserName />
248272
<FullPath>SQL Server Error Logs.sql</FullPath>
249273
</FileNode>
274+
<FileNode Name="SqlCluster-Troubleshooting.sql">
275+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:localhost:True</AssociatedConnectionMoniker>
276+
<AssociatedConnSrvName>localhost</AssociatedConnSrvName>
277+
<AssociatedConnUserName />
278+
<FullPath>SqlCluster-Troubleshooting.sql</FullPath>
279+
</FileNode>
280+
<FileNode Name="Sql-Installation-Hosts.sql">
281+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:localhost:True</AssociatedConnectionMoniker>
282+
<AssociatedConnSrvName>localhost</AssociatedConnSrvName>
283+
<AssociatedConnUserName />
284+
<FullPath>Sql-Installation-Hosts.sql</FullPath>
285+
</FileNode>
286+
<FileNode Name="Sql-Installation-Powershell-Codes.sql">
287+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:localhost:True</AssociatedConnectionMoniker>
288+
<AssociatedConnSrvName>localhost</AssociatedConnSrvName>
289+
<AssociatedConnUserName />
290+
<FullPath>Sql-Installation-Powershell-Codes.sql</FullPath>
291+
</FileNode>
250292
<FileNode Name="SSMS Tricks 01.sql">
251293
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:BAN-1ADWIVEDI-L:True</AssociatedConnectionMoniker>
252294
<AssociatedConnSrvName>BAN-1ADWIVEDI-L</AssociatedConnSrvName>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
10.10.10.10 dc
2+
10.10.10.10 dc.contso.com
3+
10.10.10.20 sql-a
4+
10.10.10.20 sql-a.contso.com
5+
10.10.10.10 dc
6+
10.10.10.10 dc.contso.com
7+
10.10.10.11 BAN-2AJAYDWI-LT
8+
10.10.10.11 host
9+
10.10.10.11 BAN-2AJAYDWI-LT.contso.com
10+
10.10.10.21 tsqlprd01
11+
10.10.10.21 tsqlprd01.contso.com
12+
10.10.10.22 tsqlprd02
13+
10.10.10.22 tsqlprd02.contso.com
14+
10.10.10.23 tsqlprd03
15+
10.10.10.23 tsqlprd03.contso.com
16+
10.10.10.24 sqlclusteradmin
17+
10.10.10.24 sqlclusteradmin.contso.com
18+
10.10.10.25 sqlcluster2016
19+
10.10.10.25 sqlcluster2016.contso.com
20+
10.10.10.26 sqlcluster
21+
10.10.10.26 sqlcluster.contso.com
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Install-WindowsFeature Net-Framework-Core -source D:\Sources\SxS\
2+
3+
netsh advfirewall firewall add rule name="Microsoft iSCSI Software Target Service-TCP-3260" dir=in action=allow protocol=TCP localport=3260
4+
netsh advfirewall firewall add rule name="Microsoft iSCSI Software Target Service-TCP-135" dir=in action=allow protocol=TCP localport=135
5+
netsh advfirewall firewall add rule name="Microsoft iSCSI Software Target Service-UDP-138" dir=in action=allow protocol=UDP localport=138
6+
netsh advfirewall firewall add rule name="Microsoft iSCSI Software Target Service" dir=in action=allow program="%SystemRoot%\System32\WinTarget.exe" enable=yes
7+
netsh advfirewall firewall add rule name="Microsoft iSCSI Software Target Service Status Proxy" dir=in action=allow program="%SystemRoot%\System32\WTStatusProxy.exe" enable=yes
8+
9+
Netsh interface ipv4 set address "Ethernet" static 10.10.10.30
10+
Netsh interface ipv4 set dnsservers "Ethernet" static 10.10.10.10 primary
11+
netdom renamecomputer %computername% /newname:TSQLPRD07
12+
netdom join TSQLPRD07 /domain:Contso.com
13+
14+
$Servers = 'tsqlprd01','tsqlprd02','tsqlprd03'
15+
$command = {
16+
netsh advfirewall firewall add rule name="SQL Server (MSSQLSERVER)" dir=in action=allow protocol=TCP localport=1433
17+
}
18+
Invoke-Command -ComputerName $Servers -ScriptBlock $command
19+
20+
$command = {
21+
netsh advfirewall firewall add rule name="SQL Server (SQL2017)" dir=in action=allow protocol=TCP localport=1432
22+
}
23+
Invoke-Command -ComputerName $Servers -ScriptBlock $command
24+
25+
$command = {
26+
netsh advfirewall firewall add rule name="SQL Server Browser" dir=in action=allow protocol=UDP localport=1434
27+
}
28+
Invoke-Command -ComputerName $Servers -ScriptBlock $command
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Get-ClusterLog -Cluster sqlclusteradmin -TimeSpan 30 -UseLocalTime -Destination 'c:\temp\clusterLogs'
2+
3+
select * from sys.dm_os_cluster_nodes
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
$Servers = @('SqlNode01','SqlNode02','SqlNode03');
2+
13
/* Mirroring Failover */
2-
Get-EventLog -ComputerName TUL1CORPWIT1,TUL1CIPXIDB3,TUL1CIPXIDB2 -LogName Application |
4+
Get-EventLog -ComputerName $Servers -LogName Application |
35
Where-Object {$_.TimeGenerated -ge '3/19/2018 8:00:00 PM' -and ($_.Message -ilike '*The specified network name is no longer available*' -or $_.Message -ilike '*timed out*')} |
46
Select-Object MachineName, TimeGenerated, Source, Message | ft -AutoSize -Wrap
57

68
/* Shutdown/Reboot */
7-
Get-EventLog -ComputerName tul1cipmbdb1, tul1cipmbdb2 -LogName System |
9+
Get-EventLog -ComputerName $Servers -LogName System |
810
Where-Object { $_.TimeGenerated -ge '4/01/2018 4:00:00 AM' -and ($_.Message -ilike "*shutdown*" -or $_.Message -ilike "*reboot*")} |
911
Format-Table MachineName, EventID,TimeGenerated, EntryType, Source, Message, UserName -AutoSize -Wrap
1012

1113
/* Shutdown/Reboot */
12-
Get-EventLog -ComputerName tul1cipmbdb1, tul1cipmbdb2 -LogName System -InstanceId 1074,41,6008,6005,7036 |
13-
Where-Object { $_.TimeGenerated -ge '4/01/2018 4:00:00 AM' }
14+
Get-EventLog -ComputerName $Servers -LogName System -InstanceId 1074,41,6008,6005,7036 |
15+
Where-Object { $_.TimeGenerated -ge '4/01/2018 4:00:00 AM' }
16+
17+
/* Specific Events */
18+
$logs = Get-EventLog -LogName System -ComputerName $Servers | ? {$_.EventID -in 2004}
19+

PowerShell Commands/Ping Servers.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* PowerShell Command */
22
$sizeThreshold_In_MB = 5;
3-
$pingResultPath = 'F:\PingMirroringPartners\';
4-
$pingResultFile = 'F:\PingMirroringPartners\pingResult';
5-
$names = @('MyDatabaseServer01');
3+
$pingResultPath = $PSScriptRoot;
4+
$pingResultFile = "$pingResultPath\pingResult";
5+
$names = @('MyDbServer01');
66

77
# Delete files older than 15 days
88
$limit = (Get-Date).AddDays(-15);
9-
Get-ChildItem -Path $pingResultPath -Recurse -Force | Where-Object {$_.Name -like 'pingResult*' -and !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force;
9+
Get-ChildItem -Path $pingResultPath -Recurse -Force | Where-Object {$_.Name -like 'pingResult*' -and !$_.PSIsContainer -and $_.LastWriteTime -lt $limit } | Remove-Item -Force;
1010

1111
if (Test-Path $pingResultFile -PathType Leaf)
1212
{

PowerShell Commands/PowerShell Commands.ssmssqlproj

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
<Items>
44
<LogicalFolder Name="Connections" Type="2" Sorted="true">
55
<Items>
6+
<ConnectionNode Name="(local):CORPORATE\adwivedi">
7+
<Created>2019-07-07T20:38:32.0286356+05:30</Created>
8+
<Type>SQL</Type>
9+
<Server>(local)</Server>
10+
<UserName />
11+
<Authentication>Windows Authentication</Authentication>
12+
<InitialDB />
13+
<LoginTimeout>30</LoginTimeout>
14+
<ExecutionTimeout>0</ExecutionTimeout>
15+
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
16+
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
17+
</ConnectionNode>
618
<ConnectionNode Name="BAN-1ADWIVEDI-L:CORPORATE\adwivedi">
719
<Created>2018-08-28T18:54:33.17665+05:30</Created>
820
<Type>SQL</Type>
@@ -56,9 +68,9 @@
5668
<FullPath>Get-SqlResult2Excel.sql</FullPath>
5769
</FileNode>
5870
<FileNode Name="Ping Servers.sql">
59-
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:BAN-2ADWIVEDI-L:False:sa</AssociatedConnectionMoniker>
60-
<AssociatedConnSrvName>BAN-2ADWIVEDI-L</AssociatedConnSrvName>
61-
<AssociatedConnUserName>sa</AssociatedConnUserName>
71+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local):True</AssociatedConnectionMoniker>
72+
<AssociatedConnSrvName>(local)</AssociatedConnSrvName>
73+
<AssociatedConnUserName />
6274
<FullPath>Ping Servers.sql</FullPath>
6375
</FileNode>
6476
<FileNode Name="QueryResults-2-Excel.sql">

0 commit comments

Comments
 (0)