View Single Post
  #2  
Old 05-10-2018, 10:02 AM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 998
Setting/Getting environment variables in VBScript works inside SecureCRT the same way that it works outside SecureCRT because:
  1. the script engine is the same and
  2. you're using the WScript.Shell object to do the setting/getting of environment variables.

I've provided some example code below.

Try running the example code:
  • Outside of SecureCRT (using WScript.exe, double-click on the .vbs file w/in the file Explorer or Win+R 'run': Wscript.exe "path_to_this_script_file")
  • Outside of SecureCRT again
  • Inside of SecureCRT (Script > Run)
  • Inside of the same SecureCRT process again.
  • Inside of SecureCRT, after closing SecureCRT and starting a fresh instance.

So, "PROCESS" environment vars are the most secure because they're only accessible within the same process.

"VOLATILE" env variables last only until you log out of Windows.

"USER" env variables are there to stay for your user until you remove/reset them.

"SYSTEM" = "USER", but are available to all users and all processes.


--Jake

Code:
' EnvVarExample_PROCESS_USER_VOLATILE_SYSTEM.vbs

Dim g_shell
Set g_shell = CreateObject("WScript.Shell")

' There are 4 different "Environments":
'   System, User, Volatile, or Process.  For inter-process
'   communication (parent process to child and vice versa),
'   the "Process" environment is used.
Set g_env_prc = g_shell.Environment("PROCESS")
Set g_env_sys = g_shell.Environment("SYSTEM")
Set g_env_usr = g_shell.Environment("USER")
Set g_env_vol = g_shell.Environment("VOLATILE")

strVarName = "VARNAME"
strVarValu = "VARVALUE"

strError = ""

MsgBox _
    "Current values for env variable: " & strVarName & vbcrlf & _
    " Process: " & vbtab & g_env_prc(strVarName) & vbcrlf & _
    "Volatile: " & vbtab & g_env_vol(strVarName) & vbcrlf & _
    "    User: " & vbtab & g_env_usr(strVarName) & vbcrlf & _
    "  System: " & vbtab & g_env_sys(strVarName) & vbcrlf & _
    strError

' Reset the var for all environments:
On Error Resume Next
g_env_prc(strVarName) = ""
g_env_sys(strVarName) = ""
g_env_usr(strVarName) = ""
g_env_vol(strVarName) = ""
On Error Goto 0

MsgBox _
    "All values reset for this variable: " & strVarName & vbcrlf & _
    " Process: " & vbtab & g_env_prc(strVarName) & vbcrlf & _
    "Volatile: " & vbtab & g_env_vol(strVarName) & vbcrlf & _
    "    User: " & vbtab & g_env_usr(strVarName) & vbcrlf & _
    "  System: " & vbtab & g_env_sys(strVarName) & vbcrlf & _
    strError

MsgBox "Setting 'PROCESS' " & strVarName & "=" & strVarValu & "..."
On Error Resume Next
g_env_prc(strVarName) = strVarValu
nError = Err.Number
strErr = Err.Description
On Error Goto 0

If nError <> 0 Then
    strError = vbcrlf & vbcrlf & "Error: " & nError & ": " & strErr
Else
    strError = ""
End If

MsgBox _
    "Values for env variable: " & strVarName & vbcrlf & _
    " Process: " & vbtab & g_env_prc(strVarName) & vbcrlf & _
    "Volatile: " & vbtab & g_env_vol(strVarName) & vbcrlf & _
    "    User: " & vbtab & g_env_usr(strVarName) & vbcrlf & _
    "  System: " & vbtab & g_env_sys(strVarName) & vbcrlf & _
    strError

MsgBox "Setting 'VOLATILE' VARNAME=VARVALU..."
On Error Resume Next
g_env_vol(strVarName) = strVarValu
nError = Err.Number
strErr = Err.Description
On Error Goto 0

If nError <> 0 Then
    strError = vbcrlf & vbcrlf & "Error: " & nError & ": " & strErr
Else
    strError = ""
End If

MsgBox _
    "Values for env variable: " & strVarName & vbcrlf & _
    " Process: " & vbtab & g_env_prc(strVarName) & vbcrlf & _
    "Volatile: " & vbtab & g_env_vol(strVarName) & vbcrlf & _
    "    User: " & vbtab & g_env_usr(strVarName) & vbcrlf & _
    "  System: " & vbtab & g_env_sys(strVarName) & vbcrlf & _
    strError

MsgBox "Setting 'USER' VARNAME=VARVALU..."
On Error Resume Next
g_env_usr(strVarName) = strVarValu
nError = Err.Number
strErr = Err.Description
On Error Goto 0

If nError <> 0 Then
    strError = vbcrlf & vbcrlf & "Error: " & nError & ": " & strErr
Else
    strError = ""
End If

MsgBox _
    "Values for env variable: " & strVarName & vbcrlf & _
    " Process: " & vbtab & g_env_prc(strVarName) & vbcrlf & _
    "Volatile: " & vbtab & g_env_vol(strVarName) & vbcrlf & _
    "    User: " & vbtab & g_env_usr(strVarName) & vbcrlf & _
    "  System: " & vbtab & g_env_sys(strVarName) & vbcrlf & _
    strError

MsgBox "Setting 'SYSTEM' VARNAME=VARVALU..."
On Error Resume Next
g_env_sys(strVarName) = strVarValu
nError = Err.Number
strErr = Err.Description
On Error Goto 0

If nError <> 0 Then
    strError = vbcrlf & vbcrlf & "Error: " & nError & ": " & strErr
Else
    strError = ""
End If

MsgBox _
    "Values for env variable: " & strVarName & vbcrlf & _
    " Process: " & vbtab & g_env_prc(strVarName) & vbcrlf & _
    "Volatile: " & vbtab & g_env_vol(strVarName) & vbcrlf & _
    "    User: " & vbtab & g_env_usr(strVarName) & vbcrlf & _
    "  System: " & vbtab & g_env_sys(strVarName) & vbcrlf & _
    strError
__________________
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