1. 获取处理器ID、类型及内存大小
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)
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)
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
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"
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))
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()
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
import wmi c = wmi.WMI () for s in c.Win32_StartupCommand (): print "[%s] %s <%s>" % (s.Location, s.Caption, s.Command)