|
| 1 | +--- |
| 2 | +author: ajithkr-ms |
| 3 | +ms.service: sql-database |
| 4 | +ms.topic: include |
| 5 | +ms.date: 12/19/2022 |
| 6 | +ms.author: ajithkr-ms |
| 7 | +--- |
| 8 | + |
| 9 | + The login used to connect to a source SQL Server instance requires certain minimal permissions to query the requisite information. The following script shows creation of a SQL Server login with the requisite permissions. |
| 10 | + |
| 11 | + ```sql |
| 12 | + -- Create a login to run the assessment |
| 13 | + use master; |
| 14 | + CREATE LOGIN [evaluator] |
| 15 | + WITH PASSWORD = '<provide a strong password>' |
| 16 | + GO |
| 17 | + |
| 18 | + -- Create user in every database other than tempdb and model and provide minimal read-only permissions. |
| 19 | + use master; |
| 20 | + EXECUTE sp_MSforeachdb 'USE [?]; IF (''?'' NOT IN (''tempdb'',''model'')) CREATE USER [evaluator] FOR LOGIN [evaluator]' |
| 21 | + EXECUTE sp_MSforeachdb 'USE [?]; IF (''?'' NOT IN (''tempdb'',''model'')) GRANT SELECT ON sys.sql_expression_dependencies TO [evaluator]' |
| 22 | + EXECUTE sp_MSforeachdb 'USE [?]; IF (''?'' NOT IN (''tempdb'',''model'')) GRANT VIEW DATABASE STATE TO [evaluator]' |
| 23 | + GO |
| 24 | + |
| 25 | + -- Provide server level read-only permissions |
| 26 | + use master; |
| 27 | + GRANT SELECT ON sys.sql_expression_dependencies TO [evaluator] |
| 28 | + GRANT EXECUTE ON OBJECT::sys.xp_regenumkeys TO [evaluator]; |
| 29 | + GRANT VIEW DATABASE STATE TO evaluator |
| 30 | + GRANT VIEW SERVER STATE TO evaluator |
| 31 | + GRANT VIEW ANY DEFINITION TO evaluator |
| 32 | + GO |
| 33 | + |
| 34 | + -- Required from SQL 2014 onwards for database connectivity. |
| 35 | + use master; |
| 36 | + GRANT CONNECT ANY DATABASE TO evaluator |
| 37 | + GO |
| 38 | + |
| 39 | + -- Provide msdb specific permissions |
| 40 | + use msdb; |
| 41 | + GRANT EXECUTE ON [msdb].[dbo].[agent_datetime] TO [evaluator] |
| 42 | + GRANT SELECT ON [msdb].[dbo].[sysjobsteps] TO [evaluator] |
| 43 | + GRANT SELECT ON [msdb].[dbo].[syssubsystems] TO [evaluator] |
| 44 | + GRANT SELECT ON [msdb].[dbo].[sysjobhistory] TO [evaluator] |
| 45 | + GRANT SELECT ON [msdb].[dbo].[syscategories] TO [evaluator] |
| 46 | + GRANT SELECT ON [msdb].[dbo].[sysjobs] TO [evaluator] |
| 47 | + GRANT SELECT ON [msdb].[dbo].[sysmaintplan_plans] TO [evaluator] |
| 48 | + GRANT SELECT ON [msdb].[dbo].[syscollector_collection_sets] TO [evaluator] |
| 49 | + GRANT SELECT ON [msdb].[dbo].[sysmail_profile] TO [evaluator] |
| 50 | + GRANT SELECT ON [msdb].[dbo].[sysmail_profileaccount] TO [evaluator] |
| 51 | + GRANT SELECT ON [msdb].[dbo].[sysmail_account] TO [evaluator] |
| 52 | + GO |
| 53 | + |
| 54 | + -- Clean up |
| 55 | + --use master; |
| 56 | + -- EXECUTE sp_MSforeachdb 'USE [?]; DROP USER [evaluator]' |
| 57 | + -- DROP LOGIN [evaluator] |
| 58 | + --GO |
| 59 | + ``` |
| 60 | + |
| 61 | + Here's how the permissions script can be used: |
| 62 | + |
| 63 | + - Save the permissions script (with valid password string) as an _.sql_ file, say _c:\workspace\MinPermissions.sql_ |
| 64 | + - Connect to the instance(s) using an account with sysadmin permissions and execute the script. You can use **SQL Server Management Studio** or **sqlcmd**. The following example uses a trusted connection. |
| 65 | + ```cmd |
| 66 | + sqlcmd.exe -S sourceserver\sourceinstance -d master -E -i c:\workspace\MinPermissions.sql |
| 67 | + ``` |
| 68 | + - Use the minimal permissions account so created for further connections. |
| 69 | + |
0 commit comments