use Win32::OLE qw(EVENTS); use Win32::Console; my $console = Win32::Console->new(STD_INPUT_HANDLE); my $refWMI = Win32::OLE->GetObject('winMgmts:'); my $refSink = Win32::OLE->new ('WBemScripting.SWbemSink'); Win32::OLE->WithEvents($refSink,\&eventCallback); my $strQuery = "SELECT * FROM __SessionEndingEventHandler " . "WITHIN 10 WHERE TargetInstance ISA '\Win32_Service\'"; $refWMI->ExecNotificationQueryAsync($refSink, $StrQuery); print "Waiting for Events ...\n"; my $continueLooping = 1; while ($continueLooping) { if ($console->GetEvents()!= 0){ my @cons = $console->Input(); if ($cons[1] != 0){ undef $countingLooping; } } Win32::OLE->SpinMessageLoop(); Win32::Sleep(500); } $refSink->Cancel(); Win32::OLE->WithEvents($refSink); undef $refSink; undef $refWMI; undef $console; print "Finished.\n"; sub eventCallback() { my ($refSource,$refEventName,$refEvent,$refEvent,$refContext) = @_; if ($refEventName eq "onObjectReady") { print $refEvent->TargetInstance->{Name} . " has " . ($refEvent->TargetInstance->{Started} ? "started" : "stopped") . "\n"; } }