Powershell 找不到接受参数“10"的位置参数.虽然甚至不使用位置参数 [英] Powershell A positional parameter cannot be found that accepts argument '10'. While not even using a positional parameter

查看:57
本文介绍了Powershell 找不到接受参数“10"的位置参数.虽然甚至不使用位置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,示例代码是人为设计的,仅用于说明问题.

Note that the example code is contrived and is meant for illustration of the problem only.

我想在 powershell 中创建一个函数来获取一些随机数,但我遇到了一个非常奇怪的问题,当我不使用参数的位置定义时,我仍然会收到一个错误.

I wanted to make a function in powershell to get some random numbers but i'm running into the very weird issue that when i dont use the position defining of the paramater i still get a error about it.

我的代码是:

function select-random {

  param(
    [Parameter(Position = 0, Mandatory)]
    [int]$min,
    [Parameter(Position = 1, Mandatory)]
    [int]$max
  )

  Get-Random $min $max

}

此脚本使用以下命令得到的错误:select-random 5 10

The error this script gets with the command: select-random 5 10

A positional parameter cannot be found that accepts argument '10'.

并使用命令:select-random -min 5 -max 10

A positional parameter cannot be found that accepts argument '10'.`

最奇怪的是ISE检测到标签菜单中手动定义的参数.

The weirdest thing is that the ISE detects the manualy defined paramater in the tab menu.

是我的语法错误还是 powershell 错误,但主要是我如何解决这个问题?

Is my syntax wrong or is it a powershell bug but mainly how do i fix this?

推荐答案

TheoJosefZ 在问题的评论中提供了关键的指示:

Theo and JosefZ have provided the crucial pointers in the comments on the question:

您正在尝试将 $min$max 参数位置传递给 Get-Random,但 Get-Random 仅支持 一个 位置参数,即用于 -Maximum 参数.

You're trying to pass both the $min and $max arguments positionally to Get-Random, yet Get-Random supports only one positional argument, namely for the -Maximum parameter.

因此,至少 $min 值必须作为 named 参数传递,即作为 -Minimum $min 使您的命令在语法上起作用.但是,为了对称性和可读性,最好也将 $max 作为命名参数传递:

Thus, at least the $min value must be passed as a named argument, i.e. as -Minimum $min for your command to work syntactically. However, for symmetry and readability it is better to pass $max as a named argument as well:

# Use *named* arguments.
Get-Random -Minimum $min -Maximum $max


如何确定命令的位置参数:

about_Command_Syntax 概念性帮助主题描述了 PowerShell 所谓的语法图中使用的符号.

The about_Command_Syntax conceptual help topic describes the notation used in PowerShell's so-called syntax diagrams.

要显示语法图(仅),请使用 Get-Command -Syntax(或通过 -?/use Get-Help,显示附加信息):

To display the syntax diagrams (only), use Get-Command -Syntax (or pass -? / use Get-Help, which shows additional information):

PS> & { Get-Command -Syntax $args[0] } Get-Random

Get-Random [[-Maximum] <Object>] [-SetSeed <int>] [-Minimum <Object>] [-Count <int>] [<CommonParameters>]

Get-Random [-InputObject] <Object[]> -Shuffle [-SetSeed <int>] [<CommonParameters>]

Get-Random [-InputObject] <Object[]> [-SetSeed <int>] [-Count <int>] [<CommonParameters>]

只有名称[...][1]包围的参数才是位置em> - 例如[-Maximum] - 并且,如果支持多个,它们将按照调用时必须传递的顺序列出.

Only parameters whose names alone are enclosed in [...][1] are positional - e.g. [-Maximum] - and, if multiple ones are supported, they are listed in the order in which they must be passed on invocation.

请注意,每个输出行代表一个单独的参数集(请参阅about_Parameter_Sets),但由于您传递的是最小值和最大值,因此此处仅对第一个感兴趣:

Note that each output line represents a separate parameter set (see about_Parameter_Sets), but since you're passing a minimum and maximum value, only the first one is of interest here:

如您所见,只有 -Maximum 在第一个参数集中是定位的,-Minimum 而所有其他参数都不是.

As you can see, only -Maximum is positional in the first parameter set, -Minimum and all the other parameters are not.

这是辅助函数Get-PositionalParameter,它可以更轻松地确定命令的位置参数:

Here's helper function Get-PositionalParameter, which makes it easier to determine a command's positional parameters:

Function Get-PositionalParameter {
<#
.SYNOPSIS
Outputs a given command's positional parameters, if any.
#>
  param(
    [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
    [string] $Name
  )
  Set-StrictMode -Version 1; $ErrorActionPreference = 'Stop'
  # Resolve the name to a cmdlet first, if necessary
  $cmd = (Get-Command $Name)
  if ($cmd.ResolvedCommand) { $cmd = $cmd.ResolvedCommand }

  $cmd.ParameterSets | ForEach-Object {
    if ($posParams = $_.Parameters.Where( { $_.Position -ge 0 })) {
      [pscustomobject] @{
        PositionalParams = $posParams.Name
        ParameterSet     = $_.Name
      }
    }
  }
}

应用于Get-Random:

PS> Get-PositionalParameter Get-Random

PositionalParams ParameterSet
---------------- ------------
Maximum          RandomNumberParameterSet
InputObject      ShuffleParameterSet
InputObject      RandomListItemParameterSet

请注意,参数集 names 不会出现在帮助主题中以及当您使用 Get-Command -Syntax 时,因为它们并不是真正用于 public 显示,但它们的名称通常足以说明其用途.

Note that the parameter set names do not surface in the help topics and when you use Get-Command -Syntax, because they aren't really meant for public display, but their names are usually descriptive enough to infer their purpose.

[1] 将此与包含在 [...] 中的参数规范作为一个整体进行对比 - 例如[-Minimum <Object>] - 它独立地表明参数作为一个整体是可选(不是强制性的,即传递一个参数不是必需的em>).

[1] Contrast this with the parameter specification as a whole being enclosed in [...] - e.g. [-Minimum <Object>] - which independently indicates that the parameter as a whole is optional (not mandatory, i.e. passing an argument is not required).

这篇关于Powershell 找不到接受参数“10"的位置参数.虽然甚至不使用位置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆