Using PsEXEC via SQL Server agent job - sql-server

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

Related

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.

Running SQLCMD in a Batch Script for local .sql file on local SQLSERVEREXPRESS2014 throwing 'Access Denied' error

I have a created a .sql file that will update a table in a data warehouse I am creating using SQL Server 2014 Express. Since Express does not support scheduling jobs, I am attempting to use a batch script scheduled in task scheduler to accomplish this.
ECHO OFF
ECHO This is running to update the [Table Name] Table in the SQL Server Database
PAUSE
sqlcmd -S [ComputerName]\SQLEXPRESS2014 -U myusername -P mypassword -i "My Path With Spaces" of [MyQuery].sql
ECHO Query Worked!
PAUSE
However, when I run it (even as Administrator), it denies me access. I am the administrator on this computer and the SQL Server Express 2014 service is local to my machine. I appreciate any advise the community may be able to provide here. :)

Configure Jenkins to restore DB to a known dataset before running MsTest application

I have a MsTest project configured in Jenkins to test a ASP.Net MVC application.
And it's working fine.
What I need to do is, Need to restore a SQL Server database which is located on a different server that use by the MVC application, before I run the MsTest project on Jenkins.
So Can I do it as doing something like adding a step to restore the DB before the test project execution step?
And Is there any way to command (even using some plugins) remote SQL Server to restore a database through Jenkins?
REM restore DB (with REPLACE)
SQLCMD -U user -P password -S .\SQLEXPRESS -Q "RESTORE DATABASE databasename FROM DISK='path to back file'" WITH REPLACE
You need to put this script in a "batch windows cmd" in Jenkins (replace the user, password, databasename, and path to back file with the correct values).

SQL Server Agent run powershell file not found

I created a SQL Server Agent job and here is the job detail.
The job owner is my windows domain login.
The type is OS (cmdExec)
Run as SQL Server Agent Service Account
Command:
powershell.exe -executionpolicy bypass -file "D:\MyFolder\myFile.ps1".
When I ran the job it returned an error stating "D:\MyFolder\myFile.ps1" does not exist.
When I run the same command from cmd prompt it works find.
My question is what can I do to make SQL Server Agent Service Account "see" the file?
Thanks,
It's a privilege issue. I just granted read/execute permission to the Network Service login which SQL Server Agent uses.
It works now.
And thanks for all your responses.

Calling bash from PsExec from SQL with xp_cmdshell

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.

Resources