I'm finding that I am repeating myself in powershell scripts in some cases where execution context matters. Delayed string expansion is one such case:
$MyString = 'The $animal says $sound.'
function MakeNoise{
param($animal, $sound)
$ExecutionContext.InvokeCommand.ExpandString($MyString)
}
PS> MakeNoise pig oink
The pig says oink.
That long ExpandString()
line gets repeated frequently. I'd prefer that line to be terse like this:
xs($MyString)
xs $MyString
$MyString | xs
Any of these would be preferable. My usual strategies of encapsulation in commandlets don't seem to work for this case because the context of the call to ExpandString()
is critical.
So my questions are:
- Is there a way to create an alias for an object method?
- Is there some other way call an object's method in a terse manner while preserving the context of the call?