3

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.

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
Vishal Khemani
  • 171
  • 2
  • 13
  • @Borodin: I think you should not have changed the title. The whole question has become WMIC specific now, which blocks other possible answers to the general question asked by OP. – Chankey Pathak Sep 08 '16 at 12:27
  • @ChankeyPathak: [It has been established](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles) that a question's tags are implicit in the subject line, so for a different reason I agree with you. But there are some strange things in the available tags, and a subject like "How to reverse sort a log file by date" shouldn't be replaced with "Processing data" with a tag list of `sorting`, `logging`, and `reverse`. The original question had both WMI and WMIC in the tag list, and I think it's fair to assume that a WMI solution was expected. – Borodin Sep 08 '16 at 17:47

2 Answers2

2

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",
  },
]
Borodin
  • 126,100
  • 9
  • 70
  • 144
1

You can do it through WMIC:

Windows Management Instrumentation Command-line (WMIC) uses the power of Windows Management Instrumentation (WMI) to enable systems management from the command line.

To interact with WMIC using Perl you can consider using Net::WMIClient.

In short you just have to call WMIC (either directly or through related module).

You are looking for wmic product command which will provide full information about all installed software.

Without module:

my @all_installed_products_info = `wmic product`;
print "@all_installed_products_info";

Using Net::WMIClient:

use Net::WMIClient qw(wmiclient);
my $options = {Username => 'u1', Password => 'pass', Host => '...', Timeout => '...'};
($status, $output) = wmiclient($options, "wmic product");

Also see:


Edit: Quoting README to answer Borodin's comment.

DEPENDENCIES

This module requires these other modules and libraries:

lib_async_wmilib.so from the wmi package, along with the configured source code at build. The current source is available at http://www.edcint.co.nz/checkwmiplus/wmi-1.3.14.tar.gz.

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • Please would you take a look at this? Have you been able to install `Net::WMIClient`? If so then where did you find the object library file `libasync_wmi_lib.so.0` that doesn't appear to be in the documented archive? Or if not then why did you post untested code? – Borodin Sep 08 '16 at 17:50
  • It was mentioned in README. Regarding the untested code: Yes I did not test the code, as you can see my code snippet is not even complete. I just provided an alternate to OP. – Chankey Pathak Sep 09 '16 at 05:12