Geeks With Blogs
Server Error in Application An unhandled exception occurred during the execution of the current web request

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”.

Related Post:

Association, Aggregation, Composition object relationship

Talent vs Attitude – Harsha Bhogle

What we can learn from Babies

 

Posted on Friday, September 15, 2006 12:48 PM ASP.NET , C# , Web Services , Design Pattern , BizTalk , unhandled exception | Back to top


Comments on this post: Running batch file through ASP.NET application

# re: Running batch file through ASP.NET application
Requesting Gravatar...
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!
Left by Norm on Apr 11, 2007 6:55 AM

# re: Running batch file through ASP.NET application
Requesting Gravatar...
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_
Left by Norm on Apr 11, 2007 7:00 AM

# re: Running batch file through ASP.NET application
Requesting Gravatar...
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
Left by Norm on Apr 11, 2007 7:43 AM

# re: Running batch file through ASP.NET application
Requesting Gravatar...
Basic usage in ASP Classic:

strSQLCmd = "sp_runScript ping 192.168.0.1"
call objConnection.Execute(strSQLCmd)
Left by Norm on Apr 11, 2007 7:45 AM

# re: Running batch file through ASP.NET application
Requesting Gravatar...
If the path to the batch file contains blank, so the batch run fails.

c:\temp t\mybatch.bat
Left by Anton on Oct 24, 2007 6:22 PM

# re: Running batch file through ASP.NET application
Requesting Gravatar...
use single quote

'c:\temp t\mybatch.bat'
Left by mahesh on Oct 24, 2007 9:03 PM

# re: Running batch file through ASP.NET application
Requesting Gravatar...
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
Left by Mudassir Iqbal on Feb 26, 2008 4:49 AM

Your comment:
 (will show your gravatar)


Copyright © Mahesh Singh | Powered by: GeeksWithBlogs.net