Using psexec on a bat file that contains psservice - windows

I have a bat file that tries to stop a service on a remote machine, the file contains the following
psservice \\remoteServerName -u domainName\userName -p password stop serviceName
where userName is a name of a user with Admin privileges on the remote machine.
If I run the last line using cmd then the requested service does stop.
I, however, run the bat file using psexec, since it contains more operation than just stopping the service. This is where my problem occurs:
If I run
psexec -u domainName\userName -p passsword batFilePath
the cmd window seems to get stuck.
But if I run
psexec batFilePath
then the psservice executes correctly.
My problem is that I need the user "domainName\userName" to be able to run the other commands in the bat file.
What can I do ? Why does the cmd got stuck when I gave psexec the parameters of the userName and password ?

Related

Can't force psexec to use the given user account

I used to use runas for running my app under another user account. Now I need to pass the password through as well. I found that psexec is an easy way to do it.
So the batch file contains:
#echo off
psexec my.exe -u hostname\user -p password
The problem is my.exe is still being initiated under the user I'm currently logged onto the system with and not under the one declared in the cmd above.
the program must be the last argument
try this :
psexec -u hostname\user -p password my.exe

run command as administrator on remote windows machine

Situation: Running the bat file on windows machine:
1. When I double click the bat file: Bat running is failed.
2. When I right click on bat file and run as administrator: Bat run is successful.
Now I have to run this bat file successfully from remote machine.
What I did:
1. Installed freeSSDd on remote machine and configured administrator user on freeSSHd to access shell and SFTP.
2. Now I am able to login to the remote machine using putty.
Problem:
I am not able to run the bat file successfully. How can I achieve this?
I also used runas /savecred /user:administrator C:/install.bat, but It didn't helped.
There is a way to get this working without any 3rd party software.
You have to create a task on the remote machine using the windows task scheduler which simply executes the desired command. There is an option where you can tell the scheruler to run a bat with a specific account. Enter an admin account and the password and check the "run with highest privileges" box. Leave "Triggers" empty, go to "Settings" and check the "Allow task to be run on demand" box. That's it!
Now when you want to run your file from a different location do
SCHTASKS /RUN /S <RemoteServerName> /U username /P password /TN "<task name>"
If you don't want to enter username and password each time you can adept the user policy (e.g. add the calling machine to the trusted list of the server).
If you have installed an ssh daemon, then you can run your BAT in a remote shell, but you remote shell may open up in something other than CMD.COM. I use cygwin to set up sshd and then from a remote machine, if I ssh in to run a command, it is using cygwin's bash. I can run a BAT file, but need to call CMD first:
ssh WINDOWS_SERVER "cmd /C D:\PATH_TO_BAT\BATCHFILE.BAT"
But there are some pieces missing here. I looked briefly at the Freesshd page and saw only graphical interfaces. Does freesshd support remote command execution, or just secure fire transfer? And what sort of shell get executed on the windows server when you run it?
cygwin is an entire Linux subsystem that runs under Windows and includes an sshd server, but might be a bit much for someone starting out: https://cygwin.com/
\n makes a powershell remote server that listens on port 22 (ssh) and dumps you into a powershell prompt, you can then use my steps above to call CMD from powershell, versus a bash shell.
http://www.powershellserver.com/

psshutdown fails unless run from admin cmd prompt

Ok here's an interesting problem I've run into. I'm attempting to reboot some computers remotely using psshutdown and getting access denied errors unless I run the cmd from a cmd prompt that was run as admin. I myself am an admin on my machine as well as on the remote computer so my credentials should work just fine.
Example code:
psshutdown /accepteula \\COMPUTER.DOMAIN.COM -u DOMAIN\USER -p Password -r -t 0
Example output:
Could not start PsShutdown service on COMPUTER.DOMAIN.COM:
Access is denied.
However when run from admin cmd window:
COMPUTER.DOMAIN.COM is scheduled to reboot in 00:00:00.
As kludge-y as it seems, is it possible to use psexec to run psshutdown as an elevated user?
After running around in circles with this I ended up using:
runas /netonly /user:DOMAIN\USERNAME "shutdown -m \\XXX.XXX.XXX.XXX -r -f -t 0"
Which isn't ideal but got the job done.
Ok, try this. It will open an external window where you can remotely shutdown computers on your network:
shutdown -i
You can type this straight into the CMD window without needing to create a .bat file.
Hope this helps!

psexec giving the system cannot find the file specified

I'm trying to run this from my win7 CMD (as Admin):
psexec IpAddress -u domain\user -p pword c:\Autobatch\ClientJobSender.exe http://reportserver.net:8070/JobExecutor.asmx c:\AutoBatch\backup\trigger.xml
but am getting a "the system cannot find the file specified" error.
I've also tried it this way:
psexec IpAddress -u domain\user -p pword c:\Autobatch\ClientJobSender.exe http://reportserver.net:8070/JobExecutor.asmx c:\AutoBatch\backup\trigger.xml
but get a unknown user or bad password.
What's weird is that I can connect via Remote desktop with the same IP address and user/pass.
Make sure the server has the settings below:
a) Admin share is enabled: run services.msc and check the Service "Server" is enabled
b) Add the key for the share in the registry and restart:
reg add "HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" /v AutoShareServer /t REG_DWORD /d 1
And then use:
psexec \\IpAddress -u domain\user -p pword -w "c:\Autobatch" "ClientJobSender.exe http://reportserver.net:8070/JobExecutor.asmx c:\AutoBatch\backup\trigger.xml"
Actually, I don't see a difference between your 2 command lines. However, the error from the first command is because your syntax is incorrect. You must use
PsExec \\a.b.c.d ...
instead of
PsExec a.b.c.d ...
I got it to work by elevating the local batch file to execute with administrator privileges, that is to say, the terminal window was operating with administrator privileges.
If you're trying to use automation services, you can use the ClientJobSender.exe on the local machine (or on the machine where you set up the scheduling). Just copy the ClientJobSender.exe and the related config file from the install pack to the scheduler server and refer it locally.
You might have the directory path wrong. Try change the .exe path into cmd.exe and cd into your intended path to see if it is actually the correct path.

PsExec hangs when executing batch file in bamboo continous integration server

I have a batch file on my bamboo server, that starts another batch file on an remote computer trough PsExec. If I double click the batch file on the bamboo sever everything is working fine, the batch file on the remote computer starts and PsExec exites with error code 0.
But if I start bamboo then PsExec hangs itself and the batch file on the remote computer don't start.
C:\PsTools>PsExec.exe \\<remote server> -u <user> -p <password> -s -i "C:\batchfile.bat"
PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
This is my output and where PsExec hangs.
I don't understand why it hangs only when bamboo starts the batch.
Hope someone can help me
[ Program output lost when passed through PsExec ]
there's a bug in psexec and it does redirects correctly when is called by everything different by cmd. You have few more options to start a process on remote machine:
1. http://feldkir.ch/xcmd.htm - it cannot be run on localhost
2. WMIC - also cannot be used on localhost , when host and and user/pass are given
2. SCHTASKS
This is an old question but maybe somebody has the same problem.
The first time you run psexec an eula dialog is shown. In order to avoid bamboo hanging with this dialog you may try the argument \accepteula. For instance:
PsExec.exe \\<remote server> -u <user> -p <password> -s -i "C:\batchfile.bat" -accepteula

Resources