Powershell 发现命令 2


从用户的角度来看,在Powershell控制台上输入一条命令,然后直接回车执行,是一件简单的事情,事实上Powershell在后台做了很多事情,其中第一步,就是查看用户输入的命令是否可用,这个步骤也被称作自动化发现命令。使用Get-Command 命令可以查看当前作用域支持的所有命令。如果你想查看关于 LS 命令的信息,请把它传递给Get-Command。

PS C:> Get-command LS

CommandType Name Definition
----------- ---- ----------
Alias       ls   Get-ChildItem

如果你想查看更加详细的信息可以使用:

PS C:>Get-Command ls | fl *
HelpUri             : http://go.microsoft.com/fwlink/?LinkID=113308
ResolvedCommandName : Get-ChildItem
ReferencedCommand   : Get-ChildItem
ResolvedCommand     : Get-ChildItem
Definition          : Get-ChildItem
Options             : AllScope
Description         :
OutputType          : {System.IO.FileInfo, System.IO.DirectoryInfo, System.String}
Name                : ls
CommandType         : Alias
Visibility          : Public
ModuleName          :
Module              :
Parameters          : {[Path, System.Management.Automation.ParameterMetadata], [Literal
                       [Include, System.Management.Automation.ParameterMetadata]...}
ParameterSets

如果你想查看命令IPConfig的命令信息,可以使用:

PS C:> get-command ipconfig

CommandType Name         Definition
----------- ----         ----------
Application ipconfig.exe C:windowsSYSTEM32ipconfig.exe

事实上,Get-Command 返回的是一个对象CommandInfo,ApplicationInfo,FunctionInfo,或者CmdletInfo;
PS C:> $info = Get-Command ping
PS C:> $info.GetType().fullname
System.Management.Automation.ApplicationInfo
PS C:> $info = Get-Command ls
PS C:> $info.GetType().fullname
System.Management.Automation.AliasInfo
PS C:> $info = Get-Command Get-Command
PS C:> $info.GetType().fullname
System.Management.Automation.CmdletInfo
PS C:> $info=Get-Command more | select -First 1
PS C:> $info.GetType().fullname
System.Management.Automation.FunctionInfo

如果一条命令可能指向两个实体,get-command也会返回,例如more。

PS C:> Get-Command more

CommandType Name     Definition
----------- ----     ----------
Function    more     param([string[]]$paths)...
Application more.com C:windowsSYSTEM32more.com

这两条命令,前者是Powershell的自定义函数,后者是扩展的Application命令。细心的读者可能会提问,这两个会不会发生冲突。当然不会,默认会调用第一个,是不是仅仅因为它排在第一个,不是,而是在Powershell中有一个机制,就是函数永远处在最高的优先级。不信,看看下面的例子,通过函数可以重写ipconfig ,一旦删除该函数,原始的ipconfig才会重新登上历史的舞台:

PS C:> function Get-Command () {}
PS C:> Get-Command
PS C:> del Function:Get-Command
PS C:> function ipconfig(){}
PS C:> ipconfig
PS C:> del Function:ipconfig
PS C:> ipconfnig
PS C:> ipconfig.exe

Windows IP 配置

无线局域网适配器 无线网络连接 3:

   媒体状态  . . . . . . . . . . . . : 媒体已断开
   连接特定的 DNS 后缀 . . . . . . . :

无线局域网适配器 无线网络连接 2:

   媒体状态  . . . . . . . . . . . . : 媒体已断开
   连接特定的 DNS 后缀 . . . . . . . :

无线局域网适配器 无线网络连接:

   连接特定的 DNS 后缀 . . . . . . . :
   本地链接 IPv6 地址. . . . . . . . : fe80::9c4:7e81:82a2:1a3e%15
   IPv4 地址 . . . . . . . . . . . . : 192.168.1.100
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   默认网关. . . . . . . . . . . . . : 192.168.1.1
本文链接: https://www.pstips.net/powershell-command-discovery.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

回复 Mooser Lee 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

2 条评论 “Powershell 发现命令