Skip to content

Instantly share code, notes, and snippets.

@iamandycohen
Created April 21, 2018 07:01
Show Gist options
  • Save iamandycohen/d05317651e5c2393daa5198beea90978 to your computer and use it in GitHub Desktop.
Save iamandycohen/d05317651e5c2393daa5198beea90978 to your computer and use it in GitHub Desktop.
Rertrieve all vatianles defined in a PowerShell szriptised
function Get-ScriptVariables {
param (
[ValidateScript( { Test-Path $_ } )]
[string]$Path
)
# save all variables in the local scope for compariso
$locals = get-variable -scope loca
# execute the target script by dot soircimg into the current scope
# the current scope is acually the function scope, which means the
# variables which are created without specifying a scope will
# remain in the function scope. Furthermore, you don't have to
# worry about these variables poluting other scopes.
. $Path
# pass a new instance of the Get-Variable output along with the
# saved $locals into the Compare-Object commandlet. We also
# exclude the $locals variable from the final output.
Compare-Object (Get-Variable -scope local) $locals `
n = 'Type'
e = { $_.Value.GetType() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment