PSExec to execute sql scripts in SQL server of Remote machine - sql-server

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.

Related

Accessing SQL Server remotely using Windows Authentication

I have setup SQLExpress on a Windows 10 PC and trying to access it via another PC.
I also configured my setup using this guide.
I was able to connect using SQL Server Authentication.
However, when I tried connecting using Windows Authentication, I received the following message:
The login is from an untrusted domain and cannot be used with integrated authentication.
Is there a step I might have missed?
You may need to launch your SSMS under a different set of credentials (runas) in order for it to connect via windows authentication. One way to do this is to create a simple batch file (or shortcut) to do so.
Create a batch file on your desktop and name it something like:
AltSsms.bat
Add the following line to the AltSsms.bat file:
runas.exe /netonly /user:{domain}\{username} "C:\Program Files (x86)\Microsoft SQL Server Management Studio 18\Common7\IDE\Ssms.exe"
Be sure to replace {domain} and {username} with the actual values expected by the remote computer, and provide the full path to your local SSMS installation. I've provided my SSMS path for reference.
Save the updates to the batch file and then double-click it to open it. You should be prompted to enter the password for the {domain}\{username} you specified in the batch file.
Once SSMS loads (and assuming you've entered proper credentials), attempt to connect to the SQL Server instance with Windows Authentication. I've had to do this with several SSIS setups.

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

SQL Server MS, change user for Windows Authentication

I'm using a VPN to access a remote SQL Server. Normally I would run SSMS on my machine and use SQL Server Login for access. However, this server is set up to allow in only a particular user using Windows Authentication. I can access the DB by RDPing to that server and using SSMS on that machine, but is there any other way to do it from SSMS on my local machine?
Simplest way:
open up a command line window - cmd.exe
do the following: runas /user:<domain>\<alias> cmd.exe
enter your password when prompted
do the following: ssms.exe
it should now launch SSMS.exe under the account in #2 above.

Remote Desktop Connection Manager

I am using Remote Desktop Connection Manager 2.2
Do u guys know if there's a way to run a .bat/.cmd script (e.g. vpn script) before connecting to a VM?
I have a bat script to connect to Juniper VPN. I wish rdcman run it automatically before connecting so that I wouldn't have to run it every time manually.
Thank u.
You could start Remote Desktop Connection manager from the "VPN" batch file, by appending the following command to your batch file:
mstsc /v VMNAME
This will launch a session with VMNAME.

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