Powershell - 用户输入的数组范围

发布于 2024-12-07 00:17:55 字数 528 浏览 1 评论 0原文

获取数组范围的用户输入的最简单方法是什么。

例如:

function MyArrayOfMachines {
[CmdletBinding()]
param(
    [parameter(Mandatory=$true)][string]$Machine
    # What should I assign the $Range variable as?
    )
 # Hardcoded range. User should be able to enter the range
 $Range = 2..5
 for ($i=0; $i -lt $array.length; $i +=1)
 {
   $result = $array[$i]
   $output = $machine+$result
   $output
 }
}

上面的函数应该将输入作为机器的名称和数组范围。现在我已经对数组范围进行了硬编码。当我在用户提示中将 $Range 指定为 [Array]$Range 时,会提示 $Range[0] 等。但我希望用户输入范围。

What would be the easiest way to get user input for an array range.

For example:

function MyArrayOfMachines {
[CmdletBinding()]
param(
    [parameter(Mandatory=$true)][string]$Machine
    # What should I assign the $Range variable as?
    )
 # Hardcoded range. User should be able to enter the range
 $Range = 2..5
 for ($i=0; $i -lt $array.length; $i +=1)
 {
   $result = $array[$i]
   $output = $machine+$result
   $output
 }
}

The above function should take the input as the name of the machine and the array range. For now I have the array range hardcoded. When I assign $Range as [Array]$Range in the user prompt, there is a prompt for $Range[0] etc etc. But I would like the user the enter the range.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回忆凄美了谁 2024-12-14 00:17:55

这行不通吗?除非我误解了你的问题...

function test($range){

$range

}

test -range (1..5)

Doesn't this work? Unless I misunderstood your question...

function test($range){

$range

}

test -range (1..5)
咿呀咿呀哟 2024-12-14 00:17:55

您还可以接受范围作为字符串并自己解析它:

   function Test
    {
    param($range)

    if($range -is [String])
        {
        [int]$start, [int]$end = $range.split('.', [StringSplitOptions]::RemoveEmptyEntries)
        $start..$end
        }
    else
        {
        $range
        }
    }

if / else 的原因是用户传递实际范围(如 manojlds 答案),而不是要解析的字符串(如 1..5 )。这意味着您不能强键入参数。

You can also accept the range as a string and parse it yourself:

   function Test
    {
    param($range)

    if($range -is [String])
        {
        [int]$start, [int]$end = $range.split('.', [StringSplitOptions]::RemoveEmptyEntries)
        $start..$end
        }
    else
        {
        $range
        }
    }

The reason for the if / else is for cases where the user passes an actual range, as in manojlds answer, rather than a string to be parsed (like 1..5). This means you can't strongly type the param though.

不…忘初心 2024-12-14 00:17:55

使其成为两个参数:

function test{
    param ( [int]$st,
            [int]$end)

    $Range = $st..$end
    $Range
}

test 1 5

如果它们输入范围的开始和结束,您可以使用它在函数中动态创建它。

编辑

要获取字符串的范围,请尝试:

function test{
    param ($Range)

    $NewRange = $Range.substring(0,($Range.indexof('.')))..$Range.substring(($Range.lastindexof('.') + 1))
    $NewRange
}

test 1..5

Make it two parameters:

function test{
    param ( [int]$st,
            [int]$end)

    $Range = $st..$end
    $Range
}

test 1 5

If they input the start and end of the range you can use that to create it dynamically in the function.

EDIT:

To get the range from a string, try:

function test{
    param ($Range)

    $NewRange = $Range.substring(0,($Range.indexof('.')))..$Range.substring(($Range.lastindexof('.') + 1))
    $NewRange
}

test 1..5
楠木可依 2024-12-14 00:17:55

我同意@manojlds,范围应该作为数组传入。解析字符串限制了用户输入内容的可能性。通过使用[int[]],您可以强制用户指定整数数组。这还允许用户指定一个断开的范围,例如 ((2..4)+(6..12)) ,这在解析字符串时更难允许。

在您的示例中,我不确定 $array 来自何处,并且您只需要一行即可返回计算机器名称。

function MyArrayOfMachines {
    param(
    [parameter(mandatory=$true)]
    [string] $machine,

    [parameter(mandatory=$true)]
    [int[]] $range
    )

    foreach($n in $range) {
        $machine+$n
    }
}

您可以创建单个计算机名称、

MyArrayOfMachines Laptop 1

一系列计算机

MyArrayOfMachines Workstation (2..10)

或不连续的计算机阵列

MyArrayOfMachines Server ((2..3)+(5..9))

I agree with @manojlds, the range should be passed in as an array. Parsing a string limits the possibilities of what a user could enter. By using [int[]] you can force the user to specify an array of integers. This would also allow a user to specify a broken range such as ((2..4)+(6..12)) which is harder to allow for when parsing strings.

In your example I'm not sure where $array is coming from, and you only need one line to return a computed machine name.

function MyArrayOfMachines {
    param(
    [parameter(mandatory=$true)]
    [string] $machine,

    [parameter(mandatory=$true)]
    [int[]] $range
    )

    foreach($n in $range) {
        $machine+$n
    }
}

You could create a single machine name,

MyArrayOfMachines Laptop 1

a range of machines,

MyArrayOfMachines Workstation (2..10)

or a non-consecutive array of machines

MyArrayOfMachines Server ((2..3)+(5..9))
ぃ弥猫深巷。 2024-12-14 00:17:55

您可以只传递一个字符串并对其进行评估:

function Test([string]$range) {
  if ($Range -match '^\d+\.\.\d+

进行一些最小的验证以确保用户无法传递任意代码。

) { $RangeArray = Invoke-Expression $Range } else { $RangeArray = 1..5 } }

进行一些最小的验证以确保用户无法传递任意代码。

You could just pass a string and evaluate it:

function Test([string]$range) {
  if ($Range -match '^\d+\.\.\d+

Some minimal validation is done to ensure that the user cannot pass arbitrary code.

) { $RangeArray = Invoke-Expression $Range } else { $RangeArray = 1..5 } }

Some minimal validation is done to ensure that the user cannot pass arbitrary code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文