except Exception,e:
print e
return False
3、还有一点需要注意的是powershell脚本里最后必须要调用一下自己的函数,并且函数要有返回值,以便python能接收powershell脚本返回的结果,同时powershell脚本调用函数传参的方式是args[0],args[1]等等,然后在python里的args里传入对应的参数。
如果需要将策略设置为原来的默认状态,在powershell脚本最后加上:Set-ExecutionPolicy Restricted
python远程调用bat执行命令
1、首先安装python的wmi包
2、远程调用bat如下:
# -*- coding: utf-8 -*-
import wmi,json
import time
logfile = 'logs_%s.txt' % time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())
#远程执行bat文件
def call_remote_bat(ipaddress,username,password):
try:
#用wmi连接到远程服务器
conn = wmi.WMI(computer=ipaddress, user=username, password=password)
filename=r"D:appsautorun.bat" #此文件在远程服务器上
cmd_callbat=r"cmd /c call %s"%filename
conn.Win32_Process.Create(CommandLine=cmd_callbat) #执行bat文件
print "执行成功!"
return True
except Exception,e:
log = open(logfile, 'a')
log.write(('%s, call bat Failed!rn') % ipaddress)
log.close()
return False
return False
if __name__=='__main__':
call_remote_bat(computer="192.168.1.2", user="testuser", password="testpwd")










