找回密码
 新建账号

Powershell 不能删除 curl wget 这些自带的命令别名

[复制链接]
php 发表于 2023/9/13 23:29 | 显示全部楼层 |阅读模式
Powershell 脚本文件不能删除 curl wget rmdir 这些自带的命令别名,假设有一个 ps1 文件有以下代码
Powershell can't remove built-in aliases like curl, wget, rmdir that have the AllScope option in script ....
  1. Remove-Item -Path Alias:curl -ErrorAction Ignore -Force
  2. Test-Path -Path Alias:curl
复制代码
在 Powershell 5.1 中运行以上脚本后,curl 没有被删除, Test-Path 仍然返回 True,测试 wget rmdir 有相同的效果。
运行下面句命令看看 curl 这个命令别名的详情
  1. Get-Alias curl | Select-Object *
复制代码
  1. PSPath              : Microsoft.PowerShell.Core\Alias::curl
  2. PSDrive             : Alias
  3. PSProvider          : Microsoft.PowerShell.Core\Alias
  4. PSIsContainer       : False
  5. HelpUri             : https://go.microsoft.com/fwlink/?LinkID=217035
  6. ResolvedCommandName : Invoke-WebRequest
  7. DisplayName         : curl -> Invoke-WebRequest
  8. ReferencedCommand   : Invoke-WebRequest
  9. ResolvedCommand     : Invoke-WebRequest
  10. Definition          : Invoke-WebRequest
  11. Options             : AllScope
  12. Description         :
  13. OutputType          : {}
  14. Name                : curl
  15. CommandType         : Alias
  16. Source              :
  17. Version             :
  18. Visibility          : Public
  19. ModuleName          :
  20. Module              :
  21. RemotingCapability  : PowerShell
  22. Parameters          : {[UseBasicParsing, System.Management.Automation.ParameterMetadata], [Uri, System.Management.Automation.Para
  23.                       meterMetadata], [WebSession, System.Management.Automation.ParameterMetadata], [SessionVariable, System.Mana
  24.                       gement.Automation.ParameterMetadata]...}
  25. ParameterSets       :
复制代码
发现这些无法删除的命名别名有一个共同点,就是都有这个属性值。
  1. Options : AllScope
复制代码
研究了 Powershell 官方文档,折腾出了解决方法,就是删除别名以后再删除一次。
  1. Remove-Item -Path Alias:curl -ErrorAction Ignore -Force
  2. Remove-Item -Path Alias:curl -ErrorAction Ignore -Force
复制代码
或者索性写个 while 循环一直删
  1. While(Test-Path -Path Alias:curl){
  2.         Remove-Item -Path Alias:curl -ErrorAction Ignore -Force
  3. }
  4. Test-Path -Path Alias:curl
复制代码
如果使用 Powershell 6.0+,Remove-Item 可以一次性删除 rmdir 别名,curl 和 wget 在 Powershell 6.0+ 已经被删除了,
  1. Get-alias rmdir | Select Options
复制代码
运行以上命令,发现 Options 为 None,原来 AllScope 已经没有了。
Powershell 6.0+ 也可以使用 Remove-Alias 这个专门删除别名的命令来删除别名,很好用。
参考链接 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes

手机版|轻松E站

GMT+8, 2024/4/28 12:39

快速回复 返回顶部 返回列表