You need to debug the situation.
You have a script, then something (what is Jenkins?) Runs it on a remote PC, once it works, once it fails.
Is it deterministic? When he fails, does it always fail?
How does this happen?
You need to get a better idea of ​​how / if the script fails.
Here is what I would like to do to better understand that this fails.
Can you run the script several times?
From the comments it seems that you run the script every hour, can you run it 3/4/5 times in a row, for every hour?
This will help you determine how this will happen: if you run it 5 times, does it work every time? Does he fail, does he fail 5 times in a row?
Is it possible to use different script?
You can create several similar but simpler scenarios.
This way you can try your script using RMDIR, then another script with a simple DIR command (only if the script start / connect mechanism works), and then another script with a simple ECHO command (so it doesent need to be accessed to any files / folder)
Run debug scripts on local PC
Then you can simultaneously run other scripts that run on the LOCAL PC (and not on the remote, where you need to run RMDIR) that try to access the remote PC using PING or by copying the file from / to a network share ...
Take off the net
You can even set up an instance of Wireshark that registers the entire packet sent between two PCs, this can be useful for analyzing / eliminating network problems.
You clearly need to track / record everything.
With this information, perhaps you / we can better understand where the problem is.
=======================================
UPDATE 1 - Record some journal
=======================================
Perhaps you can try using the following modified scripts to have some log files. These scripts will create 2 log files, one on the remote PC (containing the remote command message) and one on the local PC (containing any message from PsExec)
(you will need to configure the path where the log file is 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 next without / i
Are you sure you need the / I options for CMD? On my PC, this does not work if I use the / I options ...
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 tests on my computers, I saw that PsExec installs the service on a remote PC to run the command remotely. (It is called PsExecSvc.exe, installed in c: \ windows \ on the WinXP computer, which I use for this test)
Remotely installing / uninstalling this temporary service to execute a command can certainly be one of the possible “failure points” that generate an error.
If so, then you should be able to track this by looking at LocalComputer.log, which will contain the message / error from PsExec.
As stated in my previous tip, I will also try to schedule a 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 using WMI
=====================================
You can try to run a remote command using WMI
wmic /node:%HOSTNAME% /user:%USERNAME% /password:%PASSWORD% process call create "CMD /C RMDIR /S /Q e:\SomeDir"
When using WMI, you must be sure that Windows Firewall is not blocking your command. (when I tried to run the remote command with WMIC, a notification about the Windows firewall appeared on my Win 7 computer)
(I have instructions for using WMIC here )