使用Python中的psExec在远程计算机上运行批处理文件

时间:2014-08-12 17:24:46

标签: python python-2.7 batch-file psexec

我可以像这样

从命令提示符运行批处理文件
PsExec.exe \\remoteMachine "C:\Users\admin\test.bat"

我正在尝试使用python

调用相同的上述命令
remoteCommand = r'C:\Users\username\test.bat' 
argList = ["PsExec.exe", remoteCommand]
out = subprocess.check_output(argList)
print "Output: ", out

但它会抛出如下所示的错误

The system cannot find the file specified.

PsExec could not start C:\Users\admin\test.bat:
.
.<Stacktrace present here>
.
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['PsExec.exe', 'C:\\Users\\admin\\test.bat']' returned non-zero exit status 2

2 个答案:

答案 0 :(得分:0)

如果您所做的只是在远程计算机上运行批处理文件,则可以使用WMI而不是PSExec:

connection = wmi.WMI(ip, user='username', password='password')
startup = connection.Win32_ProcessStartup.new(ShowWindow=1)
connection.Win32_Process.Create(CommandLine=batcmd, ProcessStartupInformation=startup)

其中&#34; batcmd&#34;是远程计算机上批处理文件的完整本地路径(例如 - &#34; C:\\ directory \\ something.bat&#34;)。

答案 1 :(得分:0)

在您的参数列表中,您没有指明您正在呼叫的远程计算机。也许将你的args列表更改为:

argList = ["PsExec.exe", "\\\\remotemachine", remoteCommand]
相关问题