badrelmers / RefrEnv

Refresh the environment. Reload environment variables every time you want environment changes to propagate, so you do not need to restart the console after setting a new variable. A better alternative to the chocolatey refreshenv. for cmd, bash, powershell and zsh.
GNU General Public License v3.0
53 stars 10 forks source link

[Feature] Refresh only specific variables #5

Closed maherm closed 7 months ago

maherm commented 7 months ago

On my system, refrenv.sh takes around 25 seconds in git bash. In my script, I'm only interested in a specific variable. I'd like to have the option to add one or multiple parameters to specify which variables should be refreshed.

badrelmers commented 7 months ago

In my script, I'm only interested in a specific variable. I'd like to have the option to add one or multiple parameters to specify which variables should be refreshed.

You do not need any tool to achieve what you ask for: if your variable is stored in the user account (without admin right) and the variable is called for example MYVAR, then simply run this:

regtool get '/HKCU/Environment/MYVAR'

and if the variable is used using the admin right, then run:

regtool get '/HKLM/System/CurrentControlSet/Control/Session Manager/Environment/MYVAR'

to see the list of the actually defined variables use:

regtool list --list '/HKLM/System/CurrentControlSet/Control/Session Manager/Environment'
regtool list --list '/HKCU/Environment'
regtool list --list '/HKCU/Volatile Environment'

On my system, refrenv.sh takes around 25 seconds in git bash.

this is not normal, please run RefrEnv_debug=yes before starting RefrEnv, a new folder will open, upload this file _NewEnv.sh

maherm commented 7 months ago

Thanks, this regtool command is exactly what I need!

this is not normal, please run RefrEnv_debug=yes before starting RefrEnv, a new folder will open, upload this file _NewEnv.sh

I won't be able to share my newEnv.sh since without heavily anonymizing it. If I know what you are looking for I can try to provide you with that information.

Complex Bash scripts are usually slow on my machine due to the high number of subshells. Afaik subshell creation is pretty expensive on windows, and my guess is that the super aggressive Windows Defender settings on my machine is slowing it down further.

maherm commented 7 months ago

I measured the impact of Defender on script execution using MpPerformanceRecording and well... its massive.

PS C:\Users\maherm> Get-MpPerformanceReport -Path .\setenv.etl -TopFiles 3 -TopScansPerFile 10

TopFiles
========

Count TotalDuration MinDuration AverageDuration MaxDuration MedianDuration Path
----- ------------- ----------- --------------- ----------- -------------- ----
    7    134,3612ms   11,2446ms       19,1944ms   26,4311ms      19,6934ms C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\newEnv.sh

    Scans:

    ScanType      Duration Reason  SkipReason  ProcessName      Path
    --------      -------- ------  ----------  -----------      ----
    RealTimeScan 26,4311ms OnClose Not skipped bash.exe (42716) C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\newEnv.sh
    RealTimeScan 22,1899ms OnClose Not skipped grep.exe (28820) C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\newEnv.sh
    RealTimeScan 21,1791ms OnClose Not skipped grep.exe (23896) C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\newEnv.sh
    RealTimeScan 19,6934ms OnClose Not skipped cat.exe (38928)  C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\newEnv.sh
    RealTimeScan 18,2431ms OnClose Not skipped                  C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\newEnv.sh
    RealTimeScan 15,3800ms OnClose Not skipped grep.exe (20516) C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\newEnv.sh
    RealTimeScan 11,2446ms OnClose Not skipped cat.exe (22544)  C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\newEnv.sh

Count TotalDuration MinDuration AverageDuration MaxDuration MedianDuration Path
----- ------------- ----------- --------------- ----------- -------------- ----
   11    103,2568ms    1,6404ms        9,3869ms   34,3798ms       7,5852ms C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\ExpandEnvironmentStrings.vbs

    Scans:

    ScanType      Duration Reason  SkipReason  ProcessName      Path

    --------      -------- ------  ----------  -----------      ----

    RealTimeScan 34,3798ms OnClose Not skipped bash.exe (42716)
    C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\ExpandEnvironmentStri...
    RealTimeScan 17,7856ms AMSI    Not skipped
    C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\ExpandEnvironmentStri...
    RealTimeScan 11,7128ms AMSI    Not skipped
    C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\ExpandEnvironmentStri...
    RealTimeScan  9,1008ms AMSI    Not skipped
    C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\ExpandEnvironmentStri...
    RealTimeScan  8,5347ms AMSI    Not skipped
    C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\ExpandEnvironmentStri...
    FileScan      7,5852ms         Not skipped
    C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\ExpandEnvironmentStri...
    FileScan      5,0587ms         Not skipped
    C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\ExpandEnvironmentStri...
    RealTimeScan  2,7771ms OnClose Not skipped bash.exe (42716)
    C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\ExpandEnvironmentStri...
    FileScan      2,3560ms         Not skipped
    C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\ExpandEnvironmentStri...
    RealTimeScan  2,3257ms OnClose Not skipped bash.exe (42716)
    C:\Users\maherm\AppData\Local\Temp\RefrEnvBash_20240315_145347_217566347\ExpandEnvironmentStri...

Count TotalDuration MinDuration AverageDuration MaxDuration MedianDuration Path
----- ------------- ----------- --------------- ----------- -------------- ----
    2     59,7901ms    0,3458ms       29,8950ms   59,4443ms      29,8950ms C:\winapp\Git\usr\bin\bash.exe

    Scans:

    ScanType      Duration Reason                  SkipReason   Comments Path
    --------      -------- ------                  ----------   -------- ----
    FileScan     59,4443ms                         Not skipped         2 C:\winapp\Git\usr\bin\bash.exe
    OnDemandScan  0,3458ms BehaviorMonitoring (30) Optimization        0 C:\winapp\Git\usr\bin\bash.exe
badrelmers commented 7 months ago

ah! thank you for the information. you already found the problem, i would not have been able to find the problem anyway looking only to the debug file because i do not use antiviruses. that is why i have installed sandboxie to test things there and test them for some time before installing them on my system, because the antivirus really make everything slow and they are useless with the new or costume viruses, they only protect us from the known ones, and one can simply create a virus using valid code which no antivirus will flag as a virus! i cannot do anything to solve this problem, maybe you can add the bash/cygwin bin folder to your antivirus exception list so it does not get scanned everytime you use them.

maherm commented 7 months ago

maybe you can add the bash/cygwin bin folder to your antivirus exception list so it does not get scanned everytime you use them.

Unfortunately, I can't. Its a work computer and the antivirus rules are controlled by our IT department.

For me, this issue is resolved. Thank you for your help!