Skip to content

Commit b0b2875

Browse files
authored
Merge pull request #3380 from Montro1981/SQL-Server-First-Responder-Kit_3377-msdb_Permissions
#3377 Expanded msdb permission checks
2 parents e06fd71 + 78596fc commit b0b2875

File tree

1 file changed

+59
-12
lines changed

1 file changed

+59
-12
lines changed

sp_Blitz.sql

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ AS
196196
,@SkipXPFixedDrives bit = 0
197197
,@SkipXPCMDShell bit = 0
198198
,@SkipMaster bit = 0
199-
,@SkipMSDB bit = 0
199+
,@SkipMSDB_objs bit = 0
200+
,@SkipMSDB_jobs bit = 0
200201
,@SkipModel bit = 0
201202
,@SkipTempDB bit = 0
202203
,@SkipValidateLogins bit = 0
@@ -343,7 +344,7 @@ AS
343344
END;
344345
END;
345346

346-
IF ISNULL(@SkipMSDB, 0) != 1 /*If @SkipMSDB hasn't been set to 1 by the caller*/
347+
IF ISNULL(@SkipMSDB_objs, 0) != 1 /*If @SkipMSDB_objs hasn't been set to 1 by the caller*/
347348
BEGIN
348349
IF EXISTS
349350
(
@@ -359,16 +360,45 @@ AS
359360
FROM msdb.sys.objects
360361
)
361362
BEGIN
362-
SET @SkipMSDB = 0; /*We have read permissions in the msdb database, and can view the objects*/
363+
SET @SkipMSDB_objs = 0; /*We have read permissions in the msdb database, and can view the objects*/
363364
END;
364365
END TRY
365366
BEGIN CATCH
366-
SET @SkipMSDB = 1; /*We have read permissions in the msdb database ... oh wait we got tricked, we can't view the objects*/
367+
SET @SkipMSDB_objs = 1; /*We have read permissions in the msdb database ... oh wait we got tricked, we can't view the objects*/
367368
END CATCH;
368369
END;
369370
ELSE
370371
BEGIN
371-
SET @SkipMSDB = 1; /*We don't have read permissions in the msdb database*/
372+
SET @SkipMSDB_objs = 1; /*We don't have read permissions in the msdb database*/
373+
END;
374+
END;
375+
376+
IF ISNULL(@SkipMSDB_jobs, 0) != 1 /*If @SkipMSDB_jobs hasn't been set to 1 by the caller*/
377+
BEGIN
378+
IF EXISTS
379+
(
380+
SELECT 1/0
381+
FROM @db_perms
382+
WHERE database_name = N'msdb'
383+
)
384+
BEGIN
385+
BEGIN TRY
386+
IF EXISTS
387+
(
388+
SELECT 1/0
389+
FROM msdb.dbo.sysjobs
390+
)
391+
BEGIN
392+
SET @SkipMSDB_jobs = 0; /*We have read permissions in the msdb database, and can view the objects*/
393+
END;
394+
END TRY
395+
BEGIN CATCH
396+
SET @SkipMSDB_jobs = 1; /*We have read permissions in the msdb database ... oh wait we got tricked, we can't view the objects*/
397+
END CATCH;
398+
END;
399+
ELSE
400+
BEGIN
401+
SET @SkipMSDB_jobs = 1; /*We don't have read permissions in the msdb database*/
372402
END;
373403
END;
374404
END;
@@ -540,17 +570,34 @@ AS
540570
INSERT #SkipChecks (DatabaseName, CheckID, ServerName)
541571
SELECT
542572
v.*
543-
FROM (VALUES(NULL, 6, NULL), /*Jobs Owned By Users*/
544-
(NULL, 28, NULL), /*SQL Agent Job Runs at Startup*/
545-
(NULL, 57, NULL), /*Tables in the MSDB Database*/
573+
FROM (VALUES(NULL, 28, NULL)) AS v (DatabaseName, CheckID, ServerName) /*Tables in the MSDB Database*/
574+
WHERE @SkipMSDB_objs = 1;
575+
576+
INSERT #SkipChecks (DatabaseName, CheckID, ServerName)
577+
SELECT
578+
v.*
579+
FROM (VALUES
580+
/*sysjobs checks*/
581+
(NULL, 6, NULL), /*Jobs Owned By Users*/
582+
(NULL, 57, NULL), /*SQL Agent Job Runs at Startup*/
546583
(NULL, 79, NULL), /*Shrink Database Job*/
547584
(NULL, 94, NULL), /*Agent Jobs Without Failure Emails*/
548585
(NULL, 123, NULL), /*Agent Jobs Starting Simultaneously*/
549586
(NULL, 180, NULL), /*Shrink Database Step In Maintenance Plan*/
550587
(NULL, 181, NULL), /*Repetitive Maintenance Tasks*/
551-
(NULL, 219, NULL) /*Alerts Without Event Descriptions*/
552-
) AS v (DatabaseName, CheckID, ServerName)
553-
WHERE @SkipMSDB = 1;
588+
589+
/*sysalerts checks*/
590+
(NULL, 30, NULL), /*Not All Alerts Configured*/
591+
(NULL, 59, NULL), /*Alerts Configured without Follow Up*/
592+
(NULL, 61, NULL), /*No Alerts for Sev 19-25*/
593+
(NULL, 96, NULL), /*No Alerts for Corruption*/
594+
(NULL, 98, NULL), /*Alerts Disabled*/
595+
(NULL, 219, NULL), /*Alerts Without Event Descriptions*/
596+
597+
/*sysoperators*/
598+
(NULL, 31, NULL) /*No Operators Configured/Enabled*/
599+
) AS v (DatabaseName, CheckID, ServerName)
600+
WHERE @SkipMSDB_jobs = 1;
554601

555602
INSERT #SkipChecks (DatabaseName, CheckID, ServerName)
556603
SELECT
@@ -9993,4 +10040,4 @@ EXEC [dbo].[sp_Blitz]
999310040
@OutputProcedureCache = 0 ,
999410041
@CheckProcedureCacheFilter = NULL,
999510042
@CheckServerInfo = 1
9996-
*/
10043+
*/

0 commit comments

Comments
 (0)