Search code examples
windowsscheduled-taskspsexec

psexec is not recognized as an internal or external command while running a batch file from windows scheduler


I have a .bat file to start the Windows service. When I run it manually it works fine but if I run it from task scheduler it gives the following error.

'psexec64.exe' is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the file sessid.txt.
Press any key to continue . . .

I tried with the following scenarios

  • Tried with both psexec64.exe and psexec.exe
  • Added the psexec path in environment variables

still no luck

@echo off
setlocal enabledelayedexpansion

set username=user1
set usr=bdomain\user1
set password=2018
set machine=192.168.1.16

psexec64.exe \\%machine% -u %usr% -p %password% query session %username%>D:\Service_Start\sessid.txt

set /a counter=0
for /F "tokens=* skip=1" %%a in (sessid.txt) do (
    for %%b in (%%a) do (
        set /a counter+=1
        if !counter! == 3 (
            psexec64.exe \\192.168.1.16 -u bdomain\user1 -p 2018 -i %%b -d net start OracleServiceTest
        )
    )
)
pause;

Solution

  • modified the code as below and it's working

    setlocal enabledelayedexpansion
    
                                      set username=user1
                                      set usr=bdomain\user1
                                      set password=2018
                                      set machine=192.168.1.16
    
                                      D:\PSTools\psexec64.exe \\%machine% -u %usr% -p %password% query session %username%>D:\Service_Start\sessid.txt
    
                                      set /a counter=0
                                      for /F "tokens=* skip=1" %%a in (D:\Service_Start\sessid.txt) do (
                                      for %%b in (%%a) do (
                                      set /a counter+=1
                                      if !counter! == 3 ( 
    
                                       D:\PSTools\psexec64.exe \\192.168.1.16 -u bdomain\user1 -p 2018 -i %%b -d net start OracleServiceTest
    
                                      )
                                      )
                                      )