It appears WMI can do this, too.

Attached is a script (setenv.pl) which takes a hostname, variable
name, and value, and sets the corresponding system environment
variable to that value.  Setting non-system variables is left as an
exercise for the reader.

I do not know what notification limitations it has, if any.  I suspect
it behaves just like the control panel GUI.

 - Pat

use warnings;
use strict;
use Win32::OLE;

sub die_usage () {
    die "Usage: $0 <hostname> <var> <value>\n";
}

scalar @ARGV == 3
    or die_usage ();

my ($hostname, $var_name, $value) = @ARGV;

# Bomb out completely if COM engine encounters any trouble.
Win32::OLE->Option ('Warn' => 3);

# Get a handle to the SWbemServices object of the local machine.
my $computer = Win32::OLE->GetObject ("WinMgmts://$hostname/");

# Get the environment variable class object.  See:
# <http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_environment.asp>
my $var_class = $computer->Get ("Win32_Environment");

my $var = $var_class->Spawninstance_ ();

$var->{'Name'} = $var_name;
$var->{'Username'} = "<SYSTEM>";
$var->{'VariableValue'} = $value;

$var->Put_();

Reply via email to