Calling bash from PsExec from SQL with xp_cmdshell - sql-server

I am trying to call a bash command through a user account with PsExec. Cygwin is being used, and I am trying to run a command from SQL:
exec master..xp_cmdshell 'psexec -u cyg_server -p <pwd> -accepteula "bash script.sh"';
However, I get the following error from psexec:
Access is denied.
PsExec could not start bash script.sh:
Any suggestions?
I am using SQL 2005, Windows Server 2008, and Cygwin with fresh binaries

You're probably running this as a SQL user or a user without permissions to that file. My best bet is a SQL user (such as sa). When you do this, the Windows credentials it uses are of the Service Account, which is SYSTEM by default, but could be something else if you set it that way.
If you're just calling this from within SQL, make sure that you are, in fact, running as a Windows user that has permissions to open all of the files necessary.
If this is running in a SQL Server Agent job, you'll need to set up a Credential, and then set up a Proxy. Once you've done that, you can assign the Proxy to run the job (it's the third or fourth drop down on the Job Step dialog), and it will run with the correct Windows credentials.

Related

Using PsEXEC via SQL Server agent job

I have a need to add a step (a batch file that needs to run on the application server) once my backup from db server is complete. I am thinking of using the PsEXEC. For whatever reason the command I am testing via the query window executes the batch file at the app server just fine but when I add the same command as step in SQL server agent job it is giving me the error "The process could not be created for step 1 of job 0x318474904D93B54A81BA8B1AEE891A13 (reason: The system cannot find the file specified). The step failed."
Here is the command line running just fine from the query window
EXEC XP_CMDShell 'psexec -accepteula -u domain\user -p psswordhere \appserverip -s -d cmd.exe /c "F:\MovefFilesTest\TheJob\MoveTest.bat"'
FYI: domain\user is local admin on both servers and the sql service and agent are running under this account. It is also a sysadmin on the sql server itself.
Your help is appreciated.
Also F:\ is a local drive on the app server. I am puzzled why it is working on the query window but not when added as agent job

PSExec to execute sql scripts in SQL server of Remote machine

How to create a batch file to connect to Remote desktop and launch Microsoft Sql server and run some scripts.
I tried creating a batch file to connect to using
#ECHO OFF
mstsc /v:"SERVERNAME"
But i was not able to pass username and password
Can anyone advise on this.
EDIT
psexec \\remotemachine-u username -p password ipconfig
Above one connects me to the remote machine and prints its Ip details.
Now I am trying to execute SQL script which is in D:\ of remote machine to execute in Microsoft SQL server (connection) in the remote machine.
Is that possible.
You can use MSTSC to create the connection, with credentials (be sure to check the box that says "Allow me to Save Credentials"), then save that information to an .RDP file
After you should be able to launch:
mstsc "MyConnectionFile.rdp"
In the connection configuration, there is also a tab: "Programs / Start the following programs on connection", which should get your scripts going.
In general, the issue is to set up everything via the GUI, save it to an RDP file, and use that file in the future.

using sqlcmd script my linked server queries fail to connect

My stored procedure in SQL Server 2008 R2 runs queries on several local and several remote databases (via linked servers) and works fine when I exec it while in SSMS.
When I run the same exec from a sqlcmd script the linked server connections fail with error...
Cannot initialize the data source object of OLE DB provider "SQLNCLI10" for linked server "BLAHBLAH.BLAH.BLAH.ORG".
The sqlcmd invocation is...
sqlcmd -U sa -P myPassword-S localhost
-i C:\SS_DB_TRACK\job_run_periodic_nightly.sql
-o C:\SS_DB_TRACK\job_run_periodic_nightly.lst
Thanks much if someone can advise me on how to get the sqlcmd script to work.
Jay
The difference you are seeing is most likely due to running in a different security context. The sqlcmd parameters you have show it logging in as sa (usually not a good idea). So if you are seeing different behavior in SSMS then you are probably logging into that same server using Windows Authentication. If you logged into that server, via SSMS, using SQL Server Authentication as sa, then you should see the same error. Along those same lines, if you switch the sqlcmd authentication to be -E for Trusted Connection (i.e. Windows Authentication), then it should work (and it appears that this did, in fact, work). If you prefer that this process not be run under your security context then you need to check the security setup of the Linked Server to see why the two different Logins get different responses from using the Linked Server. Ideally you would use another non-sa Login for the process, possibly even create a Login (that is not a member of the sysadmin server role ;-) to handle this process.
Instead of "LinkedServerName" try [LinkedServerName]

SQL Server, I can execute sqlcmd with trusted connection but not sqlpackage

We have some deployment scripts that use a combination of dacpacs through sqlpackage and straight scripts using sqlcmd.
When I execute the sqlpackage dac\sqlpackage.exe /p:BlockOnPossibleDataLoss=False /p:BackupDatabaseBeforeChanges=True /a:Publish /sf:.\somedeploy.dacpac /tcs:"Data Source=MyDB;Initial Catalog=MyCatalog;Trusted_Connection=True;"
I get an error message *** Could not deploy package. Unable to connect to target server. I have login auditing turned on in sql and the message I am getting is Login failed. The login is from an untrusted domain and cannot be used with Windows authentication. [CLIENT: 111.222.223.224]
However, I can run our sql scripts through sqlcmd no problem by using the -E argument.
sqlcmd -i Permissions\CreateSqlUser.sql -S MyDB -E
I am running from a commandline that is set to "Run As Administrator" and am currently signed in as the admin. This is running on a Windows Server 2012 VM. I am using sql 2014. We have other installs that haven't had problems but this is our first 2014 install so we are trying to determine if something changed or if we are missing something and if so what.
If I run from profiler I don't see any audit log error messages. I checked the filters and what events I'm listening for and they all look good. Inside sql when I run exec sys.sp_readerrorlog 0, 1, 'Login Failed' I do get the messgaes to show up. (I might be doign something wrong in profiler). Also they show up in the system event logs. I'm not seeing anyplace where it tells me anything about the user attempting to login though.
If don't use trusted connection for sqlpackage and instead use a username and password it works fine. However, part of what our deployment script does is create the users so our preference would be to not do this.

restarting service on a remote server

I'm creating a .bat file to restart a windows service on a remote server. I did some research on this and found 2 ways of executing this.
1. Using NET START "service"
2. Using SC //server START service
My question is, how do i supply the username and password that's required to login to the remote server? TELNET accepts server and the username, but the password doesn't work.
Any pointers on this?
Got the answer! . Use this before using NET START or SC.
net use \\server password /USER:user
Verified..and it worked flawlessly!
More info here: Start And Stop Windows Service remotely using PSEXEC
For things like these I normally install an SSH server on the remote host: Bitvise has one that works wonderfully on windows called WinSSH (They have a free Personal Edition). You then use Putty to execute the command remotely.
I don't think Telnet can execute commands remotely without an interactive shell.
Another option is to use Powershell Remoting, but someone else will have to help on that.

Resources