Skip to content

Commit a015e05

Browse files
committed
Issue #3334: sp_Blitz Fails because of permissions
1 parent 43b2902 commit a015e05

1 file changed

Lines changed: 33 additions & 7 deletions

File tree

sp_Blitz.sql

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ AS
198198
,@SkipMSDB bit = 0
199199
,@SkipModel bit = 0
200200
,@SkipTempDB bit = 0
201-
,@SkipValidateLogins bit = 0;
201+
,@SkipValidateLogins bit = 0
202+
,@SkipModelCheck BIT = 0;
202203

203204
DECLARE
204205
@db_perms table
@@ -219,7 +220,7 @@ AS
219220
fmp.permission_name
220221
FROM sys.databases AS d
221222
CROSS APPLY fn_my_permissions(d.name, 'DATABASE') AS fmp
222-
WHERE fmp.permission_name = N'SELECT' /*Databases where we don't have read permissions*/
223+
WHERE fmp.permission_name = N'SELECT'; /*Databases where we don't have read permissions*/
223224

224225
/* End of declarations for First Responder Kit consistency check:*/
225226
;
@@ -307,6 +308,31 @@ AS
307308
SET @SkipValidateLogins = 1;
308309
END; /*Need execute on sp_validatelogins*/
309310

311+
IF EXISTS
312+
(
313+
SELECT 1/0
314+
FROM @db_perms
315+
WHERE database_name = N'model'
316+
)
317+
BEGIN
318+
BEGIN TRY
319+
IF EXISTS
320+
(
321+
SELECT 1/0
322+
FROM model.sys.objects
323+
)
324+
BEGIN
325+
SET @SkipModelCheck = 0; /*We have read permissions in the model database, and can view the objects*/
326+
END;
327+
END TRY
328+
BEGIN CATCH
329+
SET @SkipModelCheck = 1; /*We have read permissions in the model database ... oh wait we got tricked, we can't view the objects*/
330+
END CATCH;
331+
END;
332+
ELSE
333+
BEGIN
334+
SET @SkipModelCheck = 1; /*We don't have read permissions in the model database*/
335+
END;
310336
END;
311337

312338
SET @crlf = NCHAR(13) + NCHAR(10);
@@ -467,11 +493,11 @@ AS
467493
);
468494

469495
/*Skip individial checks where we don't have permissions*/
470-
INSERT #SkipChecks (DatabaseName, CheckID, ServerName)
471-
SELECT
472-
v.*
473-
FROM (VALUES(NULL, 29, NULL)) AS v (DatabaseName, CheckID, ServerName) /*Looks for user tables in model*/
474-
WHERE NOT EXISTS (SELECT 1/0 FROM @db_perms AS dp WHERE dp.database_name = 'model');
496+
INSERT #SkipChecks (DatabaseName, CheckID, ServerName)
497+
SELECT
498+
v.*
499+
FROM (VALUES(NULL, 29, NULL)) AS v (DatabaseName, CheckID, ServerName) /*Looks for user tables in model*/
500+
WHERE @SkipModelCheck = 1;
475501

476502
INSERT #SkipChecks (DatabaseName, CheckID, ServerName)
477503
SELECT

0 commit comments

Comments
 (0)