博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python Module_subprocess_调用 Powershell
阅读量:6277 次
发布时间:2019-06-22

本文共 6566 字,大约阅读时间需要 21 分钟。

目录

前言

使用Python内建的subprocess模块,能够实现外部程序的调用。如果你的工作环境是Windows系统,那么Python+Powershell的组合会为你的工作带来极大的便利。本篇介绍一个使用Python做数据处理,Powershell做系统调用的例子。

Powershell call Python

首先在Windows Server 2012 R2中使用Powershell脚本做数据收集,并存放到一个文件中。

#fileName = hd.ps1#function for countdownFunction Countdown($number,$title,$text1,$text2='Pls Call Jmilk'){
Write-Host "Exit the Script after $number seconds" -ForegroundColor Red $Countdown = $number for($PercentComplete = $Countdown; $PercentComplete -ge 0; $PercentComplete--) { Write-Progress -Activity $title -Status $text1 -CurrentOperation $text2 -SecondsRemaining $PercentComplete ; Sleep -Seconds 1; }}#End Function CountdownWrite-Host "Welcome to use the script to create table for HI & OFR nodes status" -ForegroundColor CyanWrite-Host "Building the file hd.txt...Pls be patient.The script will be auto-Exit after created the hd.txt file" -ForegroundColor Yellow#Change the rdtools path$rdtoolsPath = 'E:\Users\userName\rdtools'cd $rdtoolsPath$cmd = 'commands' #commands of Data-Collection 因为保密条约不能将内部指令对外 cmd /c $cmd | Out-File -FilePath E:\Users\userName\Desktop\hd.txt #在powershell里调用了别的Shell所以需要使用cmd指令来实现环境变量的传递#Out-File 将数据导出到文件Write-Host 'Build Done' -ForegroundColor Green#Powershell call pythonStart-Process python E:\Users\userName\Desktop\hd.py #在收集完数据后调用Python做数据处理#结束并推出PowershellCountdown 60 'Hd.ps1' 'Exiting...' 'Pls go to the next step!'

Python call Powershell

主要使用了subprocess模块,subproocess的详细介绍,点击

#coding:utf8#FileName=hd.pyimport osimport codecsfrom openpyxl.workbook import Workbookfrom openpyxl.writer.excel import ExcelWriterfrom openpyxl.cell import get_column_letterfrom openpyxl.cell import Cellfrom openpyxl import Workbookfrom openpyxl import load_workbookimport subprocess#读取数据收集文件def readFile(fileUrl):    """Read the file and return the file content"""    try:        #unicode file        fileObject = codecs.open(fileUrl,'r',encoding='utf-16')    except unicodeDecodeError:        print "Pls check the encoding for hd.txt whether [unicode]"    else:        print("Unspecified Error,Pls call Jmilk")    try:        fileContent = fileObject.readlines()    finally:        fileObject.close()    return fileContent#数据分类函数def getNodeCountList(readLines):    """Get the different node status type and change the global variable"""    i = 0    for line in readLines:        lineItem = line.split(':')        if lineItem[0] == '---- \r\n':            i += 1            continue        if lineItem[0] == '  Node State':            if lineItem[1] == ' Ready count':                global ReadyCount                ReadyCount[i-1] = int(lineItem[2])            if lineItem[1] == ' OutForRepair count':                global OutForRepairCount                OutForRepairCount[i-1] = int(lineItem[2])            if lineItem[1] == ' HumanInvestigate count':                global HumanInvestigateCount                HumanInvestigateCount[i-1] = int(lineItem[2])#生成Excel表格def createTable():    """Create the HI‘s & OFR nodes status table"""    wb = Workbook()    ws = wb.worksheets[0]    ws.title = u"NodeCount"    for i in list(range(1,26)):            ws.cell("A"+str(i)).value = '%s' % (cluster[i-1])            ws.cell("B"+str(i)).value = '%s' % (HumanInvestigateCount[i-1])            ws.cell("C"+str(i)).value = '%s' % (OutForRepairCount[i-1])            ws.cell("D"+str(i)).value = '%s' % (ReadyCount[i-1])            ws.cell("E"+str(i)).value = '%.2f%s' %((float(HumanInvestigateCount[i-1])/(HumanInvestigateCount[i-1]+OutForRepairCount[i-1]+ReadyCount[i-1]))*100,'%')    wb.save("Hd.xlsx")#Python call powershell 使用powershell实现发送数据处理邮件  def python_call_powershell(bodyStr):        args = [r"C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe","-ExecutionPolicy","Unrestricted",r"E:\Users\userName\Desktop\SendMail.ps1",str(bodyStr)]        ps = subprocess.Popen(args,stdout=subprocess.PIPE)        psReturn = ps.stdout.read()        return psReturnif __name__ == '__main__':    #Change to your user name    user = 'userName'    cluster = []    ReadyCount = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]    OutForRepairCount = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]    HumanInvestigateCount = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]    percentage = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]    fileUrl = 'E:\\Users\\' + user + '\\Desktop\\hd.txt'    if os.path.exists(fileUrl):        readContent = readFile(fileUrl)        getNodeCountList(readContent)        #createTable()    else:        print('Not exist the file!')    for i in list(range(0,24)):                percentage[i] = '%.2f%s' % ((float(HumanInvestigateCount[i])/(HumanInvestigateCount[i]+OutForRepairCount[i]+ReadyCount[i]))*100,'%')    bodyStr = [x for li in [cluster,HumanInvestigateCount,OutForRepairCount,ReadyCount,percentage] for x in li]    for index in list(range(0,24)):                print cluster[index]+'\t',str(HumanInvestigateCount[index])+'\t',str(OutForRepairCount[index])+'\t',str(ReadyCount[index])+'\t'+percentage[index]    print bodyStr    callResult = python_call_powershell(bodyStr)    print callResult

Powershell发送邮件

#fileName = sendMail.ps1#Set secure passwordFunction Set-SecurePwd($storage){
$mysecret = 'mailPassword' $mysecret | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File -FilePath $storage $pw = Get-Content $storage | ConvertTo-SecureString return $pw}#End Function Set-SecurePwd#Sned EmailFunction Send-Email($attach,$body){
#$pwd = Set-SecurePwd $storage #$cred = New-Object System.Management.Automation.PSCredential "mailUsername",$pwd $to = "XXX@XXX.com" $from = "XXX@XXX.com" $cc = "XXX@XXX.com" $sub = "Number of statistics for Node status" $smtp = "SMTP.163.COM" Send-MailMessage -To $to -From $from -cc $cc -Subject $sub -Body $body -BodyAsHtml -SmtpServer $smtp -port 25 -Attachments $attach -Credential $cred -UseSsl if($?) { Write-Host "Sent Successfully!" -ForegroundColor Green } else { Write-Host "Error" -ForegroundColor Red }}#End Function Send-Email#Mail$storage = "E:\Users\userName\Desktop\password.txt"$attach = "E:\Users\userName\Desktop\Hd.xlsx"$data = $args[0] #获取Python传递过来的参数$date = Get-Date$currentTime = "{0:G}" -f $date.AddHours(16)$body = "Report of data deal with" #使用HTML的方式来自定义邮件格式Send-Email $attach $bodySleep 10

最后

在上面这个例子中,使用Powershell做数据收集,Python做数据处理,最后使用Powershell的内建方法Send-MailMessage来发送数据处理报告。实现的过程非常简便。

转载于:https://www.cnblogs.com/jmilkfan-fanguiju/p/7533729.html

你可能感兴趣的文章
数据结构实践——顺序表应用
查看>>
python2.7 之centos7 安装 pip, Scrapy
查看>>
机智云开源框架初始化顺序
查看>>
Spark修炼之道(进阶篇)——Spark入门到精通:第五节 Spark编程模型(二)
查看>>
一线架构师实践指南:云时代下双活零切换的七大关键点
查看>>
ART世界探险(19) - 优化编译器的编译流程
查看>>
玩转Edas应用部署
查看>>
music-音符与常用记号
查看>>
sql操作命令
查看>>
zip 数据压缩
查看>>
Python爬虫学习系列教程
查看>>
【数据库优化专题】MySQL视图优化(二)
查看>>
【转载】每个程序员都应该学习使用Python或Ruby
查看>>
PHP高级编程之守护进程,实现优雅重启
查看>>
PHP字符编码转换类3
查看>>
rsync同步服务配置手记
查看>>
http缓存知识
查看>>
Go 时间交并集小工具
查看>>
iOS 多线程总结
查看>>
webpack是如何实现前端模块化的
查看>>