Server Error in Application

An unhandled exception occurred during the execution of the current web request
posts - 60, comments - 76, trackbacks - 54

My Links

News

My Bookmarks

Archives

Post Categories

ASP.NET

Running batch file through ASP.NET application

Few days back In my project I have to run a batch file which contains the series of piped MS-DOS commands to deals with Active Directory. Running batch file through ASP.NET requires impersonate the user id (for ASP.NET aspnet account), which requires lots of code and require understanding of windows securities.

I found one work around which is very easy and no need of extra coding.

 

In MS SQL Server there is one stored procedure named “xp_cmdshell” which executes a given command string as an operating-system command shell and returns any output as rows of text. i made a stored procedure and pass the batch file path as a parameter.

 

CREATE PROCEDURE RunBatchFile
 @BatchFile varchar(1000)
AS
 
 
 Create table #BatchResult (output varchar(1000)) 
 Insert # BatchResult exec master..xp_cmdshell @BatchFile
 
 
 Select output from # BatchResult 
 set noCount on
 Drop table  #OptInUserGroup
 set noCount off


GO

 

I used SQL Server temporary table to get the result.

I hope it will help other guys who are searching “How to run batch file from ASP.NET”.

Cheers!

Mahesh


Print | posted on Friday, September 15, 2006 12:48 PM |

Feedback

Gravatar

# re: Running batch file through ASP.NET application

THANK YOU! I've been searching the Internet forever for a solution that doesn't require me to screw up my server security. I first had this problem on a project in 2004. Didn't find a solution. Had the same issue this week. Finally, I have a solid method for executing batch files safely in W2K3. Thanks again!
4/11/2007 6:55 AM | Norm
Gravatar

# re: Running batch file through ASP.NET application

To help others looking for a solution to the same problem, I'm dropping in some search terms on this page.

Microsoft VBScript runtime error '800a0046' Permission denied, wscript, Windows Server 2003, IIS 6, WSH, Classic ASP, Vbscript, CreateObject("WScript.Shell"), W2K3, permissions, IUSR_, IWAM_
4/11/2007 7:00 AM | Norm
Gravatar

# re: Running batch file through ASP.NET application

FYI, here is code for an asynchronous call, if needed.

CREATE PROCEDURE sp_runScript @cmd varchar(1000) AS
declare @OLEResult int, @ShellID int
EXECUTE @OLEResult = sp_OACreate 'WScript.Shell', @ShellID OUT
EXECUTE @OLEResult = sp_OAMethod @ShellID, 'Run', Null, @cmd, 0, False
EXECUTE @OLEResult = sp_OADestroy @ShellID
GO
4/11/2007 7:43 AM | Norm
Gravatar

# re: Running batch file through ASP.NET application

Basic usage in ASP Classic:

strSQLCmd = "sp_runScript ping 192.168.0.1"
call objConnection.Execute(strSQLCmd)
4/11/2007 7:45 AM | Norm
Gravatar

# re: Running batch file through ASP.NET application

If the path to the batch file contains blank, so the batch run fails.

c:\temp t\mybatch.bat
10/24/2007 6:22 PM | Anton
Gravatar

# re: Running batch file through ASP.NET application

use single quote

'c:\temp t\mybatch.bat'
10/24/2007 9:03 PM | mahesh
Gravatar

# re: Running batch file through ASP.NET application

can some body explain me how that "master..xp_cmdshell" stored procedure is written which executes a given command string

i'm little confused in that stored procedure call "master..xp_cmdshell"


thanks in advance
Mudassir Iqbal
2/26/2008 4:49 AM | Mudassir Iqbal

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 1 and 5 and type the answer here:

Powered by: