python WMI模块的使用实例
2013-03-21 14:26
826 查看
1. 获取处理器ID、类型及内存大小
2. 列出所有进程
3. 列出指定名称的进程
4. 创建然后删除记事本进程
5. 显示共享文件夹及其路径
6. 获取硬盘信息和分区情况
7. 显示为自动,当前未启动的服务
8. 显示磁盘使用百分比
9. 运行记事本,等待直到它关闭,并且显示内容
10. 显示IP和MAC
11. 自启动程序和位置
import wmi w=wmi.WMI() for processor in w.Win32_Processor(): print "Processor ID: %s" % processor.DeviceID print "Process Name: %s" % processor.Name.strip() totalMemSize=0 for memModule in w.Win32_PhysicalMemory(): totalMemSize+=int(memModule.Capacity) print "Memory Capacity: %.2fMB" %(totalMemSize/1048576)
2. 列出所有进程
import wmi c = wmi.WMI () for process in c.Win32_Process (): print "%5s %s" % (process.ProcessId, process.Name)
3. 列出指定名称的进程
import wmi c = wmi.WMI () for process in c.Win32_Process (name="pycharm.exe"): print "%5s %s" % (process.ProcessId, process.Name)
4. 创建然后删除记事本进程
import wmi c = wmi.WMI () 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 process.Terminate()
5. 显示共享文件夹及其路径
import wmi c = wmi.WMI () for share in c.Win32_Share (): print "%6s %s" % (share.Name, share.Path)
6. 获取硬盘信息和分区情况
import wmi c = wmi.WMI () for physical_disk in c.Win32_DiskDrive (): for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"): for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"): print physical_disk.Caption, partition.Caption, logical_disk.Caption
7. 显示为自动,当前未启动的服务
import wmi c = wmi.WMI () 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 services stopped"
8. 显示磁盘使用百分比
import wmi c = wmi.WMI () for disk in c.Win32_LogicalDisk (DriveType=3): print disk.Caption, "%0.2f%% free" % (100.0 * long (disk.FreeSpace) / long (disk.Size))
9. 运行记事本,等待直到它关闭,并且显示内容
import wmi c = wmi.WMI () filename = r"D:\pythonshell\log.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", delay_secs=1, ProcessId=process_id ) watcher () print "This is what you wrote:" print open(filename).read()
10. 显示IP和MAC
import wmi c = wmi.WMI () for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1): print interface.Description, interface.MACAddress for ip_address in interface.IPAddress: print ip_address
11. 自启动程序和位置
import wmi c = wmi.WMI () for s in c.Win32_StartupCommand (): print "[%s] %s <%s>" % (s.Location, s.Caption, s.Command)
相关文章推荐
- Python命令行参数解析模块getopt使用实例
- Python使用email模块对邮件进行编码和解码的实例教程
- python使用urllib2模块获取gravatar头像实例
- python urllib模块的urlopen()的使用方法及实例
- 在python中的socket模块使用代理实例
- 【Python】配置文件解析模块ConfigParser使用实例
- Python fileinput模块使用实例
- Python使用QRCode模块生成二维码实例详解
- 使用Python的urllib和urllib2模块制作爬虫的实例教程
- Python中使用Tkinter模块创建GUI程序实例
- 使用pdb模块调试Python程序实例
- python time模块使用实例
- 【python】使用pickle模块将pickle.dump()、pickle.load()封装实例详解
- Python配置文件解析模块ConfigParser使用实例
- Python CSV模块使用实例
- python使用wmi模块获取windows下硬盘信息的方法
- python使用marshal模块序列化实例
- Python httplib模块使用实例
- Python中使用Tkinter模块创建GUI程序实例