Windows Updates in Perl

By Dusty on February 17, 2009 4:36 PM · No Comments

Need to get Windows Updates via Perl?  Here's the way I did it:

use Win32::OLE qw(in);
use Data::Dumper;

my $updateOLE = Win32::OLE->new('Microsoft.Update.Session') or die "ERROR creating Microsoft.Update.Session\n";
my $updateSearcher = $updateOLE->CreateUpdateSearcher or die "ERROR creating CreateUpdateSearcher\n";
$updateSearcher->SetProperty('Online', 1); # set to 1 to check online for updates, 0 to not check online. seems to default to online.
my $queryResult = $updateSearcher->Search("IsInstalled = 0 and IsHidden = 0");
my $updates = $queryResult->Updates;

my @updates;
foreach my $update (in $updates)
{
  my $info = {};

  $info->{UpdateId} = $update->Identity->UpdateID;
  $info->{Title} = $update->Title;
  $info->{Description} = $update->Description;
  $info->{RebootRequired} = $update->RebootRequired;
  $info->{ReleaseNotes} = $update->ReleaseNotes;
  $info->{EulaText} = $update->EulaText;
  $info->{EulaAccepted} = $update->EulaAccepted;

  push(@{$info->{Category}}, {Name => $_->Name, CategoryID=> $_->CategoryID}) foreach (in $update->Categories);
  push(@{$info->{MoreInfoUrl}}, $_) foreach (in $update->MoreInfoUrls);
  push(@{$info->{KBArticleID}}, $_) foreach (in $update->KBArticleIDs);
  push(@{$info->{SecurityBulletinID}}, $_) foreach (in $update->SecurityBulletinIDs);
  push(@{$info->{SupersededUpdateID}}, $_) foreach (in $update->SupersededUpdateIDs);

  push(@updates, $info);
}

print Dumper(\@updates)

No TrackBacks

TrackBack URL: http://www.dustywilson.com/mt-tb.cgi/11

Leave a comment