Python调用PowerShell

需求

在server程序运行到一半或者要结束的时候希望它自动记录下当前server的状态,包括有多少个进程,每个占多少CPU,多少内存,系统还剩多少内存之类的。

想法

很简单,既然程序有python脚本,那么就通过python脚本开一个进程运行一些power shell的脚本,把结果写文件。

PowerShell

花了一天时间,啃了点文档,记录一些例子:

  • 获取帮助:get-help
    • get-help *
    • get-help get-help -detailed
  • 获取object的member
    • get-process | get-member
  • 获取进程列表然后输出用CPU时间最多的5个和最少的5个,并把结果写到 D:\a.txt
    • get-process | sort-object CPU -descending | select-object -first 5 -last 5 | out-file D:\a.txt
  • 输出到csv或者xml
    • get-process | Export-Csv D:\ccc.csv
    • get-process | Export-Clixml d:\ccc.xml
  • 输出为HTML
    • get-service | convertto-html -property name,status | out-file D:\ab.html
  • 输出带颜色
    • write-host "aaa" -ForegroundColor red -BackgroundColor blue
  • 把当前的service按satus排序,输出service的名字和satus,并且根据status的状态用不同颜色输出
    • get-service | sort-object status | foreach-object{if ($_.status -eq "stopped"){write-host $_.Name $_.status -ForegroundColor red} elseif($_.status -eq "Running"){write-host $_.Name $_.status -ForegroundColor green}}
  • 文件操作:获取power shell的drive
    • get-psdrive
  • 文件操作:创建一个新的drive
    • new-psdrive -name testps -psprovider filesystem -root d:\testps
  • 列出所有.py文件
    • get-childitem *.py
    • Get-ChildItem | Where-Object {$_.extension -eq ".xml"}
  •  文件操作:根据扩展名分组,并且按组里的文件数量排序输出
    • Get-ChildItem | group-object extension | sort-object Count -descending
  • 文件操作: 统计文件夹里的所有xml文件,但是最终只输出所有xml文件的大小之和
    • Get-ChildItem *.xml | Measure-Object length -sum -average -maximum -minimum | foreach-object {write-host $_.sum}
  • 删除所有.py文件
    • remove-item *.py
  • 给文件夹里的每个文件按扩展名创建一个文件夹
    • Get-ChildItem | sort-object extension -Unique | ForEach-Object {new-item $_.extension -type directory}
  • 把文件夹里的文件都挪到上面创建的文件夹里去
    • Get-ChildItem | Where-Object {$_.GetType() -notmatch "d"} | ForEach-Object {Move-Item $_ $_.Extension}
  • 操作wmi object
    • Get-WmiObject -class win32_computersystem
    • (Get-WmiObject -class win32_computersystem).UserName
    • Get-WmiObject -class win32_desktop -ComputerName keke-pc3
  • 操作.NET的object,访问一个rss地址并且打印内容
  • 操作COM object,创建并写Excel文档
    • PS testps:\> $a = new-object -comobject excel.application
    • PS testps:\> $b = $a.Workbooks.Add()
    • PS testps:\> $c = $b.Worksheets.Item(1)
    • PS testps:\> $c.Cells.Item(1,1) = "windows powershiell rocks"
    • PS testps:\> $a.Visible = $True
    • PS testps:\> $b.SaveAs(".\Test.xls")
    • PS testps:\> $a.Quit()
  • 操作COM object,调用IE访问一个网站
    • PS testps:\> $ie = new-object -comobject InternetExplorer.application
    • PS testps:\> $ie.Visible = $True
    • PS testps:\> $ie | get-member
    • $ie.Navigate("http://www.baidu.com")
  • 操作eventlog
    • get-eventlog -list
    • get-eventlog system -newest 3 | format-list
  • 操作WMI获取电脑信息

Python调用PowerShell

不想引入其他第三方库,那么就开一个子进程,让子进程自己运行powershell并且写文件

关于python调用子进程的文档在这里:

http://docs.python.org/2.7/library/subprocess.html?highlight=subprocess#subprocess

这里列一些要点:

  • subprocess是用来取代os... popen2... commands...的
  • subprocess.call会导致当前进程阻塞直到得到子进程返回值
  • subprocess.check_call和subprocess.call一样,区别在于如果子进程返回非0,那么会抛出CalledProcessError
  • subprocess.check_output返回的不是返回值而是子进程的output,如果子进程返回值非0,也会抛出CalledProcessError。
  • subprocess.PIPE: 给stdin, stdout, stderr指定一个管道
  • subprocess.STDOUT:可以用在stderr上,让子进程的错误也输出在stdout流中
  • subprocess.Popen是那个真正工作的类
    • Popen.poll()
    • Popen.wait()
    • Popen.communicate(input=None)  把input给子进程,返回的是子进程的(stdoutdata, stderrdata)
    • Popen.send_signal()
    • Popen.terminate()
    • Popen.kill()

 

转载于:https://www.cnblogs.com/dbbs/archive/2012/11/26/2784658.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值