How to connect to a remote database with Windows authentication using sqlcmd? - sql-server

I'm trying to use sqlcmd to execute querys on a remote database but I can´t login
When I connect using SSMS i have to use this runas command
runas /user:domain\username /netonly "Ssms.exe"
How can I log in like that using sqlcmd?
I´ve tried "-U domain\username -P password" and "-E" but none of those work.
I've also tried
runas /user:domain\username cmd.exe
to run cmd and then use sqlcmd from there but it says cmd.exe cannot be executed.
Thanks in advance!

Related

PsExec command working on cmd doesn't work inside .bat file nor via SQL xp_cmdshell

When I execute the following command on machine1's cmd it works fine:
C:\PSTools\psexec64.exe \\machine2 -u domain\user -p pass -i -d -accepteula C:\folder\file.bat
However, if I try to run it via xp_cmdshell (sql server is also on machine1) as bellow it will fail:
exec xp_cmdshell 'C:\PSTools\psexec64.exe \\machine2 -u domain\user -p pass -i -d -accepteula C:\folder\file.bat'
Here is the output:
NULL
PsExec v2.4 - Execute processes remotely
Copyright (C) 2001-2022 Mark Russinovich
Sysinternals - www.sysinternals.com
NULL
Connecting to xx.xxx.xxx.xx... Starting PSEXESVC service on xx.xxx.xxx.xx... Could not start PSEXESVC service on xx.xxx.xxx.xx:
Access is denied.
Connecting to xx.xxx.xxx.xx... Starting PSEXESVC service on xx.xxx.xxx.xx...
I also tried to save the command as a bat file to see if the problem is exclusive to the SQL server, but it won't work either.
Here is the output:
PsExec could not start C:\folder\file.bat on xx.xxx.xxx.xx:
The user name or password is incorrect.
The exact same command works fine on cmd, same machine.

SQL cmdshell to execute putty command

I am trying to run a sh file in putty ssh from windows which was done successfully using putty and cmd prompt. When trying the same in xp_cmdshell or through sp_start_job in mssql the query is running for longtime without end.
SQL Query:
exec xp_cmdshell 'D:\path\run.bat'
batch file:
"C:\Program Files\Putty\putty.exe" -ssh root#10.10.10.1 -pw mypass -m D:\path\runcmd.txt
Is the way I am trying correct?
Could you please try this?
EXEC master..xp_CMDShell '"D:\path\run.bat"'
details referred from below link,
Executing a bat file inside a Stored Procedure using SQL server 2005

Windows Server 2012 - start up using Runas Administrator

I need to start an application by running "Run as Administrator". I have been searching for the command line and tool, it seems psexec.exe able to provide this functionality.
However, I have no success running it.
psexec -u myowndomain\david123 -p myPassword1 "E:\Temp\StartUp.exe"
I do not need to run remote computer. I just want to simulate the "Run as Administrator" when I do it manually.
Is the command correct ? Thanks.
There is a runas command, you can read more there: RUNAS
runas [{/profile | /noprofile}] [/env] [{/netonly | /savecred}] [/smartcard] [/showtrustlevels] [/trustlevel] /user: " "

Run a batch file on remote host using PsExec

I am experimenting with PsExec and I am trying to run a batch file on a remote host from a local PC on the same LAN. The batch file has been tested on the local PC and works fine. I managed to connect to the remote host via PsExec using the below commands.
PsExec -u Username -p Password \\Remote_Host_IP C:\Path_to_batch_file\Batch.bat
I am getting this error:-
PsExec could not start C:\Path_to_batch_file\Batch.bat on Remote_Host_IP:
The system cannot find the file specified.
This is probably occurring since it is searching for the file on the remote host while the file is located on the local PC thus not finding the file.
I do not want to make any manual intervention on the remote host.
After trying hard to find the correct commands on the net I cannot solve this issue.
If you want you can try this:
PsExec.exe #pc_list.txt >>pc_log.txt -c D:\PC\pc.bat
Where:
pc_list.txt is a list of all your PC in your network
pc_log.txt is a log
D:\PC\pc.bat is the path where your script is
You can schedule it from a server that has access on all your network with task scheduler
Try this.
Psexec \\remotehost -u username -p password -c local_path\file.bat
Run it as:
PsExec \Remote_Host_IP -u Username -p Password cmd/c "C:\Path_to_batch_file\Batch.bat"
This should fix it

Using psexec.exe in jenkins, handle is invalid

I am using Jenkins on a Windows7 system. I would like to use it to execute a batch script on a remote Windows system. The batch script will be used to flash a development board and run some tests. I came across psexec.exe. That works well through a command prompt window--I can connect and run the script without any issues, but when I try to have Jenkins do it, I get the following output:
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
The handle is invalid.
Connecting to ABCDEFG...
Couldn't access ABCDEFG:
Connecting to ABCDEFG...
Build step 'Execute Windows batch command' marked build as failure
The command I am using in both cases is:
psexec.exe \\ABCDEFG -u "DOMAIN\username" -p "password" "C:\test.bat"
The user associated with username has administrator privileges on the remote system (ABCDEFG is not the real name of the system).
Can anyone help me figure out why it is not working through Jenkins? Or, is there an easier/better way to execute a batch script on a remote Windows system through Jenkins?
Thanks to all your help, especially Technext, I have a solution.
I needed run "services.msc", find "Jenkins", right click on it, and go to "Properties". Once the properties windows appeared, I had to click the "Stop" button to stop Jenkins, open the "Log On" tab, enter in my username and password (the username I used when running through command prompt), and start Jenkins again. That got rid of the "handle is invalid" message in Jenkins.
Update:
A better solution was to go onto the system I was using psexec.exe to get onto, go to Control Panel > User Accounts > Give other users access to this computer. Click on "Add..." and type in the username and domain Jenkins uses to run its commands (to find this, open your Jenkins in a browser window, go to Manage Jenkins > System Information and look for USERNAME and USERDOMAIN under Environment Variables). Make sure you give it Administrator rights. Then click ok. Now psexec.exe shouldn't have the "handle is invalid" issue.
Sorry, I don't have enough reputation for comments, but is the single \ a typo? Since
The handle is invalid.
probably means that the computer address is invalid. Try
psexec.exe \\ABCDEFG -u "DOMAIN\username" -p "password" "C:\test.bat"
Notice the two backslashes to access a locally mapped computer.
otherwise if that does not work i recommend the # tag
psexec.exe #servername.txt -u "DOMAIN\username" -p "password" "C:\test.bat"
where #servername.txt is a textfile containing only the servernames, one per line. The file parameter handles the formatting of \
ex servername.txt
ABCDEFG
COMPUTER2
EDIT: also found after some quick googling that it can be related to windows security.
Check out that a simple restart of the remote machine doesn't solve the problem. Also, adding parameters -h and -accepteula may help. Modified command:
psexec.exe \\ABCDEFG -u "DOMAIN\username" -p "password" -h -accepteula "C:\test.bat"
I execute below code from Jenkins pipeline groovy script to connect dynamically created VM as a resource on Jenkins master. Below code connect dynamically created VM as resource on Jenkins master with 4 executors. You can change the number of executors based on your requirement.
bat label: 'ConnectResource', script: """
#echo OFF
C:\\apps\\tools\\psexec \\\\${machine_ip} -u ${machine_ip}\\${machine_username} -p ${machine_password} -accepteula -d -h -i 1 cmd.exe /c "cd C:\\apps\\jenkins\\ & java -jar C:\\apps\\jenkins\\swarm.jar -master http://pnlv6s540:8080 -username ${jenkins_user_name} -password ${jenkins_user_password} -name ${machine_ip}_${BUILD_NUMBER} -labels ${machine_ip}_${BUILD_NUMBER} -deleteExistingClients -disableClientsUniqueId -executors 4" & ping 127.0.0.1 -n 60 > nul
"""

Resources