监视新的打印任务- import wmi
- c = wmi.WMI ()
-
- print_job_watcher = c.Win32_PrintJob.watch_for (
- notification_type="Creation",
- delay_secs=1
- )
-
- while 1:
- pj = print_job_watcher ()
- print "User %s has submitted %d pages to printer %s" % \
- (pj.Owner, pj.TotalPages, pj.Name)
复制代码 重启远程机器
注:要对远程系统进行这样的操作,WMI脚本必须具有远程关机(RemoteShutdown)的权限,也就是说你必须在连接别名中进行指定。WMI构造器允许你传入一个完整的别名,或者是指定你需要的那一部分。使用wmi.WMI.__init__的帮助文档可以找到更多相关内容。- import wmi
- # other_machine = "machine name of your choice"
- c = wmi.WMI (computer=other_machine, privileges=["RemoteShutdown"])
-
- os = c.Win32_OperatingSystem (Primary=1)[0]
- os.Reboot ()
复制代码 对于启用IP的网卡显示其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
- print
复制代码 查看自启动项- import wmi
- c = wmi.WMI ()
-
- for s in c.Win32_StartupCommand ():
- print "[%s] %s <%s>" % (s.Location, s.Caption, s.Command)
复制代码 监视事件日志中的错误信息- import wmi
- c = wmi.WMI (privileges=["Security"])
-
- watcher = c.watch_for (
- notification_type="Creation",
- wmi_class="Win32_NTLogEvent",
- Type="error"
- )
- while 1:
- error = watcher ()
- print "Error in %s log: %s" % (error.Logfile, error.Message)
- # send mail to sysadmin etc.
复制代码 列出注册表子键
注:本例及以下几例使用了Registry()这个方便的函数,此函数是早期加入到wmi包的,它等效于:
import wmi
r = wmi.WMI (namespace="DEFAULT").StdRegProv- import _winreg
- import wmi
-
- r = wmi.Registry ()
- result, names = r.EnumKey (
- hDefKey=_winreg.HKEY_LOCAL_MACHINE,
- sSubKeyName="Software"
- )
- for key in names:
- print key
复制代码 增加一个新的注册表子键- import _winreg
- import wmi
-
- r = wmi.Registry ()
- result, = r.CreateKey (
- hDefKey=_winreg.HKEY_LOCAL_MACHINE,
- sSubKeyName=r"Software\TJG"
- )
复制代码 增加一个新的注册表键值- import _winreg
- import wmi
-
- r = wmi.Registry ()
- result, = r.SetStringValue (
- hDefKey=_winreg.HKEY_LOCAL_MACHINE,
- sSubKeyName=r"Software\TJG",
- sValueName="ApplicationName",
- sValue="TJG App"
- )
复制代码
|