-1

This is the log file I would like to be converted to csv.

------------------------------------------------------------------------------

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         4         2         4         0         0         0
   Files :        17        10        17         0         0         0
   Bytes :    20.0 k      5.0k    15.0 k         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : Wednesday, May 19, 2021 10:12:44 AM

The csv file can have columns like: size, date, source and destination. I will attach the code I have to get the log above. Thank you for any help!

$path = "C:\Powershell\robocopylog.txt"
If(!(test-path $path))
{
      New-Item -ItemType file -Force -Path $path
}
$Logfile = "C:\Powershell\robocopylog.txt"
Clear-Content "C:\Powershell\robocopylog.txt" -Force
$EmailFrom = "email@company.com"
$EmailTo = "myemail@company.com"
$EmailBody = "Robocopy completed successfully. See attached log file for details"
$EmailSubject = "Robocopy Summary"

$files = @("data")


for($i = 0; $i -lt $files.Count; $i++){
    robocopy "c:\powershell\[$i])" "c:\transferdata\[$i])" /Z /e /xx /W:5 /MAXAGE:2 /NFL /NDL /NJH /nc /np /unilog+:$Logfile
}

Send-MailMessage -To $EmailTo -from $EmailFrom -Subject $EmailSubject -Body $EmailBody -attachment $Logfile -smtpserver 192.168.249.211 -Port 25
mike657
  • 11
  • 1
  • Possibly relevant: [Split string on arbitrary-length substrings (Powershell)](https://stackoverflow.com/questions/55731241/split-string-on-arbitrary-length-substrings-powershell). – Jeff Zeitlin May 19 '21 at 15:04
  • 2
    Seems similar to https://stackoverflow.com/q/67339504/447901 – lit May 19 '21 at 15:09
  • seems like no final solution was helped there. – mike657 May 19 '21 at 15:22
  • @jeffZeitlin yea thats a start – mike657 May 19 '21 at 15:23
  • I'm the author of [one of those answers](https://stackoverflow.com/a/67341934/4749264) and another on the [original question](https://stackoverflow.com/a/67337515/4749264). I tried helping the user through chat, but at the end of his workday he bailed and didn't come back. However you should be able to follow the comments right into the chat room. – Steven May 19 '21 at 15:33
  • did it work for him? any follow up?@steven – mike657 May 19 '21 at 15:35
  • Give me a few minutes. I'm going to give this a shot in an answer. The 2 questions aren't exactly the same. – Steven May 19 '21 at 15:36
  • @steven I appreciate you. Im a noobie at this – mike657 May 19 '21 at 15:38
  • @mike657 Which content of your log file should be mapped to your mentioned columns? I don't see anything suitable for source or destination, for example. – stackprotector May 19 '21 at 16:33
  • @stackprotector for source and destination I would have to either write a different script to get it from the robocopy line somehow. not too sure about it. – mike657 May 19 '21 at 17:58

1 Answers1

0

On closer inspection, the code bares uncanny similarity to the previous questions I and others have pointed out:

  1. Convert a robocopy log into a csv file
  2. send email with only one line of robocopy summary

Now that I see the similarity, my answer is almost the same:

$path = "C:\Powershell\robocopylog.txt"
If(!(test-path $path)) {
      New-Item -ItemType file -Force -Path $path
}

$CsvFile   = "C:\PowerShell\RoboCsv.txt"
$Logfile   = "C:\Powershell\robocopylog.txt"
$EmailBody = [Collections.ArrayList]@("completed successfully. See attached log file & below summary","")

$EmailParams = @{
    From       = "email@company.com"
    To         = "myemail@company.com"
    Subject    = "Robocopy Summary"
    SMTPServer = '192.168.249.211' 
    Port       = 25
}

Clear-Content "C:\Powershell\robocopylog.txt" -Force

$Files = @("data")

ForEach( $File in $Files)
{
    
    $Source = "C:\$File"
    $Dest   = "C:\NEW TEST\folder\folder\$File"
    
    $Log = robocopy $Source $Dest /Z /e /xx /W:5 /MAXAGE:2 /NFL /NDL /NJH /NC /NP /UniLog+:$Logfile /tee

    $Date = ( ( $log -match "^\s+Ended.+" ) -split ":", 2 )[1].Trim()
    $Line = ( $Log -match "^\s+Bytes.+" ) -split "\s+"
        
    $Copy = $Line[5]
    $Mult = $Line[4]

    $Line = "Bytes Copied: $Copy $Mult on $Date"

    [Void]$EmailBody.Add($Line)

    # For Csv output:
    [Void]$CsvData.Add(
        [PSCustomObject]@{
            SizeCopied  = "$Copy $Mult"
            Date        = $Date
            Source      = $Source
            Destination = $Dest
        } )
}

$EmailBody = $EmailBody -join "`r`n"

Send-MailMessage @EmailParams -Body $EmailBody -Attachment $Logfile

# Output to CSVFile
$CsvData | Export-Csv -Path $CsvFile -NoTypeInformation 

I added splatting of the email parameters, and I adjusted the CSV output to include the multiplier. However, this is very similar.

I'm not really sure what you want the email body to look like. I'd rather not re-explain this on top of my previous lengthy explanations, please study the other answers and their surrounding discussions for that:

  1. https://stackoverflow.com/a/67337515/4749264
  2. https://stackoverflow.com/a/67341934/4749264

I'll reiterate this does appear to be an incorrect use of Robocopy, but that is covered in those other discussions.

halfer
  • 19,824
  • 17
  • 99
  • 186
Steven
  • 6,817
  • 1
  • 14
  • 14
  • It asks for these params when running it. "cmdlet Send-MailMessage at command pipeline position 1 Supply values for the following parameters: From: " – mike657 May 19 '21 at 18:05
  • There's a small typo there: `@$EmailParams` --> `@EmailParams` – Theo May 19 '21 at 18:10
  • dont you need to create a .csv file in the script? @steven – mike657 May 20 '21 at 13:21
  • You mean the `$CsvFile` var. Yes, forgot to transpose from other examples. Just fixed it. Please keep in mind, the goal isn't to provide perfectly working code. That sometimes happens, but only through the community's commitment. If you suspected a problem you can try to fix it. Then give clear feedback here and we can update the answer. The primary goal is to help in understanding the concepts, but people use SO discussions for learning, so the quality of demo code is also important. Please consider accepting the answer which helps people find it. Robocopy is a popular topic too. – Steven May 20 '21 at 15:37
  • Steven: a good rule of thumb when writing an answer post on Stack Overflow is not to not address the question author too specifically. Your primary readership is the hundreds of readers that will see your post after the question author has seen it. Generalisable solutions for them, without meta-commentary relating only to the OP, is ideal. `:=)` – halfer Sep 01 '21 at 19:58
  • (Of course, posting advice is fine - ideally that should be placed in a comment under the question, or perhaps as comments under your answer, if you get into a conversation). – halfer Sep 01 '21 at 19:59