提问人:Jeremy Frank 提问时间:9/21/2023 最后编辑:Jeremy Frank 更新时间:9/21/2023 访问量:61
创建 PowerShell 函数以计算平均费率
Creating a PowerShell Function to calculate average rates
问:
我正在做一个项目,我被难住了,所以我向你们寻求一点帮助。
我需要做的是创建一个具有所需文件名参数的函数,我已经这样做了,然后该函数将计算并打印出文件中所有条目的平均住宅费率。以下函数需要执行以下任务:
1. Import the CSV file
2. Create a loop to read and add the rates
3. calculate the average of the rates
4. write the output to the console
我目前的函数和脚本是:
function Get-Rates {
param (
#This Paramater is mandatory
[Parameter(mandatory=$true)]
[string] $Filepath
)
#This script will import the Filepath of a csv file, select residential_rate and send it to variable RR
Import-Csv -Path $Filepath | Select-Object residential_rate -OutVariable RR
#Here i want it to loop and calcuate the average reseidential_rate for all entries in the csv file
for ($RR = 0; $RR -lt $array.Count; $RR++) {
<# Action that will repeat until the condition is met #>
}
我正在尝试创建循环,但我不确定它应该是 for 循环、do 循环还是 foreach。
答: 暂无答案
评论
Measure-Object
$data = Import-Csv -Path $fliename; $data | measure-object -Property "residential_rate" -Average | select-object -ExpandProperty "Average"
$array