Powershell将值加在一起

Oda*_*qed 9 arrays powershell add powershell-2.0

这是我的代码:

clear-host

function isNumeric ($x) {
try {
    0 + $x | Out-Null
    return $true
} catch {
    return $false
}
}

function output-file ($ave, $high, $low, $date)
{
write-output "Programer: Oday Sawaqed"
write-output "Class: CIS 124"
write-output "PowerShell Assignmnent"
write-output ""
Write-output ""
write-output " Current Date                    Average               Highest                        Lowest"
write-output " $date                $ave                   $high                 $low "
}


$array = @()
$hold
$n = 1

do {
$hold = read-host "number $n"
if (isNumeric $hold -eq $true){
if (999 -ne $hold) {
$array += $hold
$n = $n + 1
}
else
{
clear-host
write-host "Thank you."
write-host "The numbers you entered are:" $array
write-host "Please select a file name to save the output:"
$fileName = Read-host

$date = get-date -format "dddd, MMMM d, yyyy"
$array = $array | Sort-Object 
$ave = 
$high = $array | Select-Object -last 1
$low = $array | Select-Object -first 1

output-file $ave $high $low $date | Out-File c:\$fileName.txt
}
}
else {
write-host "Please enter a numeric value"
}
}
while (999 -ne $hold)
Run Code Online (Sandbox Code Playgroud)

现在代码工作正常,我只是无法弄清楚如何将数组中的值相加以计算平均值.有人可以帮助我或给我一个提示!我知道我需要将值加在一起然后除以$ n,我只是不知道如何添加值.

JPB*_*anc 26

要计算平均值,您可以使用Measure-ObjectCmdlet:

($array | Measure-Object -Average).average
Run Code Online (Sandbox Code Playgroud)

  • +1,我一直在寻找如何对数组求和.而且你提醒我,measure-object也可以完成许多数学类型的任务. (2认同)