打算从此启动vim
PS C:\temp> get-command vim | select name,source
Name Source
---- ------
vim.bat C:\Users\noam\AppData\Local\Microsoft\WindowsApps\vim.bat
我不小心进入了vm
而不是,随之vim
而来的是很多令人头疼的事情:
C:\temp>pwsh -noprofile
PowerShell 7.4.1
PS C:\temp> vm
Get-VM: 29-Mar-24 16:28:14 Get-VM You are not currently connected to any servers. Please connect first using a Connect cmdlet.
PS C:\temp> get-command vm
Get-Command: The term 'vm' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
PS C:\temp> get-alias vm
Get-Alias: This command cannot find a matching alias because an alias with the name 'vm' does not exist.
PS C:\temp> cmd /c where vm
INFO: Could not find files for the given pattern(s).
如何查找并删除看似从 VMware PowerCLI映射vm
到的配置?Get-VM
PS C:\temp> get-command get-vm | select Name,Source
Name Source
---- ------
Get-VM VMware.VimAutomation.Core
我一定是错过了一些愚蠢的事情。我本以为上面的调用where.exe
应该发现了与我的路径相关的任何内容。无论如何,设置后行为不会改变$env:PATH = $null
。
作为一个拼凑,我可以明确地重新定义vm
viaSet-Alias
到一些不存在的命令。但我想了解为什么在执行时看不到类似下面的内容vm
。我忽略了什么?
PS C:\temp> doesnotexist
doesnotexist: The term 'doesnotexist' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
无论是否VMware.VimAutomation.Core
显式导入模块,上述结果都可以重现。
这不是一个“别名问题”,它是 PowerShell 的“动词优先”命令命名方法的一部分。
Get-
当未指定动词时,PowerShell 会自动采用cmdlet 上的动词。childitem
您可以通过简单地编写一些命令(例如 等)来尝试一下location
。它会自动运行Get-ChildItem
并Get-Location
这也解释了您尝试找出此行为的不同 cmdlet 的行为:
Get-Command vm
- 由于 vm 不是命令,因此未找到它。Get-Alias vm
- 由于它也不是别名,因此您也无法在这里找到它。此行为不适用于用户定义的函数或控制台应用程序,这就是为什么您可以创建调用的函数
DoStuff
,但 PowerShell 不会自动执行此操作Get-DoStuff
您无法更改此行为,但
vm
如果您确实想这样做,则可以创建一个执行其他操作的函数。