There seems to be no way to control the timeout that ShellEnvironment uses for the notification broadcast.
So I decided to build a workaround.
I call this script:
Option Explicit
Dim Name
Dim NewVal
If WScript.Arguments.Count <> 2 then
WScript.Echo "setUSEREnv.vbs: Pass name and value of USER environment variable to set on the command line"
WScript.Quit (1)
End If
Name=CStr (WScript.Arguments.Item (0))
NewVal=CStr (WScript.Arguments.Item (1))
Dim ShellEnvironment: Set ShellEnvironment=CreateObject ("WScript.Shell").Environment ("USER")
If ShellEnvironment (Name) <> NewVal then
ShellEnvironment (Name)=NewVal
WScript.Echo "Setting USER env var '" & Name & "'..."
WScript.Echo "USER env var '" & Name & "' set to '" & NewVal & "'"
else
WScript.Echo "USER env var '" & Name & "' already is '" & NewVal & "'"
End If
from my setenv function code using Run
:
Dim ShellEnvironment: Set ShellEnvironment=CreateObject ("WScript.Shell").Environment ("USER")
Dim Name: Name=EnvVarName
If ShellEnvironment (Name) <> NewVal Then
Dim Shell: Set Shell=CreateObject ("WScript.Shell")
Shell.Run "cscript.exe ""c:\mydir\setUSERENV.vbs"" " & Name & " " & CStr (NewVal), 0, false
End If
This still insults me with a long delay for every env var write access, but at least the main script keeps running, and so I don´t have to wait.