Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 827
Ed Heal Avatar asked Mar 20 '14 15:03

Ed Heal


People also ask

Does PsExec need admin rights?

To start using PsExec, just close the existing PowerShell console and launch a new one. If you want to use it in a command prompt, you can launch a command prompt. Whichever you choose, just make sure you launch an elevated session since PsExec requires administrator privileges to run programs on remote computers.


1 Answers

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)

like image 92
Max Avatar answered Oct 15 '22 01:10

Max