我需要在 powershell 中划分两列 CSV 文件并将输出添加到对象
I need to divided two columns of a CSV file in powershell and add the output to an object
我需要在 powershell 中将 CSV 文件分成两列,并将输出添加到一个对象中
$ImportCSV = Import-Csv "#CSV file to read"
$CSVArray = @($ImportCSV)
$CSVArray | ForEachObject {
$battingAVG.result = [math]::Round($_.Hits / $_.AtBats)
$battingAVG
}
$battingObject = New-Object System.Object
$battingObject | Add-Member -Type NoteProperty -Name "Name" -Value $CVSArray.Name
$battingObject | Add-Member -Type NoteProperty -Note "BattingAverage" -Value $battingAVG
这是经过修改的代码审查。我已在评论中注明:
# Import-CSV already returns an array of objects.
# No need to wrap in another array
#ImportCSV = $CSVArray = Import-Csv "#CSV file to read"
# $CSVArray = @($ImportCSV)
$CSVArray = Import-Csv "#CSV file to read"
# Mispelling of 'ForEach-Object' below?
$CSVArray | ForEach-Object {
# $battingAvg is undeclared at this point, so how does it have a 'result' property?
# Maybe there's more to this variable that you haven't shared
# $battingAVG.result = [math]::Round($_.Hits / $_.AtBats)
$battingAVG = [math]::Round($_.Hits / $_.AtBats)
$battingObject = New-Object System.Object
# Now that you're inside the loop, get the Name property of the current record $_
$battingObject | Add-Member -Type NoteProperty -Name "Name" -Value $_.Name
$battingObject | Add-Member -Type NoteProperty -Note "BattingAverage" -Value $battingAVG
}
# You want to create a new object for each row of the data, so this code
# belongs inside your loop
# $battingObject = New-Object System.Object
# $battingObject | Add-Member -Type NoteProperty -Name "Name" -Value $CVSArray.Name
# $battingObject | Add-Member -Type NoteProperty -Note "BattingAverage" -Value $battingAVG
您还应该对 Calculated Properties 做一些研究并做一些实验;我相当确定所有这些代码都可以替换为:
Import-Csv "#CSV file to read" | Select-Object -Property Name, @{Name='BattingAverage';Expression={[math]::Round($_.Hits / $_.AtBats)}}
我需要在 powershell 中将 CSV 文件分成两列,并将输出添加到一个对象中
$ImportCSV = Import-Csv "#CSV file to read"
$CSVArray = @($ImportCSV)
$CSVArray | ForEachObject {
$battingAVG.result = [math]::Round($_.Hits / $_.AtBats)
$battingAVG
}
$battingObject = New-Object System.Object
$battingObject | Add-Member -Type NoteProperty -Name "Name" -Value $CVSArray.Name
$battingObject | Add-Member -Type NoteProperty -Note "BattingAverage" -Value $battingAVG
这是经过修改的代码审查。我已在评论中注明:
# Import-CSV already returns an array of objects.
# No need to wrap in another array
#ImportCSV = $CSVArray = Import-Csv "#CSV file to read"
# $CSVArray = @($ImportCSV)
$CSVArray = Import-Csv "#CSV file to read"
# Mispelling of 'ForEach-Object' below?
$CSVArray | ForEach-Object {
# $battingAvg is undeclared at this point, so how does it have a 'result' property?
# Maybe there's more to this variable that you haven't shared
# $battingAVG.result = [math]::Round($_.Hits / $_.AtBats)
$battingAVG = [math]::Round($_.Hits / $_.AtBats)
$battingObject = New-Object System.Object
# Now that you're inside the loop, get the Name property of the current record $_
$battingObject | Add-Member -Type NoteProperty -Name "Name" -Value $_.Name
$battingObject | Add-Member -Type NoteProperty -Note "BattingAverage" -Value $battingAVG
}
# You want to create a new object for each row of the data, so this code
# belongs inside your loop
# $battingObject = New-Object System.Object
# $battingObject | Add-Member -Type NoteProperty -Name "Name" -Value $CVSArray.Name
# $battingObject | Add-Member -Type NoteProperty -Note "BattingAverage" -Value $battingAVG
您还应该对 Calculated Properties 做一些研究并做一些实验;我相当确定所有这些代码都可以替换为:
Import-Csv "#CSV file to read" | Select-Object -Property Name, @{Name='BattingAverage';Expression={[math]::Round($_.Hits / $_.AtBats)}}