PsExec 到远程计算机中的 运行 批处理文件

PsExec to run batch file in remote computer

我使用此代码 C++ 生成器在远程计算机上执行批处理文件

String PsExec = ExtractFilePath(Application->ExeName) + "PSTools\PsExec.exe";
String lcParam = " \"+gl_HostName+" cmd /c  "+TDirectory::GetParent(rootPath)+"\...\File1.bat "+IntToStr(8988) ;

SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = PsExec.c_str();
ShExecInfo.lpParameters = lcParam.c_str();
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_NORMAL;//SW_HIDE ;
ShExecInfo.hInstApp = NULL;

ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
bool res = ShellExecuteEx(&ShExecInfo); // Call to function
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);

没用!

但是,当我 运行 将命令输入控制台时:

\PSTools> PsExec.exe \x.y.z.w \x.y.z.w\.....\File1.bat 8988

它工作正常,运行在远程计算机中进行了这批处理!

我的示例代码有什么问题?

正如 Alex 所说,您没有在主机名中提供足够的斜线。您只提供了 1 个转义斜线,但您需要 2 个:

String lcParam = _D(" \\") + gl_HostName + _D(" cmd /c ") + TDirectory::GetParent(rootPath) + _D("\...\File1.bat ") + IntToStr(8988);

也就是说,您还应该使用 CreateProcess() 而不是 ShellExecuteEx() 来 运行 一个 .exe 文件:

String PsExec = ExtractFilePath(Application->ExeName) + _D("PSTools\PsExec.exe");
String lcParam = _D(" \\") + gl_HostName + _D(" cmd /c ") + TDirectory::GetParent(rootPath) + _D("\...\File1.bat ") + IntToStr(8988);
String CmdLine = AnsiQuotedStr(PsExec, _D('\"')) + _D(" ") + lcParam;

STARTUPINFO si = {0};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_NORMAL;

PROCESS_INFORMATION pi = {0};

if (CreateProcessW(NULL, CmdLine.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
    CloseHandle(pi.hThread);
    WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hProcess);
}