?
Solved

xp_cmdshell confused

Posted on 2012-09-20
9
Medium Priority
?
760 Views
Last Modified: 2012-09-24
I am trying to execute a file, tried both a .bat file that kicks off the .exe and the .exe file that I want to run.  I don't get an error, the results tab gives me a 1 and null and the messages tab says one row affected.  But the file is never executed.  I can do a directory   of the c:\c folder, and create directories in that folder using xp_cmdshell so security on the folder should be ok.  But the file is never executed.  The executable writes a log file that is how I know it doesn't execute.  Any ideas?

DECLARE @rc AS INT
DECLARE @sqlcmd AS VARCHAR(100)

SET @sqlcmd = 'c:\c\7.bat'
EXEC @rc = master.dbo.xp_cmdshell @sqlcmd
go

Open in new window


Thanks

Dan
0
Comment
Question by:DanDWolff
  • 5
  • 2
8 Comments
 
LVL 29

Accepted Solution

by:
Bill Bach earned 2000 total points
ID: 38417938
Are you sure that it doesn't run?  Remember that this process will not be running in your native user context.  As a result, the log file may be in a completely different location that where you expect, or it may lack the rights to write the log file to the location you wanted.

First, verify that the log file is being written to a specific directory, not the CWD.  Second, make sure that "Everyone" has write access to that folder.
0
 

Author Comment

by:DanDWolff
ID: 38418045
Thanks for the quick response.  I double checked and Everyone has modify rights to the folder.  I did a search of my entire computer for the log file and search didn't find any copies.

I added a print command to the @RC variable after the exec command it it returns the following code in the messages tab -1066598274.  I would have expected a 1 or 0.  Going to try looking that up.

DECLARE @rc AS INT
DECLARE @sqlcmd AS VARCHAR(100)

SET @sqlcmd = 'c:\c\7.bat'
EXEC @rc = master.dbo.xp_cmdshell @sqlcmd
PRINT @RC
go

Open in new window

0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 38418125
The .bat file cannot be interactive in any way; that is, it cannot ask qs or for any input from the caller.  You can only call code that can run directly from the command line input, not requiring any direct user input at all.
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 

Author Comment

by:DanDWolff
ID: 38418432
The .bat file is very basic it calls a .exe and passes a commandline argument to it.

@echo off
cd \
cd c
c:\c\ssmgr.exe -fid 4

Open in new window


ssmgr.exe doesn't have anything output the the screen either.  Presently all it does it read an sql database using the commandline and output a text file based on what it reads in the database.  Both the bat file and exe run from command prompt like they should.
0
 

Author Comment

by:DanDWolff
ID: 38419045
it is something with my security on the folder I am trying to run the executable from.  If I move everything to the root c:\  it works just like it is supposed to.  The security on the folder that I am trying to run it from is as follows:

Everyone - modify
authenticated users - modify
system - full
domain\computername$ - modify
administrators - full
user - full - trying anything at this point but this is not an option for me when I move this to production.

Any ideas what I am missing?

Thanks

Dan
0
 

Author Comment

by:DanDWolff
ID: 38419067
I feel dumb, I forgot the sp_xp_cmdshell_proxy_account.  I gave that account access to the folder and all is well.

Thanks to everybody that looked at this for me.

Dan
0
 

Author Comment

by:DanDWolff
ID: 38419126
I've requested that this question be closed as follows:

Accepted answer: 0 points for DanDWolff's comment #a38419067

for the following reason:

I figured out what I missed on my own before the experts did.
0
 
LVL 29

Expert Comment

by:Bill Bach
ID: 38419127
it may lack the rights to write the log file to the location you wanted.
Wasn't this the crux of my first answer???
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question