Is there a way to create aliases for object methods in powershell?

只愿长相守 提交于 2019-12-12 04:55:55

问题


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:

  1. Is there a way to create an alias for an object method?
  2. Is there some other way call an object's method in a terse manner while preserving the context of the call?

回答1:


It seems you need a way to delay evaluating $ExecutionContext until it's time to actually do the string expansion. Here's one way to do that, implemented as a function:

function xs
{
    [CmdletBinding()]
    param
    (
        [parameter(ValueFromPipeline=$true,
                   Position=0,
                   Mandatory=$true)]
        [string]
        $string
    )
    process
    {
        $code = "`$ExecutionContext.InvokeCommand.ExpandString(`"$string`")"
        [scriptblock]::create($code) 
    }
}

Then:

&($MyString | xs)
&(xs $MyString)

The scriptblock is created at runtime, so $ExecutionContext is evaluated for each invocation.




回答2:


You could just make your own function for this

function xs{
    param(
    [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
    [string]$expand)
    $ExecutionContext.InvokeCommand.ExpandString($expand)
}

function MakeNoise{ 
   param($animal, $sound)
   $MyString = 'The $animal says $sound.'
   xs $MyString
   # $MyString | xs     <--  would also work
}

MakeNoise pig oink



回答3:


Look at the cmdlet New-Alias - https://technet.microsoft.com/en-us/library/hh849959.aspx



来源:https://stackoverflow.com/questions/28616274/is-there-a-way-to-create-aliases-for-object-methods-in-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!