Check if acces is blocked for MGET/MPUT - sql-server

I am trying to use the MPUT/MGET scripts as shown in:
https://www.codeproject.com/Articles/1170393/Download-file-s-from-FTP-Server-using-Command-thro
But i cant get it to work, is there a way to check if you have enought acces to be able to automate ftp through SQL ?

This script builds OS command and runs it using Sql Server xp_cmdshell system stored proc. To check current permission to run xp_cmdshell run
EXEC sp_configure;
without parameters and find the row with the name = 'xp_cmdshell'.
If you need to enable it see how to enable xp_cmdshell.

Related

Run SSIS package using T-SQL under different account

I normally run an SSIS package using a Sql Agent Job and a proxy user as described here: https://www.sqlservercentral.com/articles/run-an-ssis-package-under-a-different-account
I now need to run the same package using the same proxy user using T-SQL. I've been trying to use the [catalog].[create_execution] and [catalog].[start_execution] procedures to do this but there doesn't seem to be a way to specify a user.
How do I execute a package as a different user?
Is my best recourse the use of T-SQL to execute a SQL Agent Job that is configured to use the proxy user instead?
I don't know how agent actually works to make proxy users work - especially with regard to SSIS packages.
In a "normal" sql session say in SSMS, if I wanted to run a query as another user
EXECUTE AS USER = 'TurgidWizard';
SELECT USER_NAME() AS WhoAmI;
REVERT;
That code would allow me to impersonate you until I hit the REVERT call.
But, if you swap out calls to create_execution/start_execution you'll run into the same issue as trying to use a local sql server user runs into with using the methods in the SSISDB - it doesn't work. The methods in the SSISDB all run checks before they begin to ensure users have the correct access level and there isn't impersonation going on. Because once those methods start running, they themselves do impersonation so I guess that doesn't work well.
How can I run a package using tsql under a different account? I would start SSMS/sqlcmd under the credentials using RUNAS For example, the following will open a new command window as you.
runas /netonly /user:corpdomain.com\turgiwizard "cmd"
From there, things I do will be under the aegis of your user so I could run sqlcmd calls like
sqlcmd -S TheServer -d SSISDB -Q "EXECUTE catalog.create_execution ...;"
Mouse click will be Ctrl+Shift+right click executable.
Your SSMS install location is version dependent but try various ten digit increments of 140 in the following path
C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\Ssms.exe
The downside for me with regard to runas is that I could not automate getting my credentials passed into it. I've seen articles about use auto hot key and such but never had any luck with it.
Cleanest/easiest approach for something that needs to run regularly is to use sql agent with a proxy, or you could use Windows Task Scheduler and create it as the target user. One off executions, I'd likely use the runas approach.

Run batch file from SQL

I want to run batch file from SQL Job without using exec xp_cmdshell.
Any idea?
Thanks
You could use a SQL Server Job, otherwise i cannot think of a way you could without xp_cmdshell.
Take a look at this
I want to run batch file from SQL Job without using exec xp_cmdshell.
Any idea?
Worth to mention that you can also leverage SQLCLR.
Example: CLR Stored procedure to execute command
Some other googlable threads:
How to execute a DOS command when xp_cmdshell is disabled in SQL Server
Executing an external process() in SQLCLR Project
Such approach introduces severe risks like memory leaks, crashing of underlying .net app pool etc
Therefore another link: Security in the CLR World Inside SQL Server
Instead of running batch file, i have created power shell and ran it from SQL job. It satisfy my requirement and resolved my issue.
Do it like the picture: like this image.
The drive containing the batch file should be other than the C drive, to avoid trouble.
Add execute, read and write permissions for the user, which you are using to run the batch file, to get the username run this query: EXEC master..xp_cmdshell 'whoami', get the name after the \ sign. For example "nt service\mssqlserver". Add permission for this user: mssqlserver
Finally make sure you put the batch file on the same server as where you execute your Job.

SQL Server to check for files in directory

I was wondering if it is possible for SQL Server to check a directory for files and run a stored procedure. I did some research and found this, but I am wondering if there is a way to do what I want WITHOUT SSIS.
EDIT: After reading my post, I realized I should have been more specific. Is there a way to AUTOMATICALLY or set SQL Server to check for files in a directory and run a stored procedure?
You can use xp_cmdshell to run file related commands. To get a directly listing:
exec xp_cmdshell 'dir *.csv';
You can also use bulk insert to load a file from disk into a table and take actions based on the loaded contents.
Normally you'd use the File Watcher Task with SSIS. But you can also use SQL Server Agent to schedule a task for periodic execution, schedule a task with Windows Task Scheduler, or configure a stored procedure to runs at startup with sp_procoption that pauses (using waitfor) between processing times.

Issue with xp_cmdshell

I am trying to copy a back up file from a remote system to local using
EXEC master.sys.xp_cmdshell 'XCopy src dest'
command.
The above mentioned command returns file not found error. However the XCopy command copies file from the remote system to the local system when run in command prompt of the local system. Can somebody suggest a solution?
You are trying to access a remote server. When you attempt this from the command line you are using your permissions.
However, xp_cmdshell is run with the permissions of the SQL Server service account. Of course this account should be run with minimum permissions, you may need to increase its permissions to the other server.
MSDN
Also, don't forget that when the xp_cmdshell executes under the SQL account permissions that mapped network drive letters won't be available. You must use UNC path. i.e. \\servername\sharename\path\file

Use of xp_cmdshell in SQL Server

I am trying to execute a perl script from xp_cmdshell.
The output of the perl script is a csv file, but when I run
EXEC master..xp_cmdshell N'perl G:\script\perl.pl';
I can't find the csv file created, though the xp_cmdshell command seems to run fine, the output is the name of file that has to be created.
I am using xp_cmdshell to create a job step to execute the perl script.
Any help would be appreciated.
Since you're running this via a SQL Agent job, it'll be much safer to disable the use of xp_cmdshell via sp_configure (ref1 | ref2) and use a CmdExec job step instead.
When configuring the job step, be sure to go to the advanced page and enable job step logging to a table.
This will allow you to better troubleshoot the issues you're having with the perl job in general, as the issue could be related to something entirely outside the context of the database engine.

Resources