如何从cmdlet中找到调用cmdlet的别名?

时间:2017-08-16 08:50:56

标签: powershell

您是否知道如何从cmdlet中查找调用cmdlet的别名?我试过这个,但它不起作用:

Function Write-Verbose {
    [CmdletBinding()]
    param($val)
    $CommandName = $Test.MyInvocation.line -replace '(^.+? ).*','$1'
    Write-host "$val Called with '$CommandName' alias"

}
Set-Alias WB Write-Verbose -Scope global
WB "goodbye"

1 个答案:

答案 0 :(得分:0)

您可以从$MyInvocation.InvocatioName

获取此信息
Function Write-Verbose {
    [CmdletBinding()]
    param($val)

    Write-host "$val Called with $($MyInvocation.InvocationName) alias"    
}
Set-Alias WB Write-Verbose -Scope global
WB "goodbye"

返回:

  

再见与WB别名一起调用