输出到日志文件失败,但PS6脚本运行没有错误

时间:2018-03-16 23:53:47

标签: powershell export-to-csv

我有其他软件正在导出csv文件,当一切正常运行时,我在csv中有1291行。我正在尝试轻松识别哪些文件不完整。

这是我第一次进入PowerShell。我修改了我在stackoverflow上找到的代码:

$files = Get-ChildItem 'd:\*.txt'

ForEach ($file in $files) {
    $lineCount = Get-Content -LiteralPath $file | Measure-Object | Select-Object -ExpandProperty Count
    Write-Output "File $($file.Name) has $lineCount lines"
    if ($lineCount -gt 50) {
        Write-Warning "Warning $($file.Name) is too big"
    }
}

并设法在没有错误的情况下实现这一目标:

$files = Get-ChildItem 'D:\!__DATA for searches\__CSVs\*.csv'

$logfile = 'D:\!__DATA for searches\__CSVs\CompletedFiles.txt'

ForEach ($file in $files) {
    $lineCount = Get-Content -LiteralPath $file | Measure-Object | Select-Object -ExpandProperty Count
    Write-Output "File $($file.Name) has $lineCount lines"
    if ($lineCount -gt 1290) {
        Write-Warning "Warning $($file.Name) is completed"   | Out-File $logfile -Append
    } 
}

以及此变体

$files = Get-ChildItem 'D:\!__DATA for searches\__CSVs\*.csv'

ForEach ($file in $files) {
    $lineCount = Get-Content -LiteralPath $file | Measure-Object | Select-Object -ExpandProperty Count
    Write-Output "File $($file.Name) has $lineCount lines"
    if ($lineCount -gt 1290) {
        Write-Warning "Warning $($file.Name) is completed"  | Out-File 'D:\!__DATA for searches\__CSVs\CompletedFiles.txt' -append
   } 
}

但是都没有写入日志文件。该脚本在PS6控制台中运行良好,用1291行识别文件。

所以我需要日志文件,另外我还希望将已完成的文件移动到同一个脚本中的另一个目录中,因此非常感谢您在何​​处/何处进行输入。

感谢答案和其他研究我完成了脚本

$files = Get-ChildItem 'D:\!__DATA for searches\__CSVs\*.csv'
$logfile = 'D:\!__DATA for searches\__CSVs\CompletedFiles.txt'

ForEach ($file in $files) {
    $lineCount = Get-Content -LiteralPath $file | Measure-Object | Select-Object -ExpandProperty Count
#    Write-Output "File $($file.Name) has $lineCount lines"
    if ($lineCount -gt 1290) {
#        Write-Warning "Warning $($file.Name) is completed" 3>&1 |Out-File $logfile -Append
    Write-Output  "File $($file.Name) has $lineCount lines, and moving to D:\Completed\" 3>&1 | Out-File $logfile -Append
     Move-Item "D:\!__DATA for searches\__CSVs\$($file.Name)" "D:\!__DATA for searches\Completed\" -Force
   } 
}

1 个答案:

答案 0 :(得分:1)

<Form.Field width={3}><Select placeholder='Options' options={options} style={{minWidth:"10em"}}/></Form.Field> 不会向Write-Warning发送任何内容。

您可以使用与stdout

类似的语法重定向警告信息流
cmd

Write-Warning "Warning $($file.Name) is completed" 3>> $logfile