Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell输入管道问题_Powershell_Piping - Fatal编程技术网

Powershell输入管道问题

Powershell输入管道问题,powershell,piping,Powershell,Piping,我无法运行此简单的powershell程序 [int]$st1 = $input[0] [int]$st2 = $input[1] [int]$st3 = $input[2] [int]$pm = $input[3] [int]$cm = $input[4] $MedMarks = $st1 + $st2 + $st3 - ($pm + $cm) Write-Host "Med Marks $MedMarks" 我正试图用这样的输入管道运行它 120、130、90、45、30 |.\sam

我无法运行此简单的powershell程序

[int]$st1 = $input[0]
[int]$st2 = $input[1]
[int]$st3 = $input[2]
[int]$pm = $input[3]
[int]$cm = $input[4]

$MedMarks = $st1 + $st2 + $st3 - ($pm + $cm)
Write-Host "Med Marks  $MedMarks"
我正试图用这样的输入管道运行它

120、130、90、45、30 |.\sample_program.ps1

我一直在犯这个错误

Cannot convert the "System.Collections.ArrayList+ArrayListEnumeratorSimple" value of type
"System.Collections.ArrayList+ArrayListEnumeratorSimple" to type "System.Int32".

你不能像那样索引到
$input

您可以使用每个对象的

$st1,$st2,$st3,$pm,$cm = $input |ForEach-Object { $_ -as [int] }
或者(最好)使用命名参数:

param(
    [int]$st1,
    [int]$st2,
    [int]$st3,
    [int]$pm,
    [int]$cm
)

$MedMarks = $st1 + $st2 + $st3 - ($pm + $cm)
Write-Host "Med Marks  $MedMarks"

你不能像那样索引到
$input

您可以使用每个对象的

$st1,$st2,$st3,$pm,$cm = $input |ForEach-Object { $_ -as [int] }
或者(最好)使用命名参数:

param(
    [int]$st1,
    [int]$st2,
    [int]$st3,
    [int]$pm,
    [int]$cm
)

$MedMarks = $st1 + $st2 + $st3 - ($pm + $cm)
Write-Host "Med Marks  $MedMarks"

如果您像这样检查
$input

PS>函数f{$input.GetType().FullName}f
System.Collections.ArrayList+ArrayListEnumeratorSimple
然后您可以注意到,
$input
不是一个集合,而是一个枚举器。因此,对于裸
$input
,您没有索引器的随机访问权限。如果确实要为
$input
编制索引,则需要将其内容复制到数组或其他集合中:

$InputArray = @( $input )
然后,您可以像往常一样索引
$InputArray

[int]$st1 = $InputArray[0]
[int]$st2 = $InputArray[1]
[int]$st3 = $InputArray[2]
[int]$pm = $InputArray[3]
[int]$cm = $InputArray[4]

如果您像这样检查
$input

PS>函数f{$input.GetType().FullName}f
System.Collections.ArrayList+ArrayListEnumeratorSimple
然后您可以注意到,
$input
不是一个集合,而是一个枚举器。因此,对于裸
$input
,您没有索引器的随机访问权限。如果确实要为
$input
编制索引,则需要将其内容复制到数组或其他集合中:

$InputArray = @( $input )
然后,您可以像往常一样索引
$InputArray

[int]$st1 = $InputArray[0]
[int]$st2 = $InputArray[1]
[int]$st3 = $InputArray[2]
[int]$pm = $InputArray[3]
[int]$cm = $InputArray[4]

$InputArray=@($input);[int]$st1=$InputArray[0]@PetSerAl。。。这是正确的,但为什么在评论中?:)请将其移动到“回答”以便我可以将其标记为正确:)
$InputArray=@($input);[int]$st1=$InputArray[0]@PetSerAl。。。这是正确的,但为什么在评论中?:)请把它移到回答处,这样我就可以把它标记为正确:)这一个有效,但我会选择@Petsall的回答,这一个有点整洁。这一个有效,但我会选择@Petsall的回答,它有点整洁