Created
January 22, 2016 21:28
-
-
Save elovelan/fb01e69ac104c8292c8e to your computer and use it in GitHub Desktop.
This file contains 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
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