ej has asked for the wisdom of the Perl Monks concerning the following question:

The following code works great on the local machine if I remove the //$host/ from the path name in the Open methods, but fails on the first Open attempt when I try to run it against a remote machine. Any ideas?
use Win32::TieRegistry (Delimiter => "/"); my $key; my $host = "ajax"; if ($key = $Registry->Open("//$host/LMachine/SOFTWARE/FOO/BAR/")) { $key->CreateKey("NewKey//") or print "$^E\n"; $key = $Registry->Open("//$host/LMachine/SOFTWARE/FOO/BAR/NewKey/"); $key->SetValue("MACAddress", "BIGMAC") or print "$^E\n"; } else { print "Error: $^E\n"; }

Replies are listed 'Best First'.
Re: Modifying Win32 registry on remote hosts
by ibanix (Hermit) on Nov 20, 2002 at 23:17 UTC
    I have some code which uses Win32::TieRegistry.

    Make sure you have permission to modify the registry on the box. It will require you to have local or domain administrator rights, which you can usually do like this:

    $output = `net use \\\\$server\\ipc\$ \"$password\" \/user:$domain\\$l +ogin`; if ($output =~ /success/) { do_something(); } else { die "could not connect to $server!\n"; }


    This assumes you're using ActivePerl on a Win2K box, by the way. I have no idea how this works from a UNIX box.

    /rgds

    <-> In general, we find that those who disparage a given operating system, language, or philosophy have never had to use it in practice. <->
      I have no idea how this works from a UNIX box.

      It doesn't. Although I have heard of people working on porting Win32::TieRegistry to Unix, I haven't heard of any results and I suspect that the remote access stuff is not part of it.

      As to the original question, it would help to know what version of Win32::TieRegistry is being used (and what version of Win32API::Registry as well), and what $^E is. It is also better to use regLastError() [ which requires use Win32API::Registry qw( regLastError ); ] instead of $^E (which can get overwritten, especially with some versions of Perl).

      Using Win32::NetResource is perhaps a cleaner way to authenticate to the remote machine if that is part of the problem.

              - tye
Re: Modifying Win32 registry on remote hosts
by traveler (Parson) on Nov 20, 2002 at 22:34 UTC
    Do you have permission to edit the registry on the remote machine (ajax)? Have you tried another tool to see that you do? Unless you have set up the permissions correctly it is unlikely to work.

    HTH, --traveler

Re: Modifying Win32 registry on remote hosts
by ej (Initiate) on Nov 21, 2002 at 14:27 UTC
    OK...thanks for the tips. I've determined thru regLastError() (thanks tye!) that it's a permissions issue. I'm logged in as domain admin, and I figured that would be sufficient.

    I guess it's getting a bit off-topic for this forum, but is there somewhere else in Win2K that explicitly allows or denies remote registry access?

    And thanks again...you guys rock. I wish I'd known about this site three months ago when I first started using Perl!

      Yes it is slightly offtopic. But you could check if there was a global policy in place. Some companies don't want people touching the registry. You might also remove and add your machine to the domain. I have seen dorked machine accounts do strange things.

      Some other considerations:

      What if the machine is down. Tie-Registry will give you a can't open the registry error. Which at times could be misleading and make you walk to the machine.

      If you make use of Net::Ping you could give yourself a down message.

      Anothing. If you have a lousy Windows ME on the network. Well you don't have Remote Registry access so you will have to code around it.

      Here is a partial snippet of a domain virus report I wrote.

      if ($p->ping($computer)) { if ($key = $Registry->Open("/$computer/LMachine/SYSTEM/CurrentCont +rolSet/" . "Control/ComputerName/ComputerName/", {Access=>KEY_REA +D}) ) { unless ($name = $key->GetValue("ComputerName")) { print "Could not attach to read ComputerName o +n $computer\n"; printf $fh "Could not attach to read ComputerN +ame on $computer\n"; }

      The ping takes care of machines that are down and the open took care of registry access issues.

Re: Modifying Win32 registry on remote hosts
by ej (Initiate) on Nov 22, 2002 at 14:33 UTC
    OK, so I'm an idiot. I spent most of yesterday re-building my four machines in the lab, and the permissions issue disappeared. It appears that in the week I was gone, someone was messing around with my lab setup and really gummed up the works.

    The code works great now. Thanks again to all that responded!