I have a batch script that is not able to be changed as it is owned by another application. I want to call that batch script from Powershell and access the variable values. This looks to be a scope issue and maybe I am not calling the batch script correctly. I have tried "&", "invoke-expression", "cmd /c" and "start-process" but the result is the same. Here is the simplified problem.
I have a batch script called batch.bat containing two lines:
set /p variable=what is the fruit:
echo %variable% (from batch)
I have a powershell script containing:
$fp=resolve-path(".\batch.bat")
Invoke-Expression -Command "$fp"
write-host "$env:variable (from PS)"
from cmd prompt:
set variable=banana
powershell .\script.ps1
what is the fruit: orange
orange (from batch)
banana (from PS)
I want orange to be available to PS. If I hadn't preset the variable to banana, then PS would not display anything so it has nothing to do with presetting the value. I considered that I could write another wrapper command script that executes the original batch script, then writes out all the variables (in PS format) to a temp .ps1 file and then execute (dot source) that but it seems clunky.