gpt4 book ai didi

powershell - 如何将当前项目返回到管道?

转载 作者:行者123 更新时间:2023-12-03 01:24:18 25 4
gpt4 key购买 nike

我目前正在导入csv文件并将其批量加载到sql表中。
使用此代码

$CSVDataTable = Import-Csv $csvFile | % -begin {$i=0} -process { Write-Progress -activity "Importing file" -currentOperation "Reading line $i" -PercentComplete -1; $i++; return $_ } | Out-DataTable
我能够显示进度,但是我想对其进行优化,而Ive发现的一项建议是利用StreamReader。
因此,我尝试了以下方法:
[int]$LinesInFile = 0
$reader = New-Object IO.StreamReader $csvFile
$line = $reader.ReadLine()
while($reader.ReadLine() -ne $null) { $LinesInFile++ }

$CSVDataTable = 0..($LinesInFile-1) | foreach {
$percent = ($_/$LinesInFile)*100
Write-Progress -Activity 'Importing from CSV' -Status "$percent % Complete" -CurrentOperation "Importing row # $($_+1)" -PercentComplete $percent;
return $reader[$_]
} | Import-Csv $csvFile | Out-DataTable
错误(由于 return $reader[$_]):
Import-Csv : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its
properties do not match any of the parameters that take pipeline input.

最佳答案

唯一可以使用管道传输到Import-Csv的东西是表示CSV文件的System.IO.FileInfo实例,例如Get-ChildItem的输出(顺便说一句,由于Windows PowerShell中的错误,该代码已损坏,因为在PowerShell [Core] v6 +中已修复)。
如果要报告Import-Csv调用的进度,请在其后放置ForEach-Object命令,在其中可以发出进度消息,然后通过以下方式传递Import-Csv的输出对象($_):

# Count the data rows in the input CSV file.
$rowCount = 0
switch -File $csvFile { default { ++$rowCount } }
--$rowCount # subtract 1 from the line count to account for the header row.

Import-Csv $csvFile | ForEach-Object -Begin { $i = 0 } {

$percent = '{0:N1}' -f (++$i / $rowCount * 100)
Write-Progress -Activity 'Importing from CSV' -Status "$percent % Complete" -CurrentOperation "Importing row # $i" -PercentComplete $percent

$_ # pass the object from Import-Csv through.

} | Out-DataTable

# Hide the progress bar now.
# (Otherwise it would linger until the script as a whole completes.)
Write-Progress '(unused))' -Completed

值得注意的是,按对象调用 Write-Progress会大大减慢的执行速度(计算行数也有其代价,甚至只是通过 ForEach-Object调用传递对象也是如此)。
缓解速度减慢的一种简单方法是 仅对每N个对象使用 Write-Progress,例如以下示例中的每个 100对象:
# Count the data rows in the input CSV file.
$rowCount = 0
switch -File $csvFile { default { ++$rowCount } }
--$rowCount # subtract 1 from the line count to account for the header row.

Import-Csv $csvFile | ForEach-Object -Begin { $i = 0 } {

if (++$i % 100 -eq 1 -or $i -eq $rowCount) {
$percent = '{0:N1}' -f ($i / $rowCount * 100)
Write-Progress -Activity 'Importing from CSV' -Status "$percent % Complete" -CurrentOperation "Importing row # $i" -PercentComplete $percent
}

$_ # pass the object from Import-Csv through.

} | Out-DataTable

# Hide the progress bar now.
# (Otherwise it would linger until the script as a whole completes.)
Write-Progress '(unused))' -Completed
注意:如果 Out-DataTable在将最后一个对象传递给它之后需要很短的处理时间,则可以基于该额外时间的估计值填充行数以达到百分比显示的目的,以真实百分比表示。行数。

关于powershell - 如何将当前项目返回到管道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64758238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com