【问题标题】:How to get more information about powershell cmdlets?如何获取有关 powershell cmdlet 的更多信息?
【发布时间】:2016-01-29 18:23:32
【问题描述】:

我已经在谷歌上搜索了几个小时关于 Web-Administration 中的 Get-WebConfiguration cmdlet 无济于事。它的 MSDN 源没有解释 什么 -Metadata 参数接受作为输入。我在部署脚本中运行这个命令:

Set-WebConfiguration -PSPath IIS:\ -Filter /system.webServer/security/authentication/windowsAuthentication -Metadata overrideMode -value Allow

我正在开发一个库来读取这些值并在用户的环境不符合规范时提醒用户,所以我正在尝试使用:

Get-WebConfiguration -PSPath IIS:\ -Filter /system.webServer/security/authentication/windowsAuthentication -Metadata overrideMode

但我得到了错误:A positional parameter cannot be found that accepts argument 'overrideMode'.

我实际上只是使用这个精确的语法设置了这个精确的参数

如何在 powershell 中找到有关参数的更多信息?是否有一个 cmdlet 或者我只是使用 Get-WebConfiguration 错误?

【问题讨论】:

    标签: powershell scripting powershell-3.0


    【解决方案1】:

    您得到的具体错误是由于-Metadata 参数是一个开关 - 它不接受任何参数。

    当您指定-Metadata 开关时,返回的对象包含Metadata 属性。

    要获取overrideMode 的值,请执行以下操作:

    (Get-WebConfiguration -Filter "/node/filter" -Metadata).Metadata.overrideMode
    

    发现命令细节:

    (我以Test-Path 为例,但这适用于任何cmdlet)

    您始终可以从Get-Command -Syntax 获得有关 cmdlet 语法 的最基本信息:

    PS C:\> Get-Command Test-Path -Syntax
    
    Test-Path [-Path] <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType <TestPathType>]
    [-IsValid] [-Credential <pscredential>] [-UseTransaction] [-OlderThan <datetime>] [-NewerThan <datetime>]
    [<CommonParameters>]
    
    Test-Path -LiteralPath <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType
    <TestPathType>] [-IsValid] [-Credential <pscredential>] [-UseTransaction] [-OlderThan <datetime>] [-NewerThan
    <datetime>] [<CommonParameters>]
    

    Get-Command 返回一个CommandInfo 对象,您可以使用它来深入检查参数。

    Get-WebConfiguration -Metadata参数为例:

    PS C:\> (Get-Command Get-WebConfiguration).Parameters["Metadata"]
    
    Name            : Metadata
    ParameterType   : System.Management.Automation.SwitchParameter
    ParameterSets   : {[__AllParameterSets, System.Management.Automation.ParameterSetMetadata]}
    IsDynamic       : False
    Aliases         : {}
    Attributes      : {__AllParameterSets}
    SwitchParameter : True
    

    在这里我们可以看到-Metadata 实际上是一个开关(注意SwitchParameter : True 属性)

    要检索有关 cmdlet 的文档,您始终可以使用 Get-Help cmdlet 来获取有关特定 cmdlet 的类似 perldoc/manpage 的输出。由于文档只是文本,您可以通过管道将其发送到 more 以逐步完成它(再次,很像手册页或 perldoc):

    # Get a basic summary
    Get-Help Test-Path
    
    # Get more comprehensive summary
    Get-Help Test-Path -Detailed
    
    # Get the full documentation including examples
    Get-Help Test-Path -Full
    
    # Get just the examples
    Get-Help Test-Path -Examples
    
    # Get the help section about a specific parameter
    Get-Help Test-Path -Parameter Path
    

    【讨论】:

    • 这是一篇很棒的资源帖子,但是资源和 cmdlet 没有我想要发现的信息(元数据的允许值或我正在使用的 cmdlet 有什么问题)。您还有其他资源可以带领我走得更远吗?
    • @CBauer 如果您查看Get-Command Get-WebConfiguration -Syntax 的输出,您会看到MetaData 参数不接受任何参数——它是一个开关。这就是您收到错误的原因,PowerShell 无法将字符串“overrideMode”绑定到 MetaData 参数
    • 谢谢马蒂亚斯!当我阅读 switch 时,我没有意识到它是一个特定于 powershell 的概念,因为我用 c# 编程,而 switch 只是花哨的 if,所以我认为它接受了一个普通的字符串并想出了如何处理它。我的问题是我没有 RTFM。
    猜你喜欢
    • 2020-04-24
    • 1970-01-01
    • 2017-06-15
    • 2015-10-14
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多