1

What I am attempting to do is read the file size from 4 files every minute and then output the results to a CSV file. I would like each file size written to their own column. I have attempted this with the code below but I am not getting the results I need. When the file sizes are written to the CSV they are added to a single column, each size on their own row. If I expand my array ($Process) to two records it forces all of the file lengths in the first record to the first row, each in their own column (which is what I want). The second record does the same. Is there any way to force a single record to one line and their values in their own columns by making an adjustment to the code? I appreciate your help. Thank you.

$Csv = 'c:\users\rob\LogFileSize.csv'
$today = get-date -f "ddd"
$day = $today.ToLower()
$date = Get-Date
$numColstoExport=5

$a = dir C:\RTscada\bin\ErrorLogs\error1_Log_$day.txt
$a | Add-Member -MemberType AliasProperty -Name FileLength -Value Length
$b = dir C:\RTscada\bin\ErrorLogs\error2_Log_$day.txt
$b| Add-Member -MemberType AliasProperty -Name FileLength -Value Length
$c = dir C:\RTscada\bin\ErrorLogs\error3_Log_$day.txt
$c | Add-Member -MemberType AliasProperty -Name FileLength -Value Length
$d = dir C:\RTscada\bin\ErrorLogs\error4_Log_$day.txt
$d | Add-Member -MemberType AliasProperty -Name FileLength -Value Length


$error1 = $a.Length
$error2 = $b.Length
$error3 = $c.Length
$error4 = $d.Length

# Array of date and file lengths
$Process = @($date, $error1, $error2, $error3, $error4)

$holdarr=@()
$pNames=@("Date", "Error1", "Error2","Error2","Error4")
foreach ($row in $Process){
    $obj = new-object PSObject
    for ($i=0;$i -lt $numColstoExport; $i++){
        $obj | Add-Member -MemberType NoteProperty -Name $pNames[$i] -Value $row[$i]
        }
    $holdarr+=$obj
    $obj=$null
}

$holdarr | export-csv $Csv -NoTypeInformation -Append

2 Answers 2

1

This should do the trick:

function MonitorLogFiles  {

    Param(
        $LogFiles,
        $Csv
    )

    $FirstCsvLine = 'Date'

    foreach ($LogFile in $LogFiles) {
        $FirstCsvLine = $FirstCsvLine + ",$(Split-Path $LogFile -leaf)"
    }

    $FirstCsvLine | Out-File $Csv  -Encoding UTF8


    while ($True) {

            $CurrentCsvLine = (Get-Date -format "dd-MMM-yyyy HH:mm").ToString()

            foreach ($LogFile in $LogFiles) {

                $Size = (Get-Item $LogFile).Length / 1KB

                $CurrentCsvLine = $CurrentCsvLine + ",$Size KB"

             }

            $CurrentCsvLine | Out-File $Csv  -append  -Encoding UTF8
            Start-Sleep -Seconds 60
           }

}

MonitorLogFiles -LogFiles C:\test.txt,C:\Test2.txt -Csv c:\test.csv
0

You can do something like this:

$holdarr|Select-Object -Property Name, Value | Export-Csv $Csv -NoTypeInformation -Append

Note: Whatever desired columns you wish to select , take all of them in the select-object.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.