从Windows Scheduler运行批处理文件时,psexec无法识别为内部或外部命令

时间:2019-08-26 12:01:59

标签: windows scheduled-tasks psexec

我有一个.bat文件来启动Windows服务。当我手动运行它时,它工作正常,但如果从任务计划程序运行它,则会出现以下错误。

'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 . . .

我尝试了以下情况

  • 尝试过psexec64.exepsexec.exe
  • 在环境变量中添加了psexec路径

仍然没有运气

@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;

1 个答案:

答案 0 :(得分:0)

按如下所示修改了代码,并且可以正常工作

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

                                  )
                                  )
                                  )