How to Create a Computer Inventory Script in Python for Windows

How to Create a Computer Inventory Script in Python for Windows

Posted on July 23, 2018 0 Comments

Basic details of your computer like operating system and version, memory, network adapter and hard drive are never stored in one central location. You always have to go clicking around for it. But if you've got Python installed, we can build a script that collects all of that information and returns all of the details right at the console.

In this article, I'd like to use Python on a Windows system to gather a few items.

  • Operating system name and version
  • Total storage
  • Free storage space
  • Total RAM
  • NIC IP address

To do this, I've installed Python 3.6.4 on a Windows 10 computer. We won't be doing anything fancy with Python so you might not need this particular version.

Unlike Linux and MacOS where we are forced to use different modules and methods to get various attributes, for Windows, we can simply stick to WMI (Windows Management Instrumentation). Luckily, we already have a Python WMI module available called wmi. To use the wmi module, we need both itself and a dependency module called pywin32. We can download them both with pip.

 
C:\> pip install wmi
Collecting wmi
  Downloading WMI-1.4.9.zip
Installing collected packages: wmi
  Running setup.py install for wmi ... done
Successfully installed wmi-1.4.9

C:\> pip install pywin32
Collecting pywin32
  Downloading pywin32-223-cp36-cp36m-win_amd64.whl (9.0MB)
    100% |████████████████████████████████| 9.0MB 117kB/s
Installing collected packages: pywin32
Successfully installed pywin32-223

Once we have the modules installed, open up the python interpreter and import the wmi module via import wmi.

Once the wmi module is imported, we can then begin gathering the various attributes of our computer inventory script. We first need to establish a connection to the WMI respository.

>>> c = wmi.WMI ()

Next, we simply need to provide the appropriate WMI class name and attributes we need to gather up. First, let's grab the operating system and version. To do this, I can use the Win32_OperatingSystem WMI class.

>>> c.Win32_OperatingSystem()[0].Caption
'Microsoft Windows 10 Pro'

Next, I'll grab total storage. WMI stores hard drive information in a WMI class called Win32_LogicalDisk. For this example, I'm just looking for total disk space for the C: drive. By referencing the Win32_LogicalDisk class, I can see that we're already looking at the C:.

>>> c.Win32_LogicalDisk()[0]
<_wmi_object: b'\\\\MACWINVM\\root\\cimv2:Win32_LogicalDisk.DeviceID="C:"'>

Because we're already looking at the C: drive, I can reference the Size and FreeSpace properties to find that information I'm looking for.

>>> c.Win32_LogicalDisk()[0].Freespace
'865746944'

>>> c.Win32_LogicalDisk()[0].Size
'63898120192'

Next up is total RAM. To find information about RAM, we can use the Win32_ComputerSystem class. This class provides a single attribute we can use to easily gather this information.

>>> c.Win32_ComputerSystem()[0].TotalPhysicalMemory
'4294479872'

Finally, we need to collect the NIC's (Network Interface Controller) IPv4 address. The IPv4 address is located on an instance of Win32_NetworkAdapterConfiguration. However, this isn't as easy as referencing an individual attribute as we've done previously. Since a machine can potentially have lots of NICs installed, we need to narrow it down. One way to do that is by filtering on the IPEnabled attribute. When this attribute is True, we can be sure that an active IP address is tied to that instance.

>>> wql = "SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'"
>>> c.query(wql)[0].IPAddress
('192.168.86.80',)

You can see that this returns the IP address but it's not a simple string like we need. It's a tuple. We can consolidate that data type to convert it into a simple string by using a string's join() method.

>>> ''.join(c.query(wql)[0].IPAddress)
'192.168.86.80'

We've now got the code built to pull each of the individual components. Finally, we need to pull all of this information together into a single Python script that will return eveything at once.

#!/usr/bin/env python

import wmi

c = wmi.WMI ()

print('OS is: {0}'.format(c.Win32_OperatingSystem()[0].Caption))
print('Disk freespace {0} - total {1}'.format(c.Win32_LogicalDisk()[0].Freespace,c.Win32_LogicalDisk()[0].Size))
print('Total Memory: {0}'.format(c.Win32_ComputerSystem()[0].TotalPhysicalMemory))

wql = "SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'"
print('Local IP address: {0}'.format(''.join(c.query(wql)[0].IPAddress)))

Here's the output we finally come up with.

C:\>python c:\ff.py
OS is: Microsoft Windows 10 Pro
Disk freespace 1095118848 - total 63898120192
Total Memory: 4294479872
Local IP address: 192.168.86.80

This small report can be improved quite a bit but now that you have a foundation built, you now have the ability to manipulate the output or add other attributes that may be necessary for your particular scenario!

Adam Bertram

Adam Bertram is a 25+ year IT veteran and an experienced online business professional. He’s a successful blogger, consultant, 6x Microsoft MVP, trainer, published author and freelance writer for dozens of publications. For how-to tech tutorials, catch up with Adam at adamtheautomator.com, connect on LinkedIn or follow him on X at @adbertram.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation