Skip to content

Instantly share code, notes, and snippets.

@markekraus
Created April 18, 2018 18:14
Show Gist options
  • Save markekraus/461c9ff609f2534012599e3ae99502d2 to your computer and use it in GitHub Desktop.
Save markekraus/461c9ff609f2534012599e3ae99502d2 to your computer and use it in GitHub Desktop.
String Expansion Scoping... Wat?
$MyObjGlobal = [PSCustomObject]@{
PSTypeName = 'MyObj'
Prop1 = 'Value1'
}
$MyObjFunction = [PSCustomObject]@{
PSTypeName = 'MyObj'
Prop1 = 'Value1'
}
$Test = 'In Global'
$MyObjGlobal | Add-Member -Name Expand -MemberType ScriptMethod -Value {param([string]$string) return $ExecutionContext.InvokeCommand.ExpandString($string)}
$String = 'Test: $Test'
' '
' '
' '
' '
'----------------------------------'
$MyObjGlobal.Expand($String)
function DumbFunction ($GlobalObj, $FunctionObj, $String) {
$Test = 'In Dumb Function'
$GlobalObj.Expand($string)
$FunctionObj | Add-Member -Name Expand -MemberType ScriptMethod -Value {param([string]$string) return $ExecutionContext.InvokeCommand.ExpandString($string)}
$FunctionObj.Expand($string)
}
'----------------------------------'
DumbFunction $MyObjGlobal $MyObjFunction $String
function Test-AdvFunction {
[CmdletBinding()]
param (
$MyObjGlobal,
$MyObjFunction,
$String
)
end {
$Test = 'In Advanced Function'
$MyObjGlobal.Expand($string)
$MyObjFunction | Add-Member -Name Expand -MemberType ScriptMethod -Value {param([string]$string) return $ExecutionContext.InvokeCommand.ExpandString($string)}
$MyObjFunction.Expand($string)
}
}
$MyObjFunction = [PSCustomObject]@{
PSTypeName = 'MyObj'
Prop1 = 'Value1'
}
'----------------------------------'
Test-AdvFunction -MyObjGlobal $MyObjGlobal -MyObjFunction $MyObjFunction -String $String
$Module = New-Module {
$Test = 'In Module'
$MyModuleObj = [PSCustomObject]@{
PSTypeName = 'MyObj'
Prop1 = 'Value1'
}
$MyModuleObj | Add-Member -Name Expand -MemberType ScriptMethod -Value {param([string]$string) return $ExecutionContext.InvokeCommand.ExpandString($string)}
$MyModuleFunctionObj = [PSCustomObject]@{
PSTypeName = 'MyObj'
Prop1 = 'Value1'
}
$MyModuleFunctionObj2 = [PSCustomObject]@{
PSTypeName = 'MyObj'
Prop1 = 'Value1'
}
$MyModuleFunctionObj3 = [PSCustomObject]@{
PSTypeName = 'MyObj'
Prop1 = 'Value1'
}
function ModuleDumbFunction ($GlobalObj, $FunctionObj, $String) {
$Test = 'In Module Dumb Function'
$GlobalObj.Expand($string)
$FunctionObj | Add-Member -Name Expand -MemberType ScriptMethod -Value {param([string]$string) return $ExecutionContext.InvokeCommand.ExpandString($string)}
$FunctionObj.Expand($string)
$Script:MyModuleObj.Expand($string)
$Script:MyModuleFunctionObj | Add-Member -Name Expand -MemberType ScriptMethod -Value {param([string]$string) return $ExecutionContext.InvokeCommand.ExpandString($string)}
$Script:MyModuleFunctionObj.Expand($String)
}
Export-ModuleMember -Function ModuleDumbFunction
function Test-ModuleAdvFunction {
[CmdletBinding()]
param (
$MyObjGlobal,
$MyObjFunction,
$String
)
end {
$Test = 'In Module Advanced Function'
$MyObjGlobal.Expand($string)
$MyObjFunction | Add-Member -Name Expand -MemberType ScriptMethod -Value {param([string]$string) return $ExecutionContext.InvokeCommand.ExpandString($string)}
$MyObjFunction.Expand($string)
$Script:MyModuleObj.Expand($string)
$Script:MyModuleFunctionObj2 | Add-Member -Name Expand -MemberType ScriptMethod -Value {param([string]$string) return $ExecutionContext.InvokeCommand.ExpandString($string)}
$Script:MyModuleFunctionObj2.Expand($String)
}
}
Export-ModuleMember -Function Test-ModuleAdvFunction
function Get-UpdatedObject {
[CmdletBinding()]
param (
$Object
)
end {
$Test = 'In Get-UpdatedObject'
$Object | Add-Member -Name Expand -MemberType ScriptMethod -Value {param([string]$string) return $ExecutionContext.InvokeCommand.ExpandString($string)} -PassThru
$Script:MyModuleObj
$Script:MyModuleFunctionObj3 | Add-Member -Name Expand -MemberType ScriptMethod -Value {param([string]$string) return $ExecutionContext.InvokeCommand.ExpandString($string)} -PassThru
[PSCustomObject]@{
PSTypeName = 'MyObj'
Prop1 = 'Value1'
} | Add-Member -Name Expand -MemberType ScriptMethod -Value {param([string]$string) return $ExecutionContext.InvokeCommand.ExpandString($string)} -PassThru
}
}
Export-ModuleMember -Function Get-UpdatedObject
}
Import-Module $Module
'----------------------------------'
$MyObjFunction = [PSCustomObject]@{
PSTypeName = 'MyObj'
Prop1 = 'Value1'
}
ModuleDumbFunction $MyObjGlobal $MyObjFunction $String
'----------------------------------'
$MyObjFunction = [PSCustomObject]@{
PSTypeName = 'MyObj'
Prop1 = 'Value1'
}
Test-ModuleAdvFunction -MyObjGlobal $MyObjGlobal -MyObjFunction $MyObjFunction -String $String
'-----------------------------------'
$MyObjFunction = [PSCustomObject]@{
PSTypeName = 'MyObj'
Prop1 = 'Value1'
}
$out = Get-UpdatedObject -Object $MyObjFunction
$out.Expand($String)
<# OUtput:
----------------------------------
Test: In Global
----------------------------------
Test: In Dumb Function
Test: In Dumb Function
----------------------------------
Test: In Advanced Function
Test: In Advanced Function
----------------------------------
Test: In Global
Test: In Module Dumb Function
Test: In Module Dumb Function
Test: In Module Dumb Function
----------------------------------
Test: In Global
Test: In Module Advanced Function
Test: In Module Advanced Function
Test: In Module Advanced Function
-----------------------------------
Test: In Module
Test: In Module
Test: In Module
Test: In Module
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment