Guest user permission check for multiple databases per security best practices (SQL)

Introduction

This T-SQL sample script checks permissions for guest user in all databases. This script applies to system and to user databases.

Scenarios

Knowing about guest user in Sql server is a frequent security requirement. This can also be a source of confusion since many a times guest user is disabled in Sql Server msdb's Sql server and results in issues indicated in below mentioned KB. 
Some forums for such requirements are below:
http://social.msdn.microsoft.com/Forums/en-US/sqldatabaseengine/thread/04940880-85c4-419a-a859-8a6bd58a18f0 
Such a script should also help in below mentioned KB (KB has script for single database):
http://support.microsoft.com/kb/2539091
You should not disable the guest user in the msdb database in SQL Server 
Script does below:

Script

Just open a new query window using SQL Server Management Studio. Then paste this script and hit the Execute button. Sample output is below. 
Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page. 
 

SQL
Edit|Remove
IF@DbName = 'msdb'IF@CountNumPermissions = 0BEGINSET@Success = 0PRINT'*Guest user does not have (Connect) permission in database ' + @DbName + '. This is not suggested. Consider granting Connect permission in this database.'END
SQL
Edit|Remove
SET@SQLString = N'USE ' + @DbName + '; ' +  
 
              'SELECT@CountNumPermissionsOUT = COUNT(*) 
 
              FROMsys.database_permissionsASpermsJOINsys.database_principalsASprinsONperms.grantee_principal_id = prins.principal_idWHEREprins.name = ''guest'' AND state_desc <> ''DENY'' 'EXECsp_executesql@SQLString 
 
              , N'@CountNumPermissionsOUT int OUTPUT' 
 
              , @CountNumPermissionsOUT = @CountNumPermissionsOUTPUT
Additional Resources

You should not disable the guest user in the msdb database in SQL Server
sys.fn_my_permissions (Transact-SQL)