0

I need to write an console application that returns a return code that can be captured through xp_cmdshell.

I started with c# code as follows,

class Program
    {
        static int Main(string[] args)
        {
            //make sure the correct number of arguments are being passed.
            if (args.Length !=5)
            {
                Console.WriteLine("not thr right number of args. \nUsage SFTPUploadFile <host> <port> <username> <password> <localFilePath>");
                return 1;
            }

            return 0;        
        }

    }

The XP_cmdhsell I am using some code I found

declare  @rc int

create table #output (id int identity(1,1), output nvarchar(255) null)
insert #output (output) exec @rc = master..xp_cmdshell 'd:\FILENAME PARA1 PARA2 PARA3 PARA4 PARA5'
select * from #output where output is not null order by id
drop table #output

but when I run my xp_cmdshell, I just get null. Shouldn't I be getting 1 or 0?

Thanks

user2206329
  • 2,792
  • 10
  • 54
  • 81

1 Answers1

0

It looks like your program checks to see whether or not there are 5 parameters, and does nothing (returns 0) if there are.

The xp_cmdshell command, in turn, supplies all the parameters. xp_cmdshell will return NULL if it does not receive any output.

If you changed the code to read something like this:

class Program
    {
        static int Main(string[] args)
        {
            //make sure the correct number of arguments are being passed.
            if (args.Length !=5)
            {
                Console.WriteLine("not thr right number of args. \nUsage SFTPUploadFile <host> <port> <username> <password> <localFilePath>");
                return 1;
            }

            Console.WriteLine("You had the right number of args.");
            return 0;        
        }

    }

(Alternatively, you can do Console.WriteLine("0"); and Console.WriteLine("1"); if you really want 0 or 1 back.)

You'd get back You had the right number of args.. This is because return 0; and return 1; do not print anything out to the console, which is what xp_cmdshell will send back to the client.

You can prove this by doing EXEC master..xp_cmdshell 'cd ..' ... it's a command that does not return results. On the other hand, EXEC master..xp_cmdshell 'dir *.exe' will return you the directory contents, as this is what would be output (or written) to the console.

brazilianldsjaguar
  • 1,409
  • 1
  • 20
  • 45
  • on my local it actually outputs the success or error messages. But on the production server it just returns null or both cases... could there be a permission issue? – user2206329 Jun 20 '13 at 03:47
  • You're right - xp_cmdshell runs under your username / password when attempting to run (and you're a sysadmin), otherwise it will use the proxy user. You (or the proxy user) may or may not have the right permissions on the server in production to see a result. More can be found [here](http://msdn.microsoft.com/en-us/library/ms175046.aspx) – brazilianldsjaguar Jun 20 '13 at 14:11