OK, what I am trying to do is check for a registry value on a remote machine via WMI.

The problem I am having is that if the value is not present my script will just exit, with no error (or I am just not catching it).

For example, I am looking in HKLM\\CurrentControlSet\\Services\\SNMP\\Parameters\\TrapConfiguration\\public for the key "1" and then checking what the value of the key is to make sure SNMP is setup and working correctly. Now if the path I mentioned above exists, the script is happy and continues doing its thing but if the above path does not exist,things don't work out so well... And by exist I mean there is no folder called "public" under "TrapConfiguration" in the current server registry

Here is my code

use strict; use Win32::OLE; use Win32::OLE::Variant; use Net::Ping; use Date::EzDate; use Spreadsheet::WriteExcel; use warnings; use Win32::OLE qw(in with); sub regCheck { my $server = $_[0]; my $exists = 0; my $refRegistry = Win32::OLE->GetObject("winMgmts://$server/root/d +efault:StdRegProv" ) or die "cannot get object\n"; my @search = ('\\xxxxx','\\public'); my $HKEY_LOCAL_MACHINE = 0x80000002; my $strSKPath="SYSTEM\\CurrentControlSet\\Services\\SNMP\\Paramete +rs\\TrapConfiguration"; my $strValueName="1"; my $count = 1; foreach(@search) { my $value = Variant(VT_BSTR | VT_BYREF, ""); my $newPath = $strSKPath . $_; $refRegistry->GetStringValue($HKEY_LOCAL_MACHINE, $newPath,$strValueNa +me, $value); #Ignore this part #if($value != 0) #{ # print OUTFILE "Regvalue is null\n"; #} #else #{ #if($value =~ m/^*xxxxxx.com*+/) #{ # $exists = 1; #} #} $count++; } return $exists; }

Also to be clear, I am searching two places in the registry for the value I want. That is the reason for the foreach in the middle of the sub. In addition, the part commented out I have not worked on because I was trying to figure out why my script was failing at the specific line mentioned below.

What I have discovered is my script is stopping at this line if the path to the registry does not exist

$refRegistry->GetStringValue($HKEY_LOCAL_MACHINE, $newPath,$strValueName, $value);

I have learned that this line will return 0 if it is successful. And according to Microsoft this line will return either a null value or a value other than 0 if it fails. See here->http://msdn.microsoft.com/en-us/library/aa390788%28v=vs.85%29.aspx

What happens is it will return 0 if the path to search in the registry exists, which is what its suppose to do. But, if it cant find the path in the registry, it will just fail without seeming to return any error,or *anything* for that matter, and my script will terminate.

I have tried all different things to try and retrieve what this problem line is returning but I have been unsuccessful.

Does anyone have any idea whats happening and why my script would just exit? Also, im sorry if what I am trying to do is a little confusing I tried my best to explain everything but if you have any questions let me know. Thanks in advance for any suggestions!

-Matt


In reply to Win32::OLE lookup registry value on remote machine via WMI by yankaxc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.