Python中調(diào)用PowerShell、遠(yuǎn)程執(zhí)行bat文件實(shí)例
python調(diào)用本地powershell方法
1、現(xiàn)在準(zhǔn)備一個(gè)簡(jiǎn)陋的powershell腳本,功能是測(cè)試一個(gè)IP列表哪些可以ping通:
function test_ping($iplist)
{
foreach ($myip in $iplist)
{
$strQuery = "select * from win32_pingstatus where address = '$myip'"
# 利用 Get-WmiObject 送出 ping 的查詢
$wmi = Get-WmiObject -query $strQuery
if ($wmi.statuscode -eq 0)
{
return "Pinging`t$myip...`tsuccessful"
}
else
{
return "Pinging`t$myip...`tErrorCode:" + $wmi.statuscode
}
}
}
test_ping args[0]
python簡(jiǎn)陋的調(diào)用方法:
# -*- coding: utf-8 -*-
import subprocess
def python_call_powershell(ip):
try:
args=[r"powershell",r"D:\jzhou\test_ping.ps1",ip] #args參數(shù)里的ip是對(duì)應(yīng)調(diào)用powershell里的動(dòng)態(tài)參數(shù)args[0],類似python中的sys.argv[1]
p=subprocess.Popen(args, stdout=subprocess.PIPE)
dt=p.stdout.read()
return dt
except Exception,e:
print e
return False
if __name__=="__main__":
ip=["1.1.1.1","2.2.2.2","3.3.3.3"]
print python_call_powershell(ip)
可能會(huì)報(bào)下面的錯(cuò)誤(如果服務(wù)器本身開(kāi)啟了運(yùn)行了powershell策略的權(quán)限可能沒(méi)有這個(gè)問(wèn)題):
第二種調(diào)用方法可以解決這個(gè)方法
2、調(diào)用時(shí)設(shè)置powershell執(zhí)行策略,這種方法一旦將策略設(shè)置好后,后面就通用了,如果需要的話再在powershell腳本最后加上已經(jīng)將策略改回去
def python_call_powershell(ip):
try:
args=[r"C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe","-ExecutionPolicy","Unrestricted", r"D:\jzhou\test_ping.ps1",ip]
p=subprocess.Popen(args, stdout=subprocess.PIPE)
dt=p.stdout.read()
return dt
except Exception,e:
print e
return False
3、還有一點(diǎn)需要注意的是powershell腳本里最后必須要調(diào)用一下自己的函數(shù),并且函數(shù)要有返回值,以便python能接收powershell腳本返回的結(jié)果,同時(shí)powershell腳本調(diào)用函數(shù)傳參的方式是args[0],args[1]等等,然后在python里的args里傳入對(duì)應(yīng)的參數(shù)。
如果需要將策略設(shè)置為原來(lái)的默認(rèn)狀態(tài),在powershell腳本最后加上:Set-ExecutionPolicy Restricted
python遠(yuǎn)程調(diào)用bat執(zhí)行命令
1、首先安裝python的wmi包
2、遠(yuǎn)程調(diào)用bat如下:
# -*- coding: utf-8 -*-
import wmi,json
import time
logfile = 'logs_%s.txt' % time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())
#遠(yuǎn)程執(zhí)行bat文件
def call_remote_bat(ipaddress,username,password):
try:
#用wmi連接到遠(yuǎn)程服務(wù)器
conn = wmi.WMI(computer=ipaddress, user=username, password=password)
filename=r"D:\apps\autorun.bat" #此文件在遠(yuǎn)程服務(wù)器上
cmd_callbat=r"cmd /c call %s"%filename
conn.Win32_Process.Create(CommandLine=cmd_callbat) #執(zhí)行bat文件
print "執(zhí)行成功!"
return True
except Exception,e:
log = open(logfile, 'a')
log.write(('%s, call bat Failed!\r\n') % ipaddress)
log.close()
return False
return False
if __name__=='__main__':
call_remote_bat(computer="192.168.1.2", user="testuser", password="testpwd")
相關(guān)文章
PowerShell中使用Get-ChildItem命令讀取目錄、文件列表使用例子和小技巧
這篇文章主要介紹了PowerShell中使用Get-ChildItem命令讀取目錄、文件列表使用例子和小技巧,比如只讀取目錄列表的方法,只讀取文件列表的方法,需要的朋友可以參考下2014-08-08PowerShell檢查網(wǎng)卡狀態(tài)和對(duì)應(yīng)的電源設(shè)置
這篇文章主要介紹了PowerShell檢查網(wǎng)卡狀態(tài)和對(duì)應(yīng)的電源設(shè)置,本文直接給出了實(shí)現(xiàn)方法和使用命令及輸出結(jié)果,需要的朋友可以參考下2015-03-03PowerShell腳本實(shí)現(xiàn)添加、修改任務(wù)計(jì)劃的例子
這篇文章主要介紹了PowerShell腳本實(shí)現(xiàn)添加、修改任務(wù)計(jì)劃的例子,PowerShell操作、設(shè)置任務(wù)計(jì)劃實(shí)例,需要的朋友可以參考下2014-08-08PowerShell 獲取系統(tǒng)信息的函數(shù)
如果你要得到本地或遠(yuǎn)程的使用配置信息,又不想浪費(fèi)太多的解決時(shí)間??梢栽赑owershell中使用systeminfo.exe提取數(shù)據(jù)2014-03-03PowerShell中刪除空格、點(diǎn)號(hào)、減號(hào)和換行方法代碼實(shí)例
這篇文章主要介紹了PowerShell中刪除空格、點(diǎn)號(hào)、減號(hào)和換行方法代碼實(shí)例,本文直接給出實(shí)例代碼,需要的朋友可以參考下2015-05-05Powershell直接腳本時(shí)出現(xiàn)無(wú)法加載文件因?yàn)榻箞?zhí)行腳本
Powershell直接腳本時(shí)出現(xiàn)無(wú)法加載文件因?yàn)樵诖讼到y(tǒng)中禁止執(zhí)行腳本,有關(guān)此問(wèn)題的解決方法如下2014-08-08PowerShell小技巧之從函數(shù)中返回多個(gè)值
本文主要是介紹了一個(gè)可以讓一個(gè)PS腳本函數(shù)需要返回多個(gè)值的方法,它能輕松的返回你想要的多個(gè)信息,并將結(jié)果分配給多個(gè)變量,有需要的朋友參考下2014-09-09PowerShell腳本實(shí)現(xiàn)檢測(cè)網(wǎng)絡(luò)內(nèi)主機(jī)類型
這篇文章主要介紹了PowerShell腳本實(shí)現(xiàn)檢測(cè)網(wǎng)絡(luò)內(nèi)主機(jī)類型,本文腳本基本功能可以實(shí)現(xiàn)判斷主機(jī)操作系統(tǒng)類型,如果是域內(nèi)的主機(jī)可以獲取主機(jī)的硬件參數(shù)和性能參數(shù),并判斷是否存在網(wǎng)絡(luò)設(shè)備,需要的朋友可以參考下2014-12-12PowerShell腳本 隨機(jī)密碼生成器(ps隨機(jī)密碼生成器)
這篇文章主要介紹了PowerShell腳本 隨機(jī)密碼生成器,需要的朋友可以參考下2017-10-10