Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Need to run XP_CMDSHELL via SQL agent job

$
0
0

Hi 

I need to move files from folder 1,Audit1 to folder 2 ,Audit1using  a SQL AGENT JOB 

the script is listed  just below 

EXEC master..xp_cmdshell 'powershell.exe "Move-Item \\SERVER\e\Audit1\*.sqlaudit \\SERVER\e\Audit2 -ErrorAction SilentlyContinue"'

to run this via SQL AGENT job and run as user with limited execute option only  for security reasons ,I have done the belwo steps.

I have created a proxy user with belwo script 

USE MASTER;
GO

--create a windows login on the server/pc then run the below script

CREATE LOGIN [domain\username1] FROM WINDOWS;



--Run the below step to create a password for the Proxy account  (password used is the server login password for this account)

CREATE CREDENTIAL ##xp_cmdshell_proxy_account## WITH IDENTITY= 'domain\username1', SECRET= 'secretpassword'


--create a role by running the below and grant access to run XP_CMDSHELL
CREATE ROLE [CmdShell_Executor] AUTHORIZATION [dbo]GRANT EXEC ON xp_cmdshell TO [CmdShell_Executor]



--create the user on the database master where the XP_CMDSHELL resides

CREATE USER [ProxyUser1] FROM LOGIN [domain\username1];

 
EXEC sp_addrolemember CmdShell_Executor,ProxyUser1;

After this user is created I used this user ProxyUser1 in the SQL AGENT JOB to use as run as "ProxyUser1"

to execute the batch script I stated in the first.

 
EXEC master..xp_cmdshell 'powershell.exe "Move-Item \\SERVER\e\Audit1\*.sqlaudit \\SERVER\e\Audit2 -ErrorAction SilentlyContinue"'

This job fnishes successfully but the files are not moved?  when I change the user to nothing(by removing the user name in the run as option) then this job fnishes sucessfully and files are moved.?

Please help 

This worked for me previously but its not working today.

My aim here is to move files form one folder to other using XP_CMDSHELL  securely?any suggestions are also appreciated


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>