Skip to content

Instantly share code, notes, and snippets.

@elovelan
Created January 22, 2016 21:28
Show Gist options
  • Save elovelan/fb01e69ac104c8292c8e to your computer and use it in GitHub Desktop.
Save elovelan/fb01e69ac104c8292c8e to your computer and use it in GitHub Desktop.
function ConvertFrom-GlobalVariables {
param(
[hashtable]$ExpectedVariables,
[string]$ScriptPath
)
$ExpectedVariables.Keys | ForEach-Object {
Set-Variable -Name $_ -Value $ExpectedVariables[$_]
}
. $ScriptPath
# this feels hacky, can't find a better way :(
# find all variables set by the script that aren't in the parent or changed in the child
# this will also include everything in $ExpectedVariables
$_parent = @{}; $_local = @{}
Get-Variable -Scope 1 | % { $_parent[$_.Name] = $_.Value }
Get-Variable -Scope 0 | Where-Object {
($_parent[$_.Name] -ne $_.Value) -and
('MyInvocation','PSBoundParameters' -notcontains $_.Name) -and
(-not $_.Name.StartsWith('_'))
} | ForEach-Object {
$_local[$_.Name] = $_.Value
}
$_local
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment