Skip to content

Instantly share code, notes, and snippets.

@bradwilson
Created March 7, 2017 04:19
Show Gist options
  • Save bradwilson/3fca203370d54304eff1cce21ffc32aa to your computer and use it in GitHub Desktop.
Save bradwilson/3fca203370d54304eff1cce21ffc32aa to your computer and use it in GitHub Desktop.
Invoke a CMD/BAT script and scrape the environment variable changes
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)][string] $Script,
[Parameter(Position=1, Mandatory=$false)][string] $Parameters
)
$tempFile = [IO.Path]::GetTempFileName()
cmd /c " `"$script`" $parameters && set > `"$tempFile`" "
Get-Content $tempFile | %{
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