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)
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.Location, s.Caption, s.Command)