简体   繁体   中英

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)?

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. 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.

There is an aliasproperty membertype but it is not working for scriptmethod. How to create alias name for ScriptMethod?

This is a bit hacky, but you can have a self-referencing 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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