How to check SQL Agent Service account has access to the UNC directory

ぐ巨炮叔叔 提交于 2021-01-29 08:14:37

问题


I'm currently using this query to figure out if the SQL has permissions,

EXEC master.dbo.xp_fileexist '\servername\sharename'

But this query is running as the user I'm logged in as, Is there any way I can execute it as SQL Agent Service Account to check if that user has access?


回答1:


To my knowledge, sql_agent runs under system\NetworkService account and it don't have access to UNC path (OR) other computers.

You can change the account which runs sql_agent by going to control panel -> services OR Sql server configuration manager.




回答2:


The following script should get you what you are after:

USE master
GO

DECLARE @ServiceAcct sysname, @Path as varchar(256)
SET @Path = '\\UNC\Path\To\Test\Access'

EXECUTE  dbo.xp_instance_regread
    @rootkey      = N'HKEY_LOCAL_MACHINE',
    @key          = N'SYSTEM\CurrentControlSet\Services\MSSQLServer',
    @value_name   = N'ObjectName',
    @value        = @ServiceAcct OUTPUT

EXECUTE AS LOGIN = @ServiceAcct;
EXEC master.dbo.xp_fileexist @Path

EXECUTE       master.dbo.xp_instance_regread
              @rootkey      = N'HKEY_LOCAL_MACHINE',
              @key          = N'SYSTEM\CurrentControlSet\Services\SQLServerAgent',
              @value_name   = N'ObjectName',
              @value        = @ServiceAcct OUTPUT

EXECUTE AS LOGIN = @ServiceAcct;
EXEC master.dbo.xp_fileexist @Path



回答3:


I'd Rahul that the agent cannot read UNC paths. I change the SQL Agent Service to run under a Service Account that is part of the domain. https://docs.microsoft.com/en-us/sql/ssms/agent/set-service-startup-account-sql-server-agent-sql-server-configuration-manager?view=sql-server-2017



来源:https://stackoverflow.com/questions/22196350/how-to-check-sql-agent-service-account-has-access-to-the-unc-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!