问题
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