Friday, May 28, 2010 3:50 PM
I need to call a batch file from sql server. The batch files exists in a different machine and it has to execute in that machine. I know batch files can be executed using xp_cmdshell in sql server, the machine where the batch file has to execute there is no sql server installed. Will I still be able to call the batch file and make it run in that machine??
Friday, May 28, 2010 5:51 PM
Here is a link that describes 3 ways to do this using: PSEXEC (from SysInternals), Windows Scripting Host, and Powershell.
Hope one of them is suitable for you.
- Marked As Answer by Tom Li - MSFTModerator Thursday, June 03, 2010 10:04 AM
Friday, May 28, 2010 6:25 PMIn addition and if you have to use SQL Server, then can use SQL Server Agent job with a "Operating system" step.
Monday, May 31, 2010 7:24 AMif you are to run sql server job to execute batch file, make sure sql serevr agent job has permission on remote drive and it is maped
Monday, May 31, 2010 10:39 AM
When you execute a batch file from a network share, you're executing it on the local machine, not the remote machine.
If you want to execute a program on another server, you can use a stored procedure on that server to invoke the command, and call that stored procedure from the local mcahine.
You could also create a web service on the remote server that invoked the command you want to execute.
In either case, be very careful that you don't open a security hole by either allowing more users to execute commands through the mechanism you implement, or by some user to execute commands other than the one you intend.
1) Example of running system command using xp_cmdshell
EXEC master..xp_CMDShell 'ISQL -L'
2) Example of running batch file using T-SQL
i) Running standalone batch file (without passed parameters)
EXEC master..xp_CMDShell 'c:findword.bat'
ii) Running parameterized batch file
DECLARE @PassedVariable VARCHAR(100)
DECLARE @CMDSQL VARCHAR(1000)
SET @PassedVariable = 'SqlAuthority.com'
SET @CMDSQL = 'c:findword.bat' + @PassedVariable
EXEC master..xp_CMDShell @CMDSQL
- Proposed As Answer by sekhara shiris chinta Tuesday, June 01, 2010 12:56 PM
Monday, May 31, 2010 12:24 PM
PSEXEC runs (if you name a remote machine in the parameters) the desired commands on the remote machine or remote machines. In other words, the PSEXEC.EXE runs locally, but it transmits other commands to the remote machine in order to run them there.
From the description: "PsExec is a light-weight telnet-replacement that lets you execute processes on other systems, complete with full interactivity for console applications, without having to manually install client software."
Tuesday, June 01, 2010 8:13 PM
sekhara, I also observe that the OP, Chaitanya, said that the other server does not have SQL Server running on it.
Thursday, June 17, 2010 10:28 AMyeah... psexec could be a method to do it, but I need to copy the tool to the server where my sql job exists.