Using PSEXEC to launch remote exe with custom flag - batch-file

I'm trying to launch an exe remotely but I can't get the program to run with the custom flag /r.
psexec -u DOMAIN\Username -p Password -s \\XXX.XXX.XXX.XXX -i "C:\Windows\System32\Program.exe /r"
However the output I get is exited on XXX.XXX.XXX.XXX with error code 87. Which is the error code for an invalid argument.

Turns out the problem was not with the code but the server I was testing on. Shame on me for assuming that Windows would do what it's supposed to. The application had crashed several days ago hence why the cmd was failing. This works perfectly:
psexec -u domain\username -p password -s \\XXX.XXX.XXX.XXX -i "C:\Windows\System32\Program.exe\" /r
Bonus round
After running the cmd the server needs to reboot and that was a pain to get working. I kept getting different error codes and then finally had trouble finding a way to give the server a shutdown reason. Here is my code for that. It reboots(/r), forces the shutdown(/f), time delay of 1 second(/t 1), marks the shutdown reason as unplanned reason 00:00(/d U:00:00).
psexec \\XXX.XXX.XXX.XXX -e -u domain\username -p password shutdown /r /f /t 1 /d U:00:00

Related

Running a HTA file remotely using PSTools

Its almost certain that HTA files are obsolete, but i've found that they are much better than net send / msg.
I'm trying to run a HTA file on a remote machine using PSTools, but instead of it running, it brings back a broken window:
Running the HTA file using CMD (locally) works perfectly though.
My PsExec line:
PsExec.exe -accepteula -i -d \\itwall cmd 'mstha \\intranet\Downloads\VisitorSystemNewMessage.hta asd'
I even tried to run the HTA from a Batch file, but the exact same thing happens.
Any ideas?
It's because the account running the command cannot interact with the session of the remote user.
Use the -s switch to run the HTA using the system account of the remote computer.
Also, you shouldn't need to run cmd. You should be able to just specify mshta.exe then your arguments.
PsExec.exe -accepteula -s -i -d \\itwall mshta.exe \\intranet\Downloads\VisitorSystemNewMessage.hta asd
Edit: To illustrate that this is not an HTA issue. Run the following command:
PsExec.exe -accepteula -i -d \\itwall notepad.exe
Notice you'll have the same black window showing.

Display Batch file output message in local machine

I am using psexec tool to run a batch file in remote machine. Everything is good except that I am unable to see any batch output messages that we usually see in cmd window. I want these msgs on my local machine psexec shell window. Is there anyway to do that?
I am using the command as:
psexec -u admin -p tool#321 \\10.189.21.19 -s -d cmd.exe /c "C:\TEMP\output\batch_script.bat"
What about creating an admin share and outputting the data there?
psexec -u admin -p tool#321 \10.189.21.19 -s -d cmd.exe /c "C:\TEMP\output\batch_script.bat >\\server\logs$\10.189.21.19.txt"

PSEXEC: not recognising remote command

I am able to execute basic operation by triggering batch script on remote location using following command.
psexec -e -h -s -u User -p pass \\10.0.0.240 C:\test.bat
But when the test.bat file is calling other program specific script like somepy.py then I am getting error on master batch file that these are not internal command.
Master batch file in Host computer:
CODE
psexec -e -h -s -u user -p pwd \\10.0.0.240 C:\Users\Desktop\TEST\test.bat
Command inside test.batlocated in remote PC:
cd C:\Users\Desktop\TEST1
impact -batch test_impact_warp_AP.cmd
`pause`
waitfor /t 5 StartNow
REM wait for 5second
echo "Run python script for Warp"
cd C:\Users\Desktop
call warp-python.bat
ipconfig /all
ping google.com
Command inside warp-python.bat present in remote machine:
set PYTHONPATH=C:\Users\Desktop\Python_Reference
cd C:\Users\Desktop\Python_Reference\examples\PYTHON_SCRIPT
python t_capture.py
When I executed the test.bat script directly then warp-python.bat as well as test_impact_warp_AP.cmd executes perfectly without any error.
But when i try to execute test.bat from remote location then *python* and *impact* commands are not recognised. and gives following error:
'Impact' is not recognised as an internal or external Command
But ipconfig/all and ping command is executed perfectly in remote PC
What am I missing in the command line argument such that psexec in not able to executed the command in remote location
Add the psexec.exe to your PATH variable in the environment variable or
give the full path of psexec.exe in your command.
Example:
c:\users\test\Desktop\psexec.exe -e -h -s -u User -p pass \10.0.0.240 C:\test.bat
Make sure C:\test.bat is present in your remote machine.

Psexec to execute batch file,which calls a vbscript,and this vbscript executes test case using qtp on a remote machine

Scenario :
System 1(Remote Machine) :
"abc.bat"
"xyz.vbs"
Both the files are in D Drive
Scenario : "abc.bat" calls "xyz.vbs" and the command is like this: cscript D:\xyz.vbs
"xyz.vbs has script which calls QTP and executes the test script on QTP.
System 2(My Machine) :
I have downloaded PSTools and making use of Psexec command to execute batch file on above remote machine.
In my cmd i have this command.
C:\Program Files\PSTools>psexec -i \System_1's_IP_address -u domain\username -p password D:\abc.bat
When i run this command on my machine, it starts PSexec on remote machine. But QTP is not invoked on the remote machine so execution halts here.
I have checked in task manager.it shows wscript,psexec.
But not QTPro.exe
Is there a solution??
Can somebody please help.
PSEXEC \\<Targetsystem> -u domain\username -p password -i -w D: cmd.exe /c abc.bat
or
place the abc.bat in the same folder as psexec and execute this one:
PSEXEC \\<Targetsystem> -u domain\username -p password -i -c -f abc.bat
(P.S.: a look at PSEXEC /? may help ;))

PsExec and invalid handles

I am trying to use a windows batch script that uses PsExec to execute commands on a remote machine. Periodically it has "invalid handle" and the script then fails.
The script has not altered or indeed either machine.
Does anybody know why this happens as sometimes the scripts runs without a hitch.
Alternatively does anybody know how to run a script on a machine as the local user for that machine with a more reliable technology.
PS Sometimes the first PsExec works and the others fail.
EDIT
The script is just on line (apart from setting the appropriate variables)
PsExec %HOSTNAME% -I -u %USERNAME% -p %PASSWORD% CMD /C RMDIR /S /Q e:\SomeDir
This sometimes works but sometimes fails with "invalid handle"
You need to debug the situation.
You have a script, then something (what is Jenkins?) launch it on a remote PC, sometime it works, sometime it fail.
Is it deterministic?
When it fail does it always fail?
How does it fail?
You need to acquire better knowledge of how/when the script fail.
Here is what I would do to gather better understanding of these fails.
Can you run the script multiple time?
From the comments it seem that you run the script every hours, can you run it 3/4/5 time in a row, for each hours?
This will help you to determine how it fail: if you run it 5 time, does it works every time? it it fail, does it fail 5 times in a row?
Can you try to use different script?
You can create some more similar, but simpler, scripts.
So you can try your script with the RMDIR, then another script with a simple DIR command (just to se if the script launching/connection mechanism works) then another script with a simple ECHO command (so it doesent need to access any files/folder)
Run debug scripts on the local PC
Then, you can simultaneously run other scripts that run on the LOCAL PC (not the remote one where you need to execute the RMDIR) that try to access the remote PC, with a PING, or by copying a file from/to a network share...
Sniff the network
You can even set up a Wireshark instance that log all the packet sent between the 2 PC, this can be helpful to analyse/exclude networking issue.
You clearly need to track/log everything.
With this kind of information maybe you/we can have a better understanding of where the issue is.
=====================================
UPDATE 1 - Record some log
=====================================
Maybe you can try to use the following modified scripts to have some log files.
These script will create 2 log files, one on the remote PC (containing the message of the remotely executed command) and one on the local PC (containing any message from PsExec)
(you'll need to tweak the path where the log file are saved)
psexec %HOSTNAME% -I -u %USERNAME% -p %PASSWORD% CMD /C "RMDIR /S /Q e:\SomeDir >>c:\RemoteComputer.log 2>&1" >>c:\LocalComputer.log 2>&1
or the following one without the /I
Are you sure you need the /I parameters for CMD? On my Pc it doesn't works if I use the /I parameters...
psexec %HOSTNAME% -u %USERNAME% -p %PASSWORD% CMD /C "RMDIR /S /Q e:\SomeDir >>c:\RemoteComputer.log 2>&1" >>c:\LocalComputer.log 2>&1
After some testing on my PCs, I've seen that PsExec install a service on the remote PC to run the command remotely. (It's called PsExecSvc.exe, installed in c:\windows\ on the WinXP PC I'm using for this test)
The remote installation/uninstallation of this temporary service for the command execution can surely be one of the possible "failure point" that generate the error.
If this is the case, then you should be able to track this down by looking at the LocalComputer.log, that will contain the message/error from PsExec.
As stated in my previous advice, I would also try to schedule simpler script like
psexec %HOSTNAME% -u %USERNAME% -p %PASSWORD% CMD /C "dir c:\ >>c:\RemoteComputerDir.log 2>&1" >>c:\LocalComputerDir.log 2>&1
and
psexec %HOSTNAME% -u %USERNAME% -p %PASSWORD% CMD /C "echo SuperEchoTest >>c:\RemoteComputerEcho.log 2>&1" >>c:\LocalComputerEcho.log 2>&1
===================================
UPDATE 2 - Try to use WMI
===================================
You can try to run the remote command by using WMI
wmic /node:%HOSTNAME% /user:%USERNAME% /password:%PASSWORD% process call create "CMD /C RMDIR /S /Q e:\SomeDir"
When you use WMI you need to be sure that windows firewall is not blocking your command. (when I tried to run a remote command with WMIC the windows firewall notification popped up on my Win 7 PC)
(I've the instruction to use WMIC here)
Yes, there is a more reliable technology for executing commands on a remote machine and is called powershell. For example, you can run :
test-connection -computername server01, server02, server12
pings from local computer to several remote computers.
Another very useful command is:
invoke-command -filepath c:\scripts\test.ps1 -computerName Server01
runs the Test.ps1 script on the Server01 computer.
A tutorial gives several examples on how to Run PowerShell Commands on Remote Computers.
A different technology can be found mimicking the Linux world, and using ssh. It's very common with clusters and I have personally used it with Windows Server 2008 R2, so I don't expect any difference on windows 7.
This task is commonly performed with ssh and password-less public key authentication. With it, the only needed information is the IP of the remote server and the public key of the client, stored on the server: only the client with the corresponding private key can connect to it (the keys must be created with ssh-keygen, on the client. The public key is copied to the server)
The server must have the TCP port 22 accessible from outside, in case there are firewalls, NATs,...
In my case I used the ssh server included in Windows SUA, but I suggest you forget them (they are deprecated, and quite cumbersome actually) and give a try to the OpenSSH cygwin server, sshd - even if not officially Microsoft, there is a large community supporting it at least - and occasionally I have used it reliably.
The client ssh command is included in SUA, in cygwin, or you can use putty if you want a lightweight solution on the client (not that cygwin is heavy - just the burden of having a sort of linux emulation that's not needed)
Giving a search for example I have found this post, explaining well the needed steps.

Resources