<?xml version="1.0" encoding="windows-1252"?>
<node id="1018589" title="WMI query with Threads" created="2013-02-13 12:53:11" updated="2013-02-13 12:53:11">
<type id="115">
perlquestion</type>
<author id="1002345">
vamsinm</author>
<data>
<field name="doctext">
&lt;p&gt;Hello PerlMonks, i am new to the perl programming. but managed to get some script put together to get the Time zone information for all the servers using WMI Query. i want to implement threads to make this run faster. i have zero knowledge on threads. but using one example i find i tried to implement wmi query. when i run the script with WMI, i was able to get the list of server names with thread. when i add WMI query, the script is crashing perl with the following messsage. &lt;/P&gt;

&lt;P&gt;
perl TimeZoneCheck.pl
Starting main program

0        server1

1        server2
Host server1 is alive
Host server2 is alive

BPYTSM.JDNET.DEERE.COM   India Standard Time     (GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi         (GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi
Free to wrong pool 49c7480 not 163ea8, &lt;FILE&gt; line 5 during global destruction.

&lt;/P&gt;

&lt;P&gt;
i have active perl version 5.10.0 &lt;/P&gt;
&lt;p&gt;

This is perl, v5.10.0 built for MSWin32-x86-multi-thread
(with 9 registered patches, see perl -V for more detail)

Binary build 1005 [290470] provided by ActiveState http://www.ActiveState.com
Built May 24 2009 12:17:36
&lt;/P&gt;

&lt;P&gt; i also request if you have any suggestions on better error handling for wmi queries&lt;/P&gt;

Following is the script i am using. 

&lt;code&gt;
use warnings;
use threads;
use threads::shared;

use Net::Ping;
use Switch;
use Net::DNS;
use Win32::OLE qw(in with);

use Carp;

use constant wbemFlagReturnImmediately =&gt; 0x10;
use constant wbemFlagForwardOnly =&gt; 0x20;

$Win32::OLE::Warn = 3;

print "Starting main program\n";

open FILE, "c:\\serverping.txt" or die "could not open the file";
my @lines = &lt;FILE&gt;;

$linecount = scalar @lines;

$row = 0;
$line =$lines[$row];
$line =~ s/^\s+//m;
$line =~ s/\s+$//m;

$mainparam = $row.':'.$lines[$row];
$thr0  = threads-&gt;new(\&amp;GetTimeZone, $mainparam);
$row++;

$mainparam = $row.':'.$lines[$row];
$thr1  = threads-&gt;new(\&amp;GetTimeZone, $mainparam);
$row++;

while(1){
	if ($thr0-&gt;is_joinable()) {
  	$thr0-&gt;join;	
  	if ($row&lt;$linecount){
  	  	$mainparam = $row.':'.$lines[$row];
				$thr0  = threads-&gt;new(\&amp;GetTimeZone, $mainparam);
				$row++;
		}
	}
	if ($thr1-&gt;is_joinable()) {
  	$thr1-&gt;join;
  	if ($row&lt;$linecount){
  		$mainparam = $row.':'.$lines[$row];
			$thr1  = threads-&gt;new(\&amp;GetTimeZone, $mainparam);
			$row++;
		}
	}
	$thread_count = threads-&gt;list();		
	#print "$thread_count \n";
	last if ($row==$linecount &amp;&amp; $thread_count == 0);	
}

print "End of main program\n";

close FILE;

 
sub GetTimeZone { 	
 		my $paramsub = shift;   
 		my @subparam = split(/:/, $paramsub); 
 		my $subrow = $subparam[0];
 		my $subhost = $subparam[1];
 		$subhost =~ s/^\s+//m;
		$subhost =~ s/\s+$//m;
 		# Create a new ping object
		$pingit = Net::Ping-&gt;new("icmp");
		my $timeout = 10;
 		
   	print "\n$subrow \t $subhost \n";
   	
    my $user="domain\\username";
    my $pwd="password";
    my $locatorObj =Win32::OLE-&gt;new("WbemScripting.SWbemLocator") or die "ERROR CREATING OBJ";  	
    $locatorObj-&gt;{Security_}-&gt;{impersonationlevel} = 3;
    
    if( $pingit-&gt;ping($subhost, $timeout) )
  	{
      print "Host ".$subhost." is alive\n";
  		eval {  		
  			my $objWMIService = $locatorObj-&gt;ConnectServer($subhost, "root\\cimv2", $user, $pwd);
  		
  	  	if (Win32::OLE-&gt;LastError()) {
        	print "$_\n";
        	Win32::OLE-&gt;LastError(0);  # this clears your error
    		}
    		else
    		{
					$colItems = $objWMIService-&gt;ExecQuery 
    				("Select * from Win32_Timezone","WQL",wbemFlagReturnImmediately | wbemFlagForwardOnly); 
    			if (Win32::GetLastError()) {
        		print "$subhost : $_\n";
        		Win32::OLE-&gt;LastError(0);  # this clears your error
    			}
    			else{
    				foreach my $objItem (in $colItems) 
						{ 
							print "\n$subhost \t $objItem-&gt;{StandardName} \t $objItem-&gt;{Caption} \t $objItem-&gt;{Description} \n";
						} 
					}
	   		}    		   	
	 		};
	 		  if ($@) {
    			print "Warning: Error querying object $subhost: $@\n";
  			}
		}
    else
  	{
      print "Warning: ".$subhost." appears to be down or icmp packets are blocked by their server\n";
	  } 
	  $pingit-&gt;close();
}

&lt;/code&gt;</field>
</data>
</node>
