Loading/Unloading Modules - Specifically Win32::OLE

Hi Folks,

New to Perl. I am using the following code snippet to retrieve the memory statistics from a remote server. I am also using similar code to retrieve CPU stats, just using a different query and calculation logic.

The issue that I’m finding is from the behavior I am seeing on the REMOTE server and NOT in the code. It seems that by running a perl script that includes these calls, the performance monitoring dlls on the REMOTE server are loaded and then being being held OPEN, even after my scripts complete execution and have exited. It’s like the connection to the remote OLE process is not completely torn down after execution completes, thus allowing the remote side to maintain a reference count to that dll, causing it to remain loaded. This is preventing me from performing maintenance on the remote server, including deploying new code, since I can’t copy over a file (our custom perfmon.dll) that remains open in memory.

Once I complete the calls shown below, is there some cleanup call that I should be making to close the OLE connection, terminate the GetObject(), or otherwise terminate the connection?

Any other advise is welcome!

Thanks in advance,
-Rob

my $objWMIService = Win32::OLE->GetObject(“winmgmts://$ServerToCheck_UC/root/cimv2”) or die “WMI connection failed.\n”;

my $MemQuery = “select * from Win32_OperatingSystem”;

For memory consumption, a snapshot in time can be retrieved

my $MemCols=$objWMIService->ExecQuery($MemQuery);

foreach my $Data( in $MemCols){ # This for loop should only execute once, since there is only one element in the returned object

  my $totalMemory = $Data->{TotalVisibleMemorySize};
  my $freeMemory = $Data->{FreePhysicalMemory};

  $MemStat = (($totalMemory-$freeMemory)/$totalMemory)*100;

}