Jump to content

Recommended Posts

Posted

Thanks Subz,

So that means we can't even see the environment variable from the windows system properties?

I learnt that the variable set with EnvSet() is only available to the sub sessions spawned by the script itself, but didn't know that it doesn't even update the variables at the windows level...: )

Regards..

 

 

Posted

Correct, I've been working on a UDF but been caught up with other project work, so haven't really been able to do a complete test:

#RequireAdmin ;~ Required when writing to System Environment Variable
#include <Array.au3>
; #VARIABLES# ===================================================================================================================
Global $MAX_VALUE_NAME = 1024
Global $HWND_BROADCAST = 0xffff
Global $WM_SETTINGCHANGE = 0x001A
Global $SMTO_ABORTIFHUNG = 0x0002
Global $SMTO_NORMAL = 0x0000
Global $MSG_TIMEOUT = 5000

;~ ============================== Begin Example ===============================
;~ If Machine Environment Variable "LSFORCEHOST" = "192.168.1.189" Then Change it to "license01.local"
    If _EnvVarGet("LSFORCEHOST", 2) = "192.168.1.189" Then _EnvVarSet("LSFORCEHOST", "license01.local", 2)
;~ If Machine Environment Variable "LSHOST" = "192.168.1.189" Then Change it to "license01.local"
    If _EnvVarGet("LSHOST", 2) = "192.168.1.189" Then _EnvVarSet("LSFORCEHOST", "license01.local", 2)
;~ If Machine Environment Variable "SPSS_LMHOST" = "192.168.1.189" Then Change it to "license01.local"
    If _EnvVarGet("SPSS_LMHOST", 2) = "192.168.1.189" Then _EnvVarSet("LSFORCEHOST", "license01.local", 2)
;~ =============================== End Example ================================

;~ $_iEnvType = 0 - Returns Enviroment Variables from all profiles
;~ $_iEnvType = 1 = Process Enviornment Variable
;~ $_iEnvType = 2 = System Enviornment Variable
;~ $_iEnvType = 3 = User Enviornment Variable
;~ $_iEnvType = 4 = Volatile Enviornment Variable

Func _EnvVarGet($_sEnvVarGet, $_iEnvVarType = 3, $_bExpVarValue = True)
    Local $iExpandEnvStrings = Opt("ExpandEnvStrings")
    Local $sEnvVarValue, $aEnvVarValue[5] = [4]
    Local $oEnvVarType, $aEnvVarType[5] = [4, "PROCESS", "SYSTEM", "USER", "VOLATILE"]
    Local $oWshShell = ObjCreate("WScript.Shell")
    Switch $_iEnvVarType
        Case 0
            If $_bExpVarValue == True Then Opt("ExpandEnvStrings", 1)
            For $i = 1 To $aEnvVarType[0]
                $oEnvVarType = $oWshShell.Environment($aEnvVarType[$i])
                $aEnvVarValue[$i] = $oEnvVarType($_sEnvVarGet)
            Next
            Opt("ExpandEnvStrings", $iExpandEnvStrings)
            Return $aEnvVarValue
        Case 1 To 4
            If $_bExpVarValue == True Then Opt("ExpandEnvStrings", 1)
            $oEnvVarType = $oWshShell.Environment($aEnvVarType[$_iEnvVarType])
            $sEnvVarValue = $oEnvVarType($_sEnvVarGet)
            Opt("ExpandEnvStrings", $iExpandEnvStrings)
            Return $sEnvVarValue
        Case Else
            Return SetError(1, 0, "")
    EndSwitch
EndFunc

;~ $_iEnvType = 0 - Sets Enviroment Variable for all profiles
;~ $_iEnvType = 1 = Set Process Enviornment Variable
;~ $_iEnvType = 2 = Set System Enviornment Variable
;~ $_iEnvType = 3 = Set User Enviornment Variable
;~ $_iEnvType = 4 = Set Volatile Enviornment Variable
Func _EnvVarSet($_sEnvVarName = "", $_sEnvVarValue = "", $_iEnvVarType = 3)
    Local $oEnvVarType, $aEnvVarType[5] = [4, "PROCESS", "SYSTEM", "USER", "VOLATILE"]
    Local $oWshShell = ObjCreate("WScript.Shell")
    Switch $_iEnvVarType
        Case 0
            For $i = 1 To $aEnvVarType[0]
                $oEnvVarType = $oWshShell.Environment($aEnvVarType[$i])
                $oEnvVarType($_sEnvVarName) = $_sEnvVarValue
            Next
        Case 1 To 4
            $oEnvVarType = $oWshShell.Environment($aEnvVarType[$_iEnvVarType])
             $oEnvVarType($_sEnvVarName) = $_sEnvVarValue
        Case Else
            Return SetError(1)
    EndSwitch
    _EnvVarRefresh()
    If @error Then Return SetError(1)
EndFunc

;~ $_iEnvType = 0 - Returns Enviroment Variables from all profiles
;~ $_iEnvType = 1 = Process Enviornment Variable
;~ $_iEnvType = 2 = System Enviornment Variable
;~ $_iEnvType = 3 = User Enviornment Variable
;~ $_iEnvType = 4 = Volatile Enviornment Variable

Func _EnvVarDelete($_sEnvVarName = "", $_iEnvVarType = 3)
    Local $oEnvVarType, $aEnvVarType[5] = [4, "PROCESS", "SYSTEM", "USER", "VOLATILE"]
    Local $oWshShell = ObjCreate("WScript.Shell")
    Switch $_iEnvVarType
        Case 0
            For $i = 1 To $aEnvVarType[0]
                $oEnvVarType = $oWshShell.Environment($aEnvVarType[$i])
                $oEnvVarType.Remove($_sEnvVarName)
            Next
        Case 1 To 4
            $oEnvVarType = $oWshShell.Environment($aEnvVarType[$_iEnvVarType])
             $oEnvVarType.Remove($_sEnvVarName)
        Case Else
            Return SetError(1, 0, "")
    EndSwitch
    _EnvVarRefresh()
    If @error Then Return SetError(1)
EndFunc

Func _EnvVarRefresh()
    ; http://msdn.microsoft.com/en-us/library/ms644952%28VS.85%29.aspx
    $iRet2 = DllCall("user32.dll", "lresult", "SendMessageTimeoutW", _
            "hwnd", $HWND_BROADCAST, _
            "dword", $WM_SETTINGCHANGE, _
            "ptr", 0, _
            "wstr", "Environment", _
            "dword", $SMTO_ABORTIFHUNG, _
            "dword", $MSG_TIMEOUT, _
            "dword_ptr*", 0)

    If $iRet2[0] = 0 Then Return SetError(1)
EndFunc

 

Posted

Wow!! Thanks Subz!

I'm struggling to get the contents of a file written into a environment var. I think my problem is not with the writing part, but with the contents that i want to write. The contents of the file is multi lines and with double quotes in many places.

I tried a StringReplace to double up on the quotes so that they stay in the string, and though from ConsoleWrite, it looks correct with the dual double quotes, the moment it gets into the environment var, some of them disappear...

Example:

In Console it reads: <i1000ServiceConfig MachineID=""4"">

After written to environment var, it reads : <i1000ServiceConfig MachineID="4> 

I'll try what you provided.. thanks!!

 

 

 

Posted

If you use my script above it would be something like:

_EnvVarSet("ServiceConfig.xml.EnvName.ID", '<i1000ServiceConfig MachineID="4">', 2)

Or

Run (@ComSpec & ' /c SETX ServiceConfig.xml.EnvName.ID "<i1000ServiceConfig MachineID=""""4"""">"', "", @SW_HIDE)

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...