Create env variable (Perl)

This code can be found in Chapter 2 of Windows Server Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# This code creates a new system environment variable called FOOBAR.
# ---------------------------------------------------------------
# Adapted from VBScript code contained in the book:
#      "Windows Server Cookbook" by Robbie Allen
# ISBN: 0-596-00633-0
# ---------------------------------------------------------------
use Win32::OLE;
$Win32::OLE::Warn = 3;

# ------ SCRIPT CONFIGURATION ------
$strVarName = 'FOOBAR';
$strVarValue = 'Foobar Value';
$strComputer = '.';
# ------ END CONFIGURATION ---------
$objVarClass = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2:Win32_Environment');
$objVar = $objVarClass->SpawnInstanceobjVar->Name eq $strVarName;
$objVar->{VariableValue} = $strVarValue;
$objVar->{UserName} = '<SYSTEM>';
$objVar->PutWScript->Echo('Created environment variable ' . $strVarName);

This code has been viewed 1486 times.

New from the creators of TechTasks.com: StatSheet.com