脚本中的 New-Alias 失败,但可以交互

脚本中的 New-Alias 失败,但可以交互

过去几年我一直在使用这个简单的小脚本:

#! PowerShell
# Create utility aliases for working with fax account management cmdlets
# account searches
new-alias -name gfad                        -value '.\Get-FaxAcctByDNIS.ps1'            
new-alias -name gfap                        -value '.\Get-FaxAcctByPhone.ps1'           
new-alias -name gfasql                      -value '.\Get-FaxAcctBySQL.ps1'             
new-alias -name gfasso                      -value '.\Get-FaxAcctBySSO.ps1'             
new-alias -name Get-AcctByDNIS              -value '.\Get-FaxAcctByDNIS.ps1'            
new-alias -name Get-AcctByPhone             -value '.\Get-FaxAcctByPhone.ps1'           
new-alias -name Get-AcctBySQL               -value '.\Get-FaxAcctBySQL.ps1'             
new-alias -name Get-AcctBySSO               -value '.\Get-FaxAcctBySSO.ps1'             
# route searches
new-alias -name gfra                        -value '.\Get-FaxRouteByDestination.ps1'    
new-alias -name gfrd                        -value '.\Get-FaxRouteByDNIS.ps1'           
new-alias -name gfrsql                      -value '.\Get-FaxRouteBySQL.ps1'            
new-alias -name Get-RouteBySQL              -value '.\Get-FaxRouteBySQL.ps1'            
new-alias -name Get-RouteByDestination      -value '.\Get-FaxRouteByDestination.ps1'    
new-alias -name Get-RouteByDNIS             -value '.\Get-FaxRouteByDNIS.ps1'           
new-alias -name Get-RouteBy                 -value '.\Get-FaxRouteBy.ps1'               
new-alias -name Get-RouteBySQL              -value '.\Get-FaxRouteBy.ps1'               
# user searches
new-alias -name gfun                        -value '.\Get-UserByName.ps1'               
new-alias -name gfusso                      -value '.\Get-UserBySSO.ps1'                
new-alias -name gfusql                      -value '.\Get-UserBySQL.ps1'                
new-alias -name Get-FaxUserByName           -value '.\Get-UserByName.ps1'               
new-alias -name Get-FaxUserBySSO            -value '.\Get-UserBySSO.ps1'                
new-alias -name Get-FaxUserBySQL            -value '.\Get-UserBySQL.ps1'                
# miscellaneous
#EoF#

很难想象这样会出什么问题(除非你运行两次并得到“已定义”错误),对吧?而且它总是按预期工作。

今天早上我突然调用它,然后尝试使用其中一个别名,结果得到了这个:

12:07:19|32|~\docs\rts\geafax# .\xyzzy.ps1 new-alias : The alias is not allowed, because an alias with the name 'Get-RouteBySQL' already exists. At C:\Users\200018252\docs\rts\geafax\xyzzy.ps1:23 char:1
+ new-alias -name Get-RouteBySQL              -value '.\Get-FaxRouteBy. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: (Get-RouteBySQL:String) [New-Alias], SessionStateException
    + FullyQualifiedErrorId : AliasAlreadyExists,Microsoft.PowerShell.Commands.NewAliasCommand​​​

但是...别名尚未定义:

12:07:29|33|~\docs\rts\geafax# gal get-routebysql
gal : This command cannot find a matching alias because an alias with the name 'get-routebysql' does not exist.
At line:1 char:1
+ gal get-routebysql
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (get-routebysql:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand
12:07:56|34|~\docs\rts\geafax# gci alias:\get-routebysql
gci : Cannot find path 'get-routebysql' because it does not exist.
At line:1 char:1
+ gci alias:\get-routebysql
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (get-routebysql:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

​​​脚本中定义的任何其他别名也都没有创建。这很奇怪,直到它一路运行到 Get-RouteBySQL 后才抛出错误,然后才发出抱怨!

更加神秘的是,我可以直接从 CLI 提示符执行脚本中的所有行,并且它们都成功了!

我没有对脚本或 Powershell 环境做任何更改。

我完全不知所措。有人有什么想法吗?

答案1

运行脚本点源。 读关于操作员帮助主题:

.点源运算符

在当前范围内运行脚本,以便脚本创建的任何函数、别名和变量都添加到当前范围。

⊕ 注意

点源运算符后面跟着一个空格。使用空格来区分点和.表示当前目录的点 ( ) 符号。

例子

D:\PShell> powershell -NoProfile -NoLogo
PS D:\PShell> Get-Alias -Name foo*
PS D:\PShell> Get-Content -Path D:\PShell\SU\1149083.ps1
new-alias -name foobar  -value 'Get-Culture'
new-alias -name foo-bar -value 'Get-Location'
new-alias -name foo-bar -value 'Get-Random'   -Force
PS D:\PShell> D:\PShell\SU\1149083.ps1
PS D:\PShell> Get-Alias -Name foo*
PS D:\PShell> . D:\PShell\SU\1149083.ps1  # dot-sourced
PS D:\PShell> Get-Alias -Name foo*
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           foobar -> Get-Culture
Alias           foo-bar
PS D:\PShell> foo-bar -Minimum 0 -Maximum 99
41

获取别名关于别名帮助主题:

从 Windows PowerShell 3.0 开始,Get-Alias以某种格式显示非连字符别名,
<alias> -> <definition>以便更轻松地找到所需信息。...
基于
箭头的别名格式不适用于包含连字符的别名。这些可能是 cmdlet 和函数的首选替代名称,而不是典型的缩写或昵称,作者可能不希望它们太明显。

相关内容