View Single Post
  #4  
Old 05-14-2018, 10:22 AM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 1,002
Quote:
Originally Posted by Nyber View Post
SecureCRT appears to clear on every session.
SecureCRT does not "clear on every session"; that's just an illusion -- a magic trick made possible by the magician leveraging a misunderstanding about how the ExpandEnvironmentStrings() is documented to operate. The VOLATILE variables are there, and they're available for SecureCRT to access, but ExpandEnvironmentStrings() cannot see them because it operates on PROCESS variables.
A process's environment variables are inherited from the process's parent (including any that may be VOLATILE). I'm guessing you might have launched SecureCRT *before* launching the .vbs within the Explorer.exe (or CMD.exe) process environment, therefore SecureCRT does not have access to any updated copies of the Explorer.exe-parented (or CMD.exe-parented) PROCESS variables -- unlike when you ran the script outside of SecureCRT. If you had run the .vbs directly w/in Explorer.exe/cmd.exe first, and then launched SecureCRT (from either explorer.exe or cmd.exe, you'd see the desired behavior.

Suggestion: Don't use ExpandEnvironmentStrings() to resolve VOLATILE environment variables. Use a different mechanism to determine whether or not your variable is set/initialized. For example:
Code:
Option Explicit
Dim wshShell,wshSystemEnv,strPassword,nResult
Set wshShell = CreateObject("WScript.Shell")
Set wshSystemEnv = wshShell.Environment("VOLATILE")

' If wshShell.ExpandEnvironmentStrings("%PASSVAR%") = "%PASSVAR%" Then
'   No can do on the above... ExpandEnvironmentStrings() only expands
'   PROCESS variables; not VOLATILE (unless such are inherited from a
'   pareent process in which the VOLATILE variable was already set.
'   To check existence/value of VOLATILE variable, for processes which
'   do not share the same copy of the parent process, use this method:
If wshSystemEnv("PASSVAR") = "" Then
  'If volatile variable is empty then set
  strPassword = InputBox("Enter password", "Password needed")
  wshSystemEnv("PASSVAR") = strPassword
Else
  MsgBox wshSystemEnv("PASSVAR")
End If
--Jake
Attached Images
File Type: png ExpandEnvironmentStrings-is-PROCESS-only.png (54.8 KB, 1061 views)
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
Reply With Quote