Run PSEXEC

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Docfxit
Posts: 130
Joined: 12 Nov 2015 12:42

Run PSEXEC

#1 Post by Docfxit » 02 Apr 2020 12:35

I would like to run a program on a remote PC.
I'm signed on to PC#1 at 192.168.168.7 with a user that has admin rights.
I would like to run the program on my LAN on PC#2 at 192.168.168.9. The same user is on this PC with admin rights and the same password.
I'm running a bat file that creates another bat file to run on PC#1.
I run this bat file called UpdateMacriumCreate.bat on PC#1:

Code: Select all

@Echo On
for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do set NetworkIP=%%a
echo Network IP: %NetworkIP%
(
echo: @Echo On
echo: If not exist "C:\Dnload\9xAddons\Macrium" md "C:\Dnload\9xAddons\Macrium"
echo: cd "C:\Dnload\9xAddons\Macrium"
Echo: Echo "Test 1>UpdateMacrium.log"
Echo: tasklist /fi "imagename eq UpdateMacrium.exe" 2>NUL | find /I /N "no tasks are running">NUL
Echo: if "%errorlevel%"=="0" ^(
Echo:    echo Task Found
Echo: 	taskkill /f /im UpdateMacrium.exe
Echo: ^)
Echo: Echo "Test 2>>UpdateMacrium.log"
Echo: tasklist /fi "imagename eq ReflectDLHF.exe" 2>NUL | find /I /N "no tasks are running">NUL
Echo: if "%errorlevel%"=="0" ^(
Echo:    echo Task Found
Echo: 	taskkill /f /im ReflectDLHF.exe
Echo: ^)
Echo: Echo "Test 3>>UpdateMacrium.log"
echo: xcopy /y /f "\\%NetworkIP%\Dnload\9xAddons\Macrium\ReflectDLHF.exe" "C:\Dnload\9xAddons\Macrium\ReflectDLHF.exe"
Echo: Echo "Test 4>>UpdateMacrium.log"
echo: xcopy /y /f "\\%NetworkIP%\Dnload\9xAddons\Macrium\UpdateMacrium.exe" "C:\Dnload\9xAddons\Macrium\UpdateMacrium.exe"
Echo: Echo "Test 5>>UpdateMacrium.log"
Echo: Call UpdateMacrium.exe
) > %~dp0UpdateMacriumRun.bat

c:\batch\psexec.exe @"C:\Batch\PushFileAndRunItComputerList.txt" -i -c -v -s "C:\Dnload\9xAddons\Macrium\UpdateMacriumRun.bat" 

cmd /k
It creates this bat file called UpdateMacriumRun.bat on PC#1:

Code: Select all

 @Echo On
 If not exist "C:\Dnload\9xAddons\Macrium" md "C:\Dnload\9xAddons\Macrium"
 cd "C:\Dnload\9xAddons\Macrium"
 Echo "Test 1>UpdateMacrium.log"
 if "0"=="0" (
    echo Task Found
 	taskkill /f /im UpdateMacrium.exe
 )
 Echo "Test 2>>UpdateMacrium.log"
 if "0"=="0" (
    echo Task Found
 	taskkill /f /im ReflectDLHF.exe
 )
 Echo "Test 3>>UpdateMacrium.log"
 xcopy /y /f "\\192.168.168.7\Dnload\9xAddons\Macrium\ReflectDLHF.exe" "C:\Dnload\9xAddons\Macrium\ReflectDLHF.exe"
 Echo "Test 4>>UpdateMacrium.log"
 xcopy /y /f "\\192.168.168.7\Dnload\9xAddons\Macrium\UpdateMacrium.exe" "C:\Dnload\9xAddons\Macrium\UpdateMacrium.exe"
 Echo "Test 5>>UpdateMacrium.log"
 net use U: "\\192.168.168.7\Dnload\9xAddons\Macrium"
 Call UpdateMacrium.exe
When I run the create bat file it brings up a CMD window and shows that it does run:

Code: Select all

PsExec v2.2 - Execute processes remotely
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com

\\192.168.168.9:

cmd.exe started on 192.168.168.9 with process ID 3952.

C:\Dnload\9xAddons\Macrium>cmd /k
When I look on PC#2 at 192.168.168.9 it shows that it's running in Task Manager.
The log file "UpdateMacrium.log" is not created.
This is part of the cmd window:

Code: Select all

C:\Dnload\9xAddons\Macrium>Echo "Test 4>>UpdateMacrium.log"
"Test 4>>UpdateMacrium.log"
C:\Dnload\9xAddons\Macrium>xcopy /y /f "\\192.168.168.7\Dnload\9xAddons\Macrium\
UpdateMacrium.exe" "C:\Dnload\9xAddons\Macrium\UpdateMacrium.exe"
\\192.168.168.7\Dnload\9xAddons\Macrium\UpdateMacrium.exe -> C:\Dnload\9xAddons\
Macrium\UpdateMacrium.exe
1 File(s) copied
C:\Dnload\9xAddons\Macrium>Echo "Test 5>>UpdateMacrium.log"
"Test 5>>UpdateMacrium.log"
C:\Dnload\9xAddons\Macrium>net use U: "\\192.168.168.7\Dnload\9xAddons\Macrium"
System error 85 has occurred.
The local device name is already in use.
C:\Dnload\9xAddons\Macrium>Call UpdateMacrium.exe
I do not have the windows firewall running on either PC.
I'm running Win7 32bit Ultimate on PC#1
I'm running Win7 64bit Ultimate on PC#2
Bitdefender did stop it from running on PC#2 the first time and I added that to exceptions.
I have turned off the Bitdefender Antivirus on PC#2

I have fixed most of the problems.
I have two problems left.
1. Echo "Test 5>>UpdateMacrium.log" Is not writing out the file.
2. net use U: "\\192.168.168.7\Dnload\9xAddons\Macrium"
System error 85 has occurred.
The local device name is already in use.

Can anyone help me figure out the solutions for these two problems?

Thank you,
Docfxit
Last edited by Docfxit on 03 Apr 2020 14:47, edited 1 time in total.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Run PSEXEC

#2 Post by aGerman » 03 Apr 2020 10:43

There seem to be a couple of un-escaped characters in your echo commands
E.g. the red marked here:
Echo: tasklist /fi "imagename eq UpdateMacrium.exe" 2>NUL | find /I /N "no tasks are running">NUL
Did you ever try to create the file locally in order to investigate what script content is written?

Can't tell about other errors caused by psexec. Just one note: Belonging to the admin group doesn't mean that you run the script elevated.

Steffen

Docfxit
Posts: 130
Joined: 12 Nov 2015 12:42

Re: Run PSEXEC

#3 Post by Docfxit » 03 Apr 2020 12:23

aGerman wrote:
03 Apr 2020 10:43
There seem to be a couple of un-escaped characters in your echo commands
E.g. the red marked here:
Echo: tasklist /fi "imagename eq UpdateMacrium.exe" 2>NUL | find /I /N "no tasks are running">NUL
Did you ever try to create the file locally in order to investigate what script content is written?

Can't tell about other errors caused by psexec. Just one note: Belonging to the admin group doesn't mean that you run the script elevated.

Steffen
Thanks Steffen,

That line is currently working.

This line is not working;
Echo "Test 5>>UpdateMacrium.log" Is not writing out the file.

Docfxit

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Run PSEXEC

#4 Post by aGerman » 04 Apr 2020 06:04

That line is currently working.
Nope. It's not working at all.
Running this piece of code ...

Code: Select all

@echo off &setlocal
(
Echo: tasklist /fi "imagename eq UpdateMacrium.exe" 2>NUL | find /I /N "no tasks are running">NUL
)>"foo.bat"
... results in an empty foo.bat because the text is redirected to NUL in the Echo line.

Running this instead ...

Code: Select all

@echo off &setlocal
(
Echo: tasklist /fi "imagename eq UpdateMacrium.exe" 2^>NUL ^| find /I /N "no tasks are running"^>NUL
)>"foo.bat"
... writes ...

Code: Select all

 tasklist /fi "imagename eq UpdateMacrium.exe" 2>NUL | find /I /N "no tasks are running">NUL
... into foo.bat.
That's the reason why I strongly recommend you to make tests locally in order to investigate what you're doing.
Echo "Test 5>>UpdateMacrium.log" Is not writing out the file.
No wonder. The whole redirection is quoted and thus, it's only a string instead of executable code.

Steffen

Docfxit
Posts: 130
Joined: 12 Nov 2015 12:42

Re: Run PSEXEC

#5 Post by Docfxit » 04 Apr 2020 20:37

Hi Steffen,

You are 100% correct. I didn't catch that it wasn't writing the code to the run.bat file.
I am not good with writing .bat scripts. I'm trying to learn. Now that you showed me how to correct the code
Should I change the post above or should I create a new post with the correct code below?

I removed the quotes in this code:

Code: Select all

@echo off &setlocal
(
Echo: Echo Test 1 >%~dp0UpdateMacrium.log
)>"foo.bat"
And nothing gets written out to foo.bat

What is the correct way to get it to write to the foo.bat file?

Thank you very much,
Docfxit

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Run PSEXEC

#6 Post by aGerman » 05 Apr 2020 06:01

What is the correct way to get it to write to the foo.bat file?
Probably

Code: Select all

@echo off &setlocal
(
Echo: Echo Test 1 ^>%%~dp0UpdateMacrium.log
)>"foo.bat"
Batch101 for escaping:
^ to ^^
< to ^<
> to ^>
| to ^|
& to ^&
% to %%

Every single character of those has to be escaped this way, unless they occur in a quoted string.
Also refer to https://ss64.com/nt/syntax-esc.html

Steffen

Docfxit
Posts: 130
Joined: 12 Nov 2015 12:42

Re: Run PSEXEC

#7 Post by Docfxit » 05 Apr 2020 19:39

When I run this bat file:

Code: Select all

 @Echo On
 setlocal enableDelayedExpansion

 set "cmd=tasklist.exe /fi "imagename eq UpdateMacrium.exe" /FI "IMAGENAME eq %1" /FI "PID eq %2""
 for /F "delims=*" %%p in ('!cmd! ^| findstr "%2" ') do (
    echo Task Found
 	taskkill /f /im UpdateMacrium.exe
 )
This is what is produced in the cmd window:

Code: Select all

C:\Dnload\9xAddons\Macrium>set "cmd=tasklist.exe /fi "imagename eq UpdateMacrium.exe" /FI "IMAGENAME eq " /FI "PID eq ""

C:\Dnload\9xAddons\Macrium>for /F "delims=*" %p in ('!cmd! | findstr "" ') do (
echo Task Found
 taskkill /f /im UpdateMacrium.exe
)
FINDSTR: No search strings
ERROR: The search filter cannot be recognized.
How can the code be changed to make this work?

Thank you,
Docfxit

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Run PSEXEC

#8 Post by ShadowThief » 05 Apr 2020 23:28

The code isn't broken, you just never gave it the PID to search for. As written, the script expects two arguments: the application name as it appears in tasklist and the PID of the running task, in that order.

Docfxit
Posts: 130
Joined: 12 Nov 2015 12:42

Re: Run PSEXEC

#9 Post by Docfxit » 06 Apr 2020 10:48

ShadowThief wrote:
05 Apr 2020 23:28
The code isn't broken, you just never gave it the PID to search for. As written, the script expects two arguments: the application name as it appears in tasklist and the PID of the running task, in that order.
I'm trying to figure out if the program is running. I found the original way I was doing it with tasklist it doesn't produce errorlevel so I tried adapting it to the above code from this link:
https://stackoverflow.com/questions/496 ... batch-file

If the program isn't running there won't be a PID. Does anyone know how to test to see if a program is running, and if it is kill it?

Thank you,

Docfxit

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Run PSEXEC

#10 Post by ShadowThief » 06 Apr 2020 17:43

Code: Select all

taskkill /IM UpdateMacrium.exe
If it's running, it gets killed. If it's not running, you get an error message and nothing else happens. You can even stick a 2>nul at the end of the code if you don't want the error message.

Post Reply