How to set environment variables in vbs that can be read in calling batch script

后端 未结 5 455
太阳男子
太阳男子 2021-01-11 17:23

I have a batch file that calls a vbscript file. I am trying to have the vbscript file change an environment variable that is later used in the batch file that calls the vbsc

5条回答
  •  青春惊慌失措
    2021-01-11 17:31

    yes, you can.... however, you'll have to resetvars in your session. see the following link:

    Is there a command to refresh environment variables from the command prompt in Windows?

    'RESETVARS.vbs

    Set oShell = WScript.CreateObject("WScript.Shell")
    filename = oShell.ExpandEnvironmentStrings("%TEMP%\resetvars.bat")
    Set objFileSystem = CreateObject("Scripting.fileSystemObject")
    Set oFile = objFileSystem.CreateTextFile(filename, TRUE)
    
    set oEnv=oShell.Environment("System")
    for each sitem in oEnv 
        oFile.WriteLine("SET " & sitem)
    next
    path = oEnv("PATH")
    
    set oEnv=oShell.Environment("User")
    for each sitem in oEnv 
        oFile.WriteLine("SET " & sitem)
    next
    
    path = path & ";" & oEnv("PATH")
    oFile.WriteLine("SET PATH=" & path)
    oFile.Close
    

    This is how I did it:

    SET oShell = CREATEOBJECT("Wscript.Shell") 
    dim varSet
    SET varSet = NOTHING 
    
      SET varSet = oShell.Environment("SYSTEM") 
      varSet("WinVer") = "6.0.2008"
    

    Then in a separate VB script (resetvars.vbs) I called from CMD script:

    cscript //nologo \\%APPSERVER%\apps\IE9.0\restartvars.vbs   
    call %TEMP%\resetvars.bat
    

提交回复
热议问题