简体   繁体   English

如何在Powershell中为脚本方法创建别名?

[英]how to create alias name for scriptmethod in powershell?

We can create alias name for function. 我们可以为函数创建别名。

Is there a way to create alias name for Scriptmethod (created by Add-member)? 有没有办法为Scriptmethod创建别名(由Add-member创建)?

The scenario is as below. 情况如下。

$TestSuite | Add-Member -MemberType ScriptMethod TestId_6099 {

}

We are having test suite where a method name TestId_6099 is there. 我们有一个测试套件,其中有一个方法名称TestId_6099。 This method is doing for example: Check Disk limit exceed event. 例如,此方法正在执行:检查磁盘限制超过事件。

So the proper name could be 所以专有名称可能是

$TestSuite | Add-Member -MemberType ScriptMethod CheckDiskLimitExceedAlert {

}

Then there should be alias name for the scriptmethod should be as TestId_6099. 然后应该有别名,脚本方法应为TestId_6099。

There is an aliasproperty membertype but it is not working for scriptmethod. 有一个aliasproperty成员类型,但不适用于脚本方法。 How to create alias name for ScriptMethod? 如何为ScriptMethod创建别名?

This is a bit hacky, but you can have a self-referencing ScriptMethod : 这有点棘手,但是您可以使用自引用ScriptMethod

$TestProject = New-Object PsObject

$TestProject | Add-Member -MemberType ScriptMethod TestId_6099 {
    Write-Host "Hell Is For Heroes"
}

$TestProject | Add-Member -MemberType ScriptMethod GreatestBandEver {
    $this.TestId_6099()
}

$TestProject.GreatestBandEver()

Result: Hell Is For Heroes 结果: Hell Is For Heroes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM