首页 > 解决方案 > Rum Get-MrPiplelineInput 但得到 5 个相同的结果

问题描述

我跑Get-MrPipelineInput -name get-help但得到:

ParameterName ParameterType ValueFromPipeline ValueFromPipelineByPropertyName
------------- ------------- ----------------- -------------------------------
Name          System.String             False                            True
Name          System.String             False                            True
Name          System.String             False                            True
Name          System.String             False                            True
Name          System.String             False                            True
Name          System.String             False                            True

获得 4 个额外的 Name 相同结果。

我使用 Get-MrPipelienInput 有什么问题吗?

标签: powershell

解决方案


我不认为你在这里做错了什么。我认为问题实际上出在代码中。Get-Help可能不同于大多数/所有其他 PS Cmdlet/Function 的结构,因此不符合 Mike Robbins(此工具的作者)所期望的结构。

在闲逛了几分钟后,我可以看到这里发生了什么。 Get-Help有 6 个参数集。在每一个中,唯一允许来自管道的值的参数是Name. 可以对Mike 的 Miscellaneous PowerShell Tools Code进行简单的改进,使其成为更好的工具。


function Get-MrPipelineInput {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$Name,
        
        [System.Management.Automation.WhereOperatorSelectionMode]$Option = 'Default',
        
        [ValidateRange(1, 2147483647)]
        [int]$Records = 2147483647
    )
    $FunctionOutput = [System.Collections.ArrayList]::new()
    (Get-Command -Name $Name).ParameterSets.ForEach{
        $ParameterSet = $PSItem
        $PSItem.Parameters.Where( {
                $_.ValueFromPipeline -or $_.ValueFromPipelineByPropertyName
            }, $Option, $Records).ForEach( {
               $null = $FunctionOutput.Add(
                    [pscustomobject]@{
                        ParameterSetName                   = $ParameterSet.Name
                        ParameterName                   = $_.Name
                        ParameterType                   = $_.ParameterType
                        ValueFromPipeline               = $_.ValueFromPipeline
                        ValueFromPipelineByPropertyName = $_.ValueFromPipelineByPropertyName
                    })
            })
    }
    $FunctionOutput
}

运行会产生一个更容易理解的输出:

PS > (Get-MrPipelineInput -Name Get-Help | ft)


ParameterSetName ParameterName ParameterType ValueFromPipeline ValueFromPipelineByPropertyName
---------------- ------------- ------------- ----------------- -------------------------------
AllUsersView     Name          System.String             False                            True
DetailedView     Name          System.String             False                            True
Examples         Name          System.String             False                            True
Parameters       Name          System.String             False                            True
Online           Name          System.String             False                            True
ShowWindow       Name          System.String             False                            True

我希望这可以帮到你!


推荐阅读