Skip to content

Commit 79f3d75

Browse files
authored
Merge pull request #3340 from Montro1981/dev
Issue #3334: sp_Blitz Fails because of permissions
2 parents a433b5b + 842526e commit 79f3d75

1 file changed

Lines changed: 35 additions & 7 deletions

File tree

sp_Blitz.sql

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ AS
219219
fmp.permission_name
220220
FROM sys.databases AS d
221221
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*/
222+
WHERE fmp.permission_name = N'SELECT'; /*Databases where we don't have read permissions*/
223223

224224
/* End of declarations for First Responder Kit consistency check:*/
225225
;
@@ -306,7 +306,35 @@ AS
306306
BEGIN
307307
SET @SkipValidateLogins = 1;
308308
END; /*Need execute on sp_validatelogins*/
309-
309+
310+
IF ISNULL(@SkipModel, 0) != 1 /*If @SkipModel hasn't been set to 1 by the caller*/
311+
BEGIN
312+
IF EXISTS
313+
(
314+
SELECT 1/0
315+
FROM @db_perms
316+
WHERE database_name = N'model'
317+
)
318+
BEGIN
319+
BEGIN TRY
320+
IF EXISTS
321+
(
322+
SELECT 1/0
323+
FROM model.sys.objects
324+
)
325+
BEGIN
326+
SET @SkipModel = 0; /*We have read permissions in the model database, and can view the objects*/
327+
END;
328+
END TRY
329+
BEGIN CATCH
330+
SET @SkipModel = 1; /*We have read permissions in the model database ... oh wait we got tricked, we can't view the objects*/
331+
END CATCH;
332+
END;
333+
ELSE
334+
BEGIN
335+
SET @SkipModel = 1; /*We don't have read permissions in the model database*/
336+
END;
337+
END;
310338
END;
311339

312340
SET @crlf = NCHAR(13) + NCHAR(10);
@@ -467,11 +495,11 @@ AS
467495
);
468496

469497
/*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');
498+
INSERT #SkipChecks (DatabaseName, CheckID, ServerName)
499+
SELECT
500+
v.*
501+
FROM (VALUES(NULL, 29, NULL)) AS v (DatabaseName, CheckID, ServerName) /*Looks for user tables in model*/
502+
WHERE @SkipModel = 1;
475503

476504
INSERT #SkipChecks (DatabaseName, CheckID, ServerName)
477505
SELECT

0 commit comments

Comments
 (0)