【问题标题】:Why aren't I getting any definitions when I type "get-command -type cmdlet"?为什么我在键入“get-command -type cmdlet”时没有得到任何定义?
【发布时间】:2020-09-20 00:17:58
【问题描述】:

这是我学习powershell的第一天,如果这是一个愚蠢的问题,请原谅我......

但我正在观看的教学视频显示教师输入“get-command -type cmdlet”以演示如何获取所有可用 cmdlet 及其定义的列表。我正在获取 cmdlet,但我的 powershell 没有列出定义。我附上屏幕截图,向您展示他拥有的和我拥有的。我必须进行哪些更改才能解决此问题?

【问题讨论】:

  • 感谢您的回答。他们对这个新手很有帮助! :)

标签: powershell definition cmdlet


【解决方案1】:

定义属性可能不会出现,因为 PowerShell 正在对其进行格式化以供您显示。该定义对于您的控制台显示来说可能太长,因此它会以“...”结尾。

使用以下内容查看所有可用的成员属性。基于此,您可以选择要查看的属性。

Get-Command -Type Cmdlet | Format-List -Property Name, Definition

如果您想查看 PowerShell 代码,这可能会很有趣。

Get-Command -Type Function |
    Where-Object { $_.Definition.Length -gt 0 } |
    Select-Object -Property Name,Definition |
    Format-List

查看所有可用的属性成员。

PS H:\> Get-Command -Type Cmdlet | Get-Member

   TypeName: System.Management.Automation.CmdletInfo

Name                MemberType     Definition
----                ----------     ----------
Equals              Method         bool Equals(System.Object obj)
GetHashCode         Method         int GetHashCode()
GetType             Method         type GetType()
ResolveParameter    Method         System.Management.Automation.ParameterMetadata ResolveParameter(string name)
ToString            Method         string ToString()
CommandType         Property       System.Management.Automation.CommandTypes CommandType {get;}
DefaultParameterSet Property       string DefaultParameterSet {get;}
Definition          Property       string Definition {get;}
HelpFile            Property       string HelpFile {get;}
ImplementingType    Property       type ImplementingType {get;}
Module              Property       psmoduleinfo Module {get;}
ModuleName          Property       string ModuleName {get;}
Name                Property       string Name {get;}
Noun                Property       string Noun {get;}
Options             Property       System.Management.Automation.ScopedItemOptions Options {get;set;}
OutputType          Property       System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.PSTypeName] OutputType {get;}
Parameters          Property       System.Collections.Generic.Dictionary[string,System.Management.Automation.ParameterMetadata] Parameters {get;}
ParameterSets       Property       System.Collections.ObjectModel.ReadOnlyCollection[System.Management.Automation.CommandParameterSetInfo] ParameterSets...
PSSnapIn            Property       System.Management.Automation.PSSnapInInfo PSSnapIn {get;}
RemotingCapability  Property       System.Management.Automation.RemotingCapability RemotingCapability {get;}
Source              Property       string Source {get;}
Verb                Property       string Verb {get;}
Version             Property       version Version {get;}
Visibility          Property       System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}
DLL                 ScriptProperty System.Object DLL {get=$this.ImplementingType.Assembly.Location;}
HelpUri             ScriptProperty System.Object HelpUri {get=$oldProgressPreference = $ProgressPreference...

【讨论】:

  • 我确定这不是格式问题。我正在使用超宽显示器,还有大量空间可以显示更多内容。除非在powershell中有配置,所以无论如何它只显示某些信息。会不会是这个问题?
【解决方案2】:

Cmdlet 是用 C# 编写的。它们是经过编译的,如果您要查看源代码,通常看不到定义。

【讨论】:

  • 我不会忽视任何事情。这是我在学习 powershell 的前 5 分钟内出现的一个问题。直到你提到它之前,我什至从未听说过“获得帮助”。
  • @Tommy 我不是在评论你的问题,而是在评论@js2010 的回答。
【解决方案3】:

命令get-command -type cmdlet 不显示定义。也许您想查看get-command 的定义? :)

要查看定义,您可以使用以下两种方式之一,如果命令支持,您只需在命令后添加-? 即可获得帮助。示例:

get-command -?

-? 相当于 bash 中的 --h--help。某些命令不支持 taht,因为这将是命令中的参数,例如 if 命令。对于这些类型的命令,您可以使用:

get-help if

它会向您显示对if 的帮助,或者向您显示名称相似的东西,您可以获得帮助。这方面的一个例子是break 命令。 break 不支持 -? 方法,因此您必须使用 get-help break 但它也不会出现在那里。相反,您会得到以下输出:

Name                              Category  Module                    Synopsis
----                              --------  ------                    --------
Disable-PSBreakpoint              Cmdlet    Microsoft.PowerShell.U... Disables the breakpoints in the current console.
Enable-PSBreakpoint               Cmdlet    Microsoft.PowerShell.U... Enables the breakpoints in the current console.
Get-PSBreakpoint                  Cmdlet    Microsoft.PowerShell.U... Gets the breakpoints that are set in the curre...
Remove-PSBreakpoint               Cmdlet    Microsoft.PowerShell.U... Deletes breakpoints from the current console.
Set-PSBreakpoint                  Cmdlet    Microsoft.PowerShell.U... Sets a breakpoint on a line, command, or varia...
about_Break                       HelpFile                            Describes a statement you can use to immediate...

您可以在别名about_Break 下看到break 帮助文件。因此,如果我输入get-help about_Break,它将向我显示帮助。

您还可以使用 get-help 来查找所有 cmdlets,例如 get-commandget-help -type cmdlet,但如果您想检查一下,我建议您查看 get-help 的帮助:)。

更多信息(字面意思):您可以使用以下四个命令之一找到有关某些 cmdlet 的更多信息:

To see the examples, type: "get-help <command name> -examples".
For more information, type: "get-help <command name> -detailed".
For technical information, type: "get-help <command name> -full".
For online help, type: "get-help <command name> -online"

例如(没有双关语),您可以使用以下方式在 get-help 上找到帮助:

get-help Get-Help -examples
get-help Get-Help -detailed
get-help Get-Help -full
get-help Get-Help -online

并且交换机将具有上述标准(示例、额外信息、技术信息和在线帮助,因为有更多关于在线事物的文档)

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    相关资源
    最近更新 更多