删除脚本中的别名 [英] Remove alias in script
问题描述
我可以像这样删除别名:
删除项目别名:wget
然后尝试别名给出了预期的结果:
PS >获取wget :术语wget"不被识别为 cmdlet、函数、脚本文件或可运行的程序.
但是,如果我将相同的内容放入脚本中,
PS >猫 wget.ps1删除项目别名:wget获取
它给出了意想不到的结果
cmdlet Invoke-WebRequest 在命令管道位置 1提供以下参数的值:乌里:
以下似乎有效
If (Test-Path Alias:wget) {Remove-Item Alias:wget}If (Test-Path Alias:wget) {Remove-Item Alias:wget}获取
第一行删除了 Script
级别的别名,第二行删除了 Global
级别的别名.请注意,两者都需要进行测试,因为如果 Global
不存在,则不会创建 Script
.
这也是一种错误行为,因为它在没有点源的情况下修改了父级.
或者,您可以直接点源您的原件,这样就行了
<预><代码>..\wget.ps1I can remove an alias like so:
Remove-Item Alias:wget
Then trying the alias gives the expected result:
PS > wget
wget : The term 'wget' is not recognized as the name of a cmdlet, function,
script file, or operable program.
However, if I put the same into a script,
PS > cat wget.ps1
Remove-Item Alias:wget
wget
it gives unexpected result
cmdlet Invoke-WebRequest at command pipeline position 1
Supply values for the following parameters:
Uri:
The following seems to work
If (Test-Path Alias:wget) {Remove-Item Alias:wget}
If (Test-Path Alias:wget) {Remove-Item Alias:wget}
wget
The first line removes the Script
level alias, and the second removes the Global
level alias. Note that both need to be tested, because if the Global
doesn't exist the Script
isn't created.
Also this misbehaves in that it modifies the parent without being dot sourced.
Alternatively you can just dot source your original and that would work
. .\wget.ps1
这篇关于删除脚本中的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!