MSSQL Agent Task Failing - Stored Procedure I have a stored procedure that is giving me some issues as an Agent Job

When running the task, I get the following error:

Executed as user: SQLUserName. Access to the remote server is denied because the current security context is not trusted. [SQLSTATE 42000] (Error 15274).  The step failed.

The Task has 1 step and that step is just "EXEC dbo.Procdure_Name" and the "Run as" is blank (i'm unable to specify anything here). I have put in "Advanced", "Run as user" as SQLUserName.

When I remove this, I get the error:

Executed as user: domain\servename$. Login failed for user 'domain\servername$'. [SQLSTATE 28000] (Error 18456).  The step failed.


This is running a command on a linked server. When I remove that command it works.

The linked server is set to logon to "be made using the login's current security context" and testing the link server works fine.

If I log into SMSS as SQLUserName and run the procedure manually it works fine.

Would appreciate any suggestions.
I have put in "Advanced", "Run as user" as SQLUserName


This is not the option to use in your case. 

Using this option you can specify the user, not login, to run your sp. However, when you connect to linked server, it's not a database user, but server login that is used, it's "login's current security context" that will be used.

So in your case all depends on the job's owner. If it's sysadmin, the job is executed under Windows account of your SQL Server Agent, and if it's not sysadmin, the job is executing using job's owner's login.

From your error it seems that your job's owner is sysadmin, as domain\servename$ is used. This is because your Agent is running under Local System or Network Service that when going on the network use your pc's account, domain\PCname$.

To fix you problem you can either change job owner to some login that has access to linked server or change SQL Server Agent's service account to domain account that should be mapped on linked server.