python利用 WMI 库操作windows
python利用 WMI 库操作windows
转载自 http://python.jobbole.com/86349/
最近在网上搜索Python和WMI相关资料时,发现大部分文章都千篇一律,并且基本上只说了很基础的使用,并未深入说明如何使用WMI。本文打算更进一步,让我们使用Python玩转WMI。
1 什么是WMI
具体请看微软官网对WMI的介绍。这里简单说明下,WMI的全称是Windows Management Instrumentation,即Windows管理规范。它是Windows操作系统上管理数据和操作的基础设施。我们可以使用WMI脚本或者应用自动化管理任务等。
从Using WMI可以知道WMI支持如下语言:
Application language | Topic |
---|---|
Scripts written in Microsoft ActiveX script hosting, including Visual Basic Scripting Edition (VBScript) and Perl | Scripting API for WMI.
Start with Creating a WMI Script. For script code examples, see WMI Tasks for Scripts and Applications and the TechNet ScriptCenterScript Repository. |
Windows PowerShell | Getting Started with Windows PowerShell
WMI PowerShell Cmdlets, such as Get-WmiObject. |
Visual Basic applications | Scripting API for WMI. |
Active Server Pages | Scripting API for WMI.
Start with Creating Active Server Pages for WMI. |
C++ applications | COM API for WMI.
Start with Creating a WMI Application Using C++ and WMI C++ Application Examples (contains examples). |
.NET Framework applications written in C#, Visual Basic .NET, or J# | Classes in the Microsoft.Management.Infrastructure namespace. (The System.Management namespace is no longer supported). For more information, see WMI .NET Overview. |
很遗憾,WMI并不原生支持Python。不过没有关系,它支持VB,而Python中的两个第三方库wmi和win32com,均能以类似VB的用法来使用。那么接下来,我们来讲讲如何使用。
2 使用WMI
2.1 使用wmi库操作WMI
以下是一个遍历所有进程,所有服务的示例:
时来天地皆同力,运去英雄不自由