restarting service on a remote server - batch-file

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.

Related

Connect to Remote Server

Is there a way of connecting to remote windows server programmatically in C?
I was looking into MSDN Remote Desktop Services API, but couldn't figure out. Can you provide me an example? I have no idea how to start with that.
NOTE
I don't want to show GUI, I just want to connect to the server without any UI interface and perform couple of tasks.
You can use PowerShell remoting like described in
https://theitbros.com/how-to-remotely-enable-remote-desktop-using-powershell/ to enable RDP on a remote computer.
Then you use an RDP client to connect, a client available in C is in https://github.com/rdesktop/rdesktop/blob/master/rdp.c
Alternatively you can use the command line interface of mstsc.exe from within C code (https://learn.microsoft.com/en-us/windows/win32/shell/launch), i think this is what you want as mstsc commands :
login to remote using "mstsc /admin" with password
Instead of plain RDP you can also use PowerShell sessions for remote managing :
https://learn.microsoft.com/de-de/powershell/scripting/learn/ps101/08-powershell-remoting?view=powershell-7.1#powershell-sessions and https://learn.microsoft.com/de-de/powershell/scripting/learn/remoting/running-remote-commands?view=powershell-7.1
and also more concisely in
https://www.howtogeek.com/117192/how-to-run-powershell-commands-on-remote-computers/
Enter-PSSession -ComputerName COMPUTER -Credential USER

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.

stoping and starting IIS 6.0 or my application pool on IIS using a batch file

I have two windows boxes. one is windows xp(local computer) where i need to write a batch file and the other is windows server 2003 where my IIS is located. Now i need to write a batch file in windows Xp that needs to do these below mentioned operations in it.
Stop the IIS or my application pool on that IIS.
Start the IIS or my application pool on that IIS.
These stop and start need to be performed in two different lines. The windows Server 2003 box has a different user id and password to my windows xp(local computer). As i am new to writing the batch files, after my one day of struggle i am just able to kill the process of my application pool(w3wp.exe) on IIS which is located in the windows server 2003 and i am still not sure what i have written is correct or not. Please help me how to stop and start the IIS which is on server 2003 box, as the batch file should be on the xp box. Thanks in advance for your help.
There is a built-in program for starting/stopping/restarting IIS - iisreset.exe
Calling this program on its own will stop and then restart IIS. If you want to just stop IIS without starting it again, use the following:
iisreset.exe /STOP
And then later you can start it again like this:
iisreset.exe /START
This should work for all versions of IIS, regardless of the operating system version. Just remember that you need to execute it as an administrator.
EDIT: Just noticed that you want to run the command from your XP box to restart IIS on a W2K3 box - there is an optional parameter to specify the target computer:
iisreset.exe ServerNameOrIp /STOP
And:
iisreset.exe ServerNameOrIp /START
finally after a long struggle i made it work by "starting and stopping the iis from remote box with different id and password". I have initially tried runas command which didn't work and then i moved on to ps tools and used this below mentioned commands.
psexec \COMPUTER_NAME -u USERID -p PASSWORD iisreset.exe /START ----> for starting the iis
psexec \COMPUTER_NAME -u USERID -p PASSWORD iisreset.exe /STOP ----> for stoping the iis
#djhayman --- Thanks for your help man.

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.

SQL Server 2008 - Login failed. The login is from an untrusted domain and cannot be used with Windows authentication

I've just installed SQL Server 2008 Developer edition and I'm trying to connect using SQLCMD.exe, but I get the following error:
H:\>sqlcmd.exe -S ".\SQL2008"
Msg 18452, Level 14, State 1, Server DEVBOX\SQL2008, Line 1
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
The SQL Server instance is configured to use SQL Server and Windows Authentication mode. If I specify -U sa then I can log in successfully, but I'd like to use windows authentication. Connecting using SSMS with windows authentication seems to work fine.
I had this issue and it was because the machine running the application isnt trusted for delegation on the domain by active directory. If it is a .net app running under an application pool identity DOMAIN_application.environment for example.. the identity can't make calls out to SQL unless the machine is trusted.
You're not passing any credentials to sqlcmd.exe
So it's trying to authenticate you using the Windows Login credentials, but you mustn't have your SQL Server setup to accept those credentials...
When you were installing it, you would have had to supply a Server Admin password (for the sa account)
Try...
sqlcmd.exe -U sa -P YOUR_PASSWORD -S ".\SQL2008"
for reference,
theres more details here...
In my case, this error was caused by renaming my client machine. I used a new name longer than 13 characters (despite the warning), which resulted in the NETBIOS name being truncated and being different from the full machine name. Once I re-renamed the client to a shorter name, the error went away.
Just tried this:
H:>"C:\Program Files\Microsoft SQL Server\90\Tools\Binn\sqlcmd.exe" -S ".\SQL2008"
1>
and it works.. (I have the Microsoft SQL Server\100\Tools\Binn directory in my path).
Still not sure why the SQL Server 2008 version of SQLCMD doesn't work though..
Your error is quite literally saying "you're trying to use Windows Authentication, but your login isn't from a trusted domain". Which is odd, because you're connecting to the local machine.
Perhaps you're logged into Windows using a local account rather than a domain account? Ensure that you're logging in with a domain account that is also a SQL Server principal on your SQL2008 instance.
Do you specify a user name and password to log on? What exactly is your complete command line?
If you're running on your own box, you can either specify a username/password, or use the -E parameter to log on with your Windows credentials (if those are permitted in your SQL server installation).
Marc
I was getting this error too, although my issue was that I kept switching between two corporate networks via my Virtual Machine, with different access credentials. I had to run the command prompt:
ipconfig /renew
After this my network issues were resolved and I could connect once again to SQL.
Just found this thread and posted an alternative answer (copied below) here:
https://stackoverflow.com/a/37853766/1948625
Specifically on this question, if the dot "." used in the -S value of the command line means the same as 127.0.0.1, then it could be the same issue as the connection string of the other question. Use the hostname instead, or check your hosts file.
Old question, and my symptoms are slightly different, but same error. My connection string was correct (Integrated security, and I don't provide user and pwd) with data source set to 127.0.0.1. It worked fine for years.
But recently I added a line in the static host file for testing purposes (C:\Windows\System32\drivers\etc\hosts)
127.0.0.1 www.blablatestsite.com
Removing this line and the error is gone.
I got a clue from this article (https://support.microsoft.com/en-gb/kb/896861) which talks about hostnames and loopback.
Other possible fix (if you need to keep that line in the hosts file) is to use the hostname (like MYSERVER01) instead of 127.0.0.1 in the data source of the connection string.

Resources