0

I am looking for a method in PowerShell to source the environment variables from running a .bat file script into the Env: provider.

Is there an equivalent to twa_env.cmd that will setup the environment for TWS correctly in PowerShell?

I can start a cmd.exe shell, CALL twa_env.cmd, then start PowerShell. That seems to work. What I cannot yet do is to start a PowerShell shell, run twa_env.cmd, and bring the new variable settings back into the PowerShell Env:.

lit
  • 14,456
  • 10
  • 65
  • 119
  • Yeah: `& tws_env.bat`. Environment variables will be created in the powershell session. – Maximilian Burszley Sep 21 '18 at 14:45
  • @TheIncorrigible1 - That would be really nice. In your experience, does it work? Start a PowerShell console, run `& tws_env.bat`, then run conman or composer. Is that working for you? – lit Sep 21 '18 at 15:30
  • I don't have it installed at the moment, but execute the batch script in a powershell session and run `Get-Command -Name co*` – Maximilian Burszley Sep 21 '18 at 15:34
  • 2
    Are you asking about https://stackoverflow.com/questions/42711294/in-powershell-after-changing-to-cmd-unable-to-call-a-bat-file-through-a-batch-sc/42729430#42729430? – Bill_Stewart Sep 21 '18 at 22:00
  • @Bill_Stewart - Yes, that answer is -very- close. It is almost identical to what I was trying, but I was using `Invoke-Command`. It still does not create a working environment for TWS. I am still investigating. – lit Sep 22 '18 at 20:07
  • Why do you need `Invoke-Comand`? – Bill_Stewart Sep 23 '18 at 00:40
  • 1
    Does this answer your question? [How can I source variables from a .bat file into a PowerShell script?](https://stackoverflow.com/questions/20077820/how-can-i-source-variables-from-a-bat-file-into-a-powershell-script) – Gabriel Devillers Apr 21 '22 at 16:20

1 Answers1

2

PowerShell can run a cmd.exe shell script (batch file), but it (naturally) has to execute it using cmd.exe. The problem is that when the cmd.exe executable closes, the environment variables it sets don't propagate to the calling PowerShell session.

The workaround is to "capture" the environment variables set in the cmd.exe session and manually propagate them to PowerShell after the batch file finishes. The following Invoke-CmdScript PowerShell function can do that for you:

# Invokes a Cmd.exe shell script and updates the environment.
function Invoke-CmdScript {
  param(
    [String] $scriptName
  )
  $cmdLine = """$scriptName"" $args & set"
  & $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
    Select-String '^([^=]*)=(.*)$' |
    ForEach-Object {
      $varName = $_.Matches[0].Groups[1].Value
      $varValue = $_.Matches[0].Groups[2].Value
      Set-Item Env:$varName $varValue
  }
}
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
  • This does work. It was not working for me because the application -REQUIRES- administrator privs. I do not see why that would be needed, but I did not write the app. It also works with the `Invoke-Expression` I used. I will give you the check for this copy of the other answer. – lit Sep 24 '18 at 15:56