wmi模块/ wmi Module
WMI (Windows Management Instrumentation) 模块可用于获取 Windows 内部信息。该模块需要 win32com 的支持,环境安装如下,
pip install wmi
pip install pypiwin32
模块信息
WMI()类
类实例化:w = wmi.WMI()
类的功能:用于生成 WMI 的实例
传入参数:无
返回参数: w
Win32_Processor()方法
函数调用: processorList = w.Win32_Processor()
函数功能:用于获取处理器信息对象,并存以列表形式
传入参数:无
返回参数: processorList
processorList: list类型,list中每个元素均为一个含cpu信息的object
cpu object通过object.name调用,所含信息包括:
instance of Win32_Processor
{
AddressWidth= 64;
Architecture= 9;
Availability= 3;
Caption= "Intel64 Family 6 Model 78 Stepping 3";
CpuStatus= 1;
CreationClassName= "Win32_Processor";
CurrentClockSpeed= 2376;
CurrentVoltage= 9;
DataWidth= 64;
Description= "Intel64 Family 6 Model 78 Stepping 3";
DeviceID= "CPU0";
ExtClock= 100;
Family= 205;
L2CacheSize= 512;
L3CacheSize= 3072;
L3CacheSpeed=0;
Level= 6;
LoadPercentage= 29;
Manufacturer= "GenuineIntel";
MaxClockSpeed= 2401;
Name= "Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz";
NumberOfCores= 2;
NumberOfLogicalProcessors= 4;
PowerManagementSupported=FALSE;
ProcessorId= "BFEBFBFF000406E3";
ProcessorType= 3;
Revision= 19971;
Role= "CPU";
SocketDesignation= "U3E1";
Status= "OK";
StatusInfo= 3;
SystemCreationClassName= "Win32_ComputerSystem";
SystemName= "CN00208511";
UpgradeMethod= 1;
Version= "";
};
View Code
Win32_PhysicalMemory()方法
函数调用: memModuleList = w.Win32_PhysicalMemory()
函数功能:用于获取物理内存信息对象,并存以列表形式
传入参数:无
返回参数: memModuleList
memModuleList: list 类型,list 中每个元素均为一个含物理内存信息的 object
物理内存 object 通过 object.name 调用,所含信息包括:
instance of Win32_PhysicalMemory
{
BankLabel= "ChannelA";
Capacity= "8589934592";
Caption= "Physical Memory";
CreationClassName= "Win32_PhysicalMemory";
DataWidth= 64;
Description= "Physical Memory";
DeviceLocator= "Bottom-Slot 1(left)";
FormFactor= 12;
InterleaveDataDepth=0;
InterleavePosition=0;
Manufacturer= "Hynix/Hyundai";
MemoryType=0;
Name= "Physical Memory";
PartNumber= "HMA81GS6AFR8N-UH";
PositionInRow= 1;
SerialNumber= "28ECE200";
Speed= 2133;
Tag= "Physical Memory 0";
TotalWidth= 64;
TypeDetail= 16512;
};
View Code
Win32_LogicalDisk ()方法
函数调用: diskList = w.Win32_LogicalDisk(DriverType=3)
函数功能:用于获取磁盘信息对象,并存以列表形式
传入参数: *argv
DriverType: int 类型,筛选驱动类型
返回参数: diskList
diskList: list类型,list 中每个元素均为一个含磁盘信息的object
磁盘信息 object 通过object.name调用,所含信息包括:
instance of Win32_LogicalDisk
{
Access=0;
Caption= "C:";
Compressed=FALSE;
CreationClassName= "Win32_LogicalDisk";
Description= "Local Fixed Disk";
DeviceID= "C:";
DriveType= 3;
FileSystem= "NTFS";
FreeSpace= "142053064704";
MaximumComponentLength= 255;
MediaType= 12;
Name= "C:";
Size= "228134481920";
SupportsDiskQuotas=FALSE;
SupportsFileBasedCompression=TRUE;
SystemCreationClassName= "Win32_ComputerSystem";
SystemName= "CN00208511";
VolumeName= "OSDisk";
VolumeSerialNumber= "3AFBC150";
};
instance of Win32_LogicalDisk
{
Access=0;
Caption= "D:";
Compressed=FALSE;
CreationClassName= "Win32_LogicalDisk";
Description= "Local Fixed Disk";
DeviceID= "D:";
DriveType= 3;
FileSystem= "FAT32";
FreeSpace= "1067384832";
MaximumComponentLength= 255;
MediaType= 12;
Name= "D:";
Size= "1069547520";
SupportsDiskQuotas=FALSE;
SupportsFileBasedCompression=FALSE;
SystemCreationClassName= "Win32_ComputerSystem";
SystemName= "CN00208511";
VolumeName= "HP_TOOLS";
VolumeSerialNumber= "B2FBEEA0";
};
instance of Win32_LogicalDisk
{
Access=0;
Caption= "E:";
Compressed=FALSE;
CreationClassName= "Win32_LogicalDisk";
Description= "Local Fixed Disk";
DeviceID= "E:";
DriveType= 3;
FileSystem= "NTFS";
FreeSpace= "21400256512";
MaximumComponentLength= 255;
MediaType= 12;
Name= "E:";
Size= "26843541504";
SupportsDiskQuotas=FALSE;
SupportsFileBasedCompression=TRUE;
SystemCreationClassName= "Win32_ComputerSystem";
SystemName= "CN00208511";
VolumeName= "HP_RECOVERY";
VolumeSerialNumber= "BCFC555B";
};
View Code
Win32_ NetworkAdapterConfiguration()方法
函数调用: interfaceList = w.Win32_NetworkAdapterConfiguration(IPEnabled=1)
函数功能:用于网络接口信息对象,并存以列表形式
传入参数: *argv
IPEnabled: bool 类型,可传入1,默认为False,为 True 将显示 IP 信息
返回参数: interfaceList
interfaceList: list 类型,list 中每个元素均为一个网络接口信息的 object
网络接口信息 object 通过 object.name 调用,所含信息包括(部分具体信息已删除):
instance of Win32_NetworkAdapterConfiguration
{
Caption= "[00000011] Intel(R) Dual Band Wireless-AC 8260";
DatabasePath= "%SystemRoot%\\System32\\drivers\\etc";
DefaultIPGateway= {"192.168.1.1"};
Description= "Intel(R) Dual Band Wireless-AC 8260";
DHCPEnabled=TRUE;
DHCPLeaseExpires= "20170531002222.000000+480";
DHCPLeaseObtained= "20170530222222.000000+480";
DHCPServer= "192.168.1.1";
DNSDomain= "DHCP HOST";
DNSDomainSuffixSearchOrder= {"ericsson.se", "DHCP HOST"};
DNSEnabledForWINSResolution=FALSE;
DNSHostName= "";
DNSServerSearchOrder= {"", ""};
DomainDNSRegistrationEnabled=FALSE;
FullDNSRegistrationEnabled=TRUE;
GatewayCostMetric={0};
Index= 11;
InterfaceIndex= 12;
IPAddress= {"", ""};
IPConnectionMetric= 20;
IPEnabled=TRUE;
IPFilterSecurityEnabled=FALSE;
IPSecPermitIPProtocols={};
IPSecPermitTCPPorts={};
IPSecPermitUDPPorts={};
IPSubnet= {"", ""};
MACAddress= "";
ServiceName= "NETwNs64";
SettingID= "{}";
TcpipNetbiosOptions=0;
WINSEnableLMHostsLookup=TRUE;
WINSScopeID= "";
};
View Code
利用wmi模块获取PC信息
获取PC信息的步骤主要有:
(1)打开存储信息的文件;
(2)实例化WMI类,利用各函数获取硬件信息类的列表;
(3)遍历硬件信息类列表,获取硬件信息并写入存储文件。
1 importwmi2 hardware=open('Hardware.txt', 'w')3
4 w=wmi.WMI()5
6 hardware.write("cpu type,main frequency:\n")7 for processor inw.Win32_Processor():8 hardware.write("Processor ID: %s" %processor.DeviceID)9 hardware.write("\nProcess Name: %s" % processor.Name.strip()+'\n\n')10 hardware.write('Memory size:')11 totalMemSize=012 for memModule inw.Win32_PhysicalMemory():13 totalMemSize+=int(memModule.Capacity)14 hardware.write("\nMemory Capacity: %.2fMB" %((totalMemSize+1048575)/1048576)+'\n\n')15 hardware.write('Hard disk usage:')16 for disk in w.Win32_LogicalDisk (DriveType=3):17 temp=disk.Caption+"%0.2f%% free" %(100.0 * int(disk.FreeSpace) /int(disk.Size))18 hardware.write('\n'+temp)19 hardware.write('\n')20 hardware.write('\nIP and MAC:\n')21 for interface in w.Win32_NetworkAdapterConfiguration(IPEnabled=1):22 hardware.write('Network card driver information:')23 hardware.write(interface.Description+'\n')24 hardware.write('Network card MAC address:')25 hardware.write(interface.MACAddress+'\n')26 hardware.write('IP address:')27 hardware.write(interface.IPAddress[0]+'\n')28 hardware.write('Network IP interface')29 hardware.write(interface.IPAddress[1]+'\n')30 hardware.close()
最终可以在当前目录下得到一个包含硬件信息的文本文件。