Created
August 4, 2017 18:59
-
-
Save botmtl/43291fbff1983f27eb563c923fb51f8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Invokes the specified batch file and retains any environment variable changes | |
it makes. | |
.DESCRIPTION | |
Invoke the specified batch file (and parameters), but also propagate any | |
environment variable changes back to the PowerShell environment that | |
called it. | |
.PARAMETER Path | |
Path to a .bat or .cmd file. | |
.PARAMETER Parameters | |
Parameters to pass to the batch file. | |
.EXAMPLE | |
C:\PS> Invoke-BatchFile "$env:VS90COMNTOOLS\..\..\vc\vcvarsall.bat" | |
Invokes the vcvarsall.bat file to set up a 32-bit dev environment. All | |
environment variable changes it makes will be propagated to the current | |
PowerShell session. | |
.EXAMPLE | |
C:\PS> Invoke-BatchFile "$env:VS90COMNTOOLS\..\..\vc\vcvarsall.bat" amd64 | |
Invokes the vcvarsall.bat file to set up a 64-bit dev environment. All | |
environment variable changes it makes will be propagated to the current | |
PowerShell session. | |
.NOTES | |
Author: Lee Holmes | |
#> | |
function Invoke-BatchFile | |
{ | |
param([string]$Path, | |
[string]$Parameters | |
) | |
$tempFile = [IO.Path]::GetTempFileName() | |
## Store the output of cmd.exe. We also ask cmd.exe to output | |
## the environment table after the batch file completes | |
cmd.exe /c " `"$Path`" $Parameters && set > `"$tempFile`" " | |
## Go through the environment variables in the temp file. | |
## For each of them, set the variable in our local environment. | |
Get-Content $tempFile | Foreach-Object { | |
if ($_ -match "^(.*?)=(.*)$") | |
{ | |
Set-Content "env:\$($matches[1])" $matches[2] | |
} | |
} | |
Remove-Item $tempFile | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment