debugging a PS script that requires $env variables to be set via .bat file fails – smart solution?

by Feb 25, 2011

I'm learning PS and believe to have progressed to an 'intermediate level' but am encountering a stumbling block now regarding debugging a PS script that requires $env variables to be set via .bat file.

In order to detach a PS script from the real run-time environment (i.e directory names, email accounts,..) the solution I used is to read ~ 20 settings from environment variables.

These environment variables are set just before the PS script gets called in a large 'env_setup.bat' file.

This is a fully working approach for 'normal' operations mode but creates an additional hurdle for debugging. This is  because the 'open' method in explorer doesn't allow for successful execution of the script in debug mode any longer as the required $env variables are not  set!

I'm using PowerGUI to debug but I'd guess the same issue also affects all other tools.

I've tried to workaround this hurdle by using 'invoke-item env_setup.bat" but the $env variables got not set in the debug scope. Hope I explained this correctly.

I'm hoping to have just overlooked a smart solution to this issue.

To illustrate:

1) create file 'env_setup.bat' with content:

set LMS_env_setup=1

2) create 'env_test.ps1'

invoke-item "..env_setup.bat"

if ($env:LMS_env_setup -ne 1)
{
    write-host "error: environment variables not set!"
    break
}

3) open 'env_test.ps1' in PowerGUI and run in debug mode

I always get "error: environment variables not set!"

 

Any other solution or workaround you could think of?