python wmi安装_Python wmi 模块的学习

本文介绍了如何使用Python的wmi模块来管理Windows系统,包括列出所有进程、创建和销毁进程、监控服务状态、查看磁盘空间、监控打印任务、重启远程机器、读取注册表、创建IIS站点、调度任务等操作。此外,还展示了如何在多线程环境中使用WMI以及监控多台机器的电源事件。
摘要由CSDN通过智能技术生成

# -*- coding:utf-8 -*-

import datetime

import os

import wmi

import time

import _winreg

import pythoncom

import threading

import win32api

import win32con

import Queue

c = wmi.WMI()

# 如果要连接远程机器,只需要在WMI构造器中指定远程机器名即可

# c = wmi.WMI("some_other_machine")

# List All Running Processes

# 列出所有正在运行的进程

for process in c.Win32_Process():

print process.ProcessID,process.Name

# List All Running Notepad Processes

# 列出所有正在运行的记事本进程

for process in c.Win32_Process(name="notepad.exe"):

print process.ProcessID,process.Name

# Create And Then Destroy A New Notepad Process

# 创建一个新的记事本进程然后结束它

process_id, return_value = c.Win32_Process.Create(CommandLine="notepad.exe")

for process in c.Win32_Process(ProcessId=process_id):

print process.ProcessID, process.Name

result = process.Terminate()

# Show The Interface For The .Create Method Of A Win32_Process Class

# 显示Win32_Process类的.Create方法的接口

# 注:wmi模块会接受WMI方法的传入参数作为Python的关键字参数,并把传出参数作为一个元组进行返回。

print c.Win32_Process.Create

# Show All Automatic Servieces Which Are Not Running

# 显示没有处于正常运行状态的自启动服务

stopped_services = c.Win32_Service(StartMode="Auto", State="Stopped")

if stopped_services:

for s in stopped_services:

print s.Caption, "service is not running"

else:

print "No auto service stopped"

# Show The Percentage Free Space For Each Fixed Disk

# 显示每个固定磁盘的剩余空间百分比

for disk in c.Win32_LogicalDisk(DriveType=3):

print disk.Caption, "%0.2f%% free" % (100.0 * long(disk.FreeSpace) / long(disk.Size) )

# Run Notepad, Wait Until It's Closed And Then Show Its Text

# 运行记事本,等它关闭之后显示它里面的文字

# 注:这个例子是运行一个进程并且知道它什么时候结束,而不是去处理输入到记事本里面的文字。\

# 所以我们只是简单的用记事本打开一个指定文件,等到用户完成输入并关闭记事本之后,显示一下它的内容。

filename = r'E:\Tools\test.txt'

process = c.Win32_Process

process_id, result = process.Create(CommandLine="notepad.exe " + filename)

watcher = c.watch_for(

notification_type = "Deletion",

wmi_class = "Win32_Process",

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值