sub new
{
my $class = shift;
my $args = shift;
my $self = {};
$self->{online} = (defined($args->{online}) ? ($args->{online} ? 1 : 0) : 1); # online by default
$self->{updateSession} = Win32::OLE->new('Microsoft.Update.Session') or die "ERROR creating Microsoft.Update.Session\n";
$self->{updateSearcher} = $self->{updateSession}->CreateUpdateSearcher or die "ERROR creating CreateUpdateSearcher\n";
$self->{updateSearcher}->{Online} = $self->{online};
return bless($self, $class);
}
# .. cut unrelated stuff ..
sub install
{
my $self = shift;
my @updates = @_;
my $updatecoll = Win32::OLE->new('Microsoft.Update.UpdateColl') or die "ERROR creating Microsoft.Update.UpdateColl\n";
$updatecoll->Clear;
my $queryResult = $self->{updateSearcher}->Search("IsInstalled = 0 AND IsHidden = 0") or die "ERROR in query\n";
my $updates = $queryResult->Updates;
foreach my $update (in $updates)
{
my $updateID = $update->Identity->UpdateID;
next unless grep(m/^$updateID$/i, @updates);
$update->AcceptEula;
$updatecoll->Add($update);
printf("Adding %s %s to collection\n", $updateID, $update->Title);
}
printf("Updates to install: %d\n", $updatecoll->Count);
my $downloader = $self->{updateSession}->CreateUpdateDownloader;
$downloader->{Updates} = $updatecoll;
my $downloadResult = $downloader->Download;
my $installer = $self->{updateSession}->CreateUpdateInstaller;
$installer->{Updates} = $updatecoll;
$installer->{AllowSourcePrompts} = 0;
$installer->{ForceQuiet} = 1;
my $installResult = $installer->Install;
use Data::Dumper;
print Dumper($updatecoll, $downloader, $downloadResult, $installer, $installResult);
}
####
use Win32::WindowsUpdate;
my $wu = Win32::WindowsUpdate->new;
$wu->install(
'09569688-db5d-422b-965d-aac88486fae4',
'60b990a0-6efa-47be-8f5a-7df2c402583e',
);
####
H:\Projects\Win32-WindowsUpdate\lib>perl test.pl
Adding 09569688-db5d-422b-965d-aac88486fae4 February 2007 CardSpace Update for Windows XP (KB925720) to collection
Adding 60b990a0-6efa-47be-8f5a-7df2c402583e Windows XP Service Pack 3 (KB936929) to collection
Updates to install: 2
$VAR1 = bless( {
'Item' => undef,
'_NewEnum' => undef,
'Count' => 2,
'ReadOnly' => 0
}, 'Win32::OLE' );
$VAR2 = bless( {
'ClientApplicationID' => '',
'IsForced' => 0,
'Priority' => 3,
'Updates' => undef
}, 'Win32::OLE' );
$VAR3 = undef;
$VAR4 = bless( {
'ClientApplicationID' => '',
'IsForced' => 0,
'parentWindow' => undef,
'Updates' => undef, ## this is the trouble spot
'IsBusy' => 0,
'AllowSourcePrompts' => 0,
'RebootRequiredBeforeInstallation' => 0,
'ForceQuiet' => 1
}, 'Win32::OLE' );
$VAR5 = undef;