python取wmi的一些实例

原文地址:python取wmi的一些实例作者:拔草
import wmi
wwmi.WMI()
for processor in w.Win32_Processor():
print "Processor ID: %s" % processor.DeviceID
print "Process Name: %s" % processor.Name.strip()
totalMemSize0
for memModule in w.Win32_PhysicalMemory():
totalMemSize+int(memModule.Capacity)
print "Memory Capacity: %.2fMB" %((totalMemSize+1048575)/1048576

列出所有进程

import wmi
c wmi.WMI ()
for process in c.Win32_Process ():
print process.ProcessId, process.Name

列出所有记事本进程

import wmi
c wmi.WMI ()
for process in c.Win32_Process (name"notepad.exe"):
print process.ProcessId, process.Name

创建然后删除记事本进程

import wmi
c wmi.WMI ()
process_id, return_value c.Win32_Process.Create (CommandLine"notepad.exe")
for process in c.Win32_Process (ProcessIdprocess_id):
print process.ProcessId, process.Name
result process.Terminate ()

列出创建进程的调用方法

import wmi
c wmi.WMI ()
print c.Win32_Process.Create

显示为自动,当前未启动的服务

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 (DriveType3):
print disk.Caption, "%0.2f%% free" % (100.0 * long (disk.FreeSpace) / long (disk.Size))

运行记事本,等待直到它关闭,并且显示内容

import wmi
c wmi.WMI ()
filename r"c:temptemp.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_secs1,
ProcessIdprocess_id
)
watcher ()
print "This is what you wrote:"
print open (filename).read ()
我运行的时候出现错误,filename似乎是获取的一个临时文件名,而非我保存的文件名,因此不能read,不能显示出内容。

监视一个打印任务

import wmi
c wmi.WMI ()
print_job_watcher c.watch_for (
notification_type"Creation",
wmi_class"Win32_PrintJob",
delay_secs1
)

#
# Or, from 1.0 rc3 onwards
#
# print_job_watcher c.Win32_PrintJob.watch_for (
# notification_type"Creation",
# delay_secs1
# )

while 1:
pj print_job_watcher ()
print "User %s has submitted %d pages to printer %s" % 
(pj.Owner, pj.TotalPages, pj.Name)

重启远程计算机

import wmi
# other_machine "machine name of your choice"
c wmi.WMI (computerother_machine, privileges["RemoteShutdown"])
os c.Win32_OperatingSystem (Primary1)[0]
os.Reboot ()
我想应该有权限才对,反正我是没有关起。另外,名字可以用IP代替吗?

显示IP和MAC

import wmi
c wmi.WMI ()
for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled1):
print interface.Description, interface.MACAddress
for ip_address in interface.IPAddress:
print ip_address
print

自启动程序和位置

import wmi
c wmi.WMI ()
for s in c.Win32_StartupCommand ():
print "[%s] %s <%s>" % (s.Location, s.Caption, s.Command)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值