I'm trying to create a PSCustomObject
from a robocopy log file. The first piece is pretty easy but I'm struggling with the $Footer
part. I can't seem to find a good way to split up the values.
It would be nice if every entry has it's own Property
, so it's possible to use for example $Total.Dirs
or $Skipped.Dirs
. I was thinking about Import-CSV
, because that's just great on how it allows you to have column headers. But this doesn't seem to fit here. There's another solution I found here but it seems a bit of overkill.
Code:
Function ConvertFrom-RobocopyLog {
Param (
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
[String]$LogFile
)
Process {
$Header = Get-Content $LogFile | select -First 10
$Footer = Get-Content $LogFile | select -Last 7
$Header | ForEach-Object {
if ($_ -like "*Source*") {$Source = (($_.Split(':'))[1]).trim()}
if ($_ -like "*Dest*") {$Destination = (($_.Split(':'))[1]).trim()}
}
$Footer | ForEach-Object {
if ($_ -like "*Dirs*") {$Dirs = (($_.Split(':'))[1]).trim()}
if ($_ -like "*Files*") {$Files = (($_.Split(':'))[1]).trim()}
if ($_ -like "*Times*") {$Times = (($_.Split(':'))[1]).trim()}
}
$Obj = [PSCustomObject]@{
'Source' = $Source
'Destination' = $Destination
'Dirs' = $Dirs
'Files' = $Files
'Times' = $Times
}
Write-Output $Obj
}
}
Log file:
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Wed Apr 01 14:28:11 2015
Source : \\SHARE\Source\
Dest : \\SHARE\Target\
Files : *.*
Options : *.* /S /E /COPY:DAT /PURGE /MIR /Z /NP /R:3 /W:3
------------------------------------------------------------------------------
0 Files...
0 More Folders and files...
------------------------------------------------------------------------------
Total Copied Skipped Mismatch FAILED Extras
Dirs : 2 0 2 0 0 0
Files : 203 0 203 0 0 0
Bytes : 0 0 0 0 0 0
Times : 0:00:00 0:00:00 0:00:00 0:00:00
Ended : Wed Apr 01 14:28:12 2015
Thank you for your help.