Vishal Khemani Vishal Khemani - 3 months ago 8
Perl Question

Get the installed date of a particular application on Windows

I have installed an application on my Windows system. I have a Perl script which will re-install the same software every day.

I would like to know if the software is really getting installed or not.

One way I thought of doing that is by checking for the installed date of the application.

Answer

I have never successfully installed the Net::WMIClient module: it requires an elusive external library that isn't in the package that the module's document links to

It is fairly straightforward to use backticks within Perl to use the wmic command directly. You need to bear these things in mind

  • Each line returned by the utility will end with a CR LF pair. I have removed these in the code below with s/\R\z// which uses the \R pattern to match any line terminator sequence

  • The output from the utility has a blank line at the start. I have removed elements from the beginning of the array @data until the first element contains a non-space character

  • The first non-blank line is a sequence of column names

  • Unless you specify otherwise, the output from the utility consists of fixed-width columns of data. It is much easier to process if you request CSV format, with the option /format:csv

  • The wmic utility isn't fast: the command takes about fifteen seconds to execute on my system

This program reads the name, version, and install date of all products matching the given pattern. I have used %perl%, but you will want to use something that will select the product you are interested in

The lines of CSV data in array @products are converted to hashes, keyed by the values in the header line. Note that, unlike with SQL, the values won't be displayed in the order they are requested in the command line

Note that the InstallDate field is only a date, so if the product is updated twice in the same day then the value won't change. That is why I have also included the version field, which will distinguish updates properly if your software keeps a proper version system

There is a full list of the properties and methods (we are interested in only the properties here) of the Win32_Product class on the Microsoft Developer Network here. It is possible that you may find other fields useful

I have used Data::Dump to display the final contents of array @products for demonstration purposes only. I trust you are able to work from there to perform the checks that you require

use strict;
use warnings 'all';

my @products = `wmic product where "name like '%perl%'" get name, version, installdate /format:csv `;

s/\R\z// for @products;
shift @products until $products[0] =~ /\S/;

my @keys = split /,/, shift @products;

for ( @products ) {
    my %item;
    @item{@keys} = split /,/;
    $_ = \%item;
}

use Data::Dump;
dd \@products;

output

[
  {
    InstallDate => 20160517,
    Name => "Strawberry Perl (64-bit)",
    Node => "CALAMUS",
    Version => "5.24.1",
  },
]