Powershell - Get Inventory Script
In my "other life" I run an IT team, which among other things supports some 300 or so workstations.
One of the most boring tasks for IT staff is to record accurately the inventory details of all of the
workstations so that we know what is in use... (Accountants especially love this stuff but support
staff hate it!) As I am always keen for the equipment to do the work, here is how I assist my
staff:
Background
Before Powershell was released, I solved this problem using the SysInternals Tools of BGInfo and PSInfo
together with a command script I wrote and filtered the output using a Perl script. The only
items that these tools could not get at were the serial numbers of the periphials. I solved this by inserting the
serial numbers of the periphials into a text file on the C:\ drive. Typically there would be a
"monitor.txt" file together with a "printer.txt" and "scanner.txt" file each containing the
relevant serial number or the string "NULL" BGInfo could get at this data also... This data insertion
was the only manual part of the inventory data collection process!
Development
With the introduction of the Microsoft Powershell scripting language I investigated if I
might solve the same problem without the need for calling applications like BGInfo or PSInfo and
parsing their output. The GetInventory.ps1 script is the result
of my efforts. Much of the original "active" portions of the script where adapted from the very
comprehensive scripts written by the Microsoft Powershell team and available on Technet at
The Script Center Script Repository: Sample Windows PowerShell Scripts.
As with all scripting projects you develop better techniques as you do the work. So what I have written
would likely be done differently if I did it again, nevertheless what I have created works!
In a further development to this script I have created an alternate
script that writes many of these fields directly to a MySQL database.
Output
The script writes output to a HTML file located on the C:\ drive of the workstation that it is run on.
The HTML as it is currently formated in the script, only writes the "body" of the HTML code but this
is enough to display correctly in most browsers.
We leave a copy of the output file on the C:\ drive so we can open it if we navigate to the C$ share
over the network. We also copy the output file onto a server which allows us to browse and/or search
the files using another script. Typically the output will look like
the output from my home computer. I really like the way this script gets details like
the processor ID number, the MAC address and the serial number off the BIOS. It makes the output file almost like a
"finger print" of the machine. (Which is great news if a laptop goes missing and the police need the details
AND you have made a copy of the file on a server!)
Script
Get the GetInventory.ps1 Powershell script.
Prerequisites
You will need to have loaded Microsoft
Windows Powershell... which was released late 2006, on any workstation on which you want to run this script.
You will also need to set the Powershell script execution policy to: "set-executionpolicy remotesigned". The
prerequisites for Powershell on XP are Service Pack 2 and the Microsoft .NET Framework version 2.0
Issues (Yes I have found a couple!)
This script extensively calls on the WMI Win32_Product which is designed to retrieve information about all the
software on a computer that was installed using the Windows Installer (e.g., a .MSI file). The Win32_Product is not
foolproof - it can miss applications that really were installed using the Windows Installer - but it seems
to have no problem identifying most major software programs.
When running this script on a Windows 2003 box an "unknown class" error can occur, because by default the Win32_Product
class of the Windows Installer WMI provider does not exist on Windows 2003. It has to be installed! To do this
Open Add or Remove Programs and get into the Add/Remove Windows Components wizard. Under Management and Monitoring Tools,
you will find the WMI Windows Installer Provider. Once installed, you have no further problem!
I have retained the process of collecting the periphials serial numbers described in the "Background" paragraph as these
details are not available to the WMI calls on the workstation. At least if I have the serial number of these items
I can match that to the purchase data we retain on the equipments arrival...
Operationally
We schedule the script to run from the AT command once a day on the local machines when the workstations are not
in use. This means that the data is up to date with any changes made in the previous 24 hours. I have another
script that is run from a server which copies the output HTML file from each machine to a directory on a web server.
We can then browse these files if we need data on the workstations and I have a
search "engine" written in Perl, that allows us
to find discrete information across all the collected files. Meaning if someone (usually an auditor) asks me how
many copies of say EndNote we have, I can find out real quick!
Also see a couple of more recently developed alternate scripts:
One that writes many of these fields directly to a MySQL database
and uses a PHP script to search as an alternative; And a second that performs a few
of the WMI queries on "remote" workstations to determine how "stressed" the memory usage is (emailing reports back
to the consultants...)
Improvements
03/12/2006 Inserted alternate values for memory size and computer speed converting base values to GB and GHz.
06/12/2006 Included a new section on hard drive data which returns the size of the drive(s)
and the free space on the drive(s).
07/12/2006 Adjust alternate display for memory size to two decimal places.
07/12/2006 Improved the hard drive data to show Physical Drive and Logical Drive data.
13/12/2006 Corrected the alternate display for the memory size in the Operating System Properties section.
15/12/2006 Remove any legacy return characters from the Hotfix description data.
16/12/2006 Improve HTML output file syntax. (It is still not "valid" but it is more consistant!)
25/02/2007 Added a section to list the Printer Drivers that are loaded.
25/02/2007 Swapped the order of display for the Applications and Hotfixes.
08/06/2007 Inserted new section on username history based on the user profile directories.
11/06/2007 Improve date display on username history and moved it to the last position in the script.
14/07/2007 Inserted new section to display Norton/Symantec Antivirus Status if it is present.
10/06/2009 Improved the date displays enforcing a NZ standard (Powershell tends to default to American...)
14/09/2009 Created an alternate script to load this data to a MySQL database.
04/09/2010 Created a new really useful script to Monitor Remote Workstation Memory Usage,
by repurposing a small portion of this script and performing the WMI queries on "external" or "remote" machines.
|