powershell - Parsing robocopy log file to PSCustomObject -


i'm trying create pscustomobject robocopy log file. first piece pretty easy i'm struggling $footer part. can't seem find way split values.

it nice if every entry has it's own property, it's possible use example $total.dirs or $skipped.dirs. thinking import-csv, because that's great on how allows have column headers. doesn't seem fit here. there's solution found here seems 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 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 , 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 help.

you can clean more basic approach take.

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*"){                 $lineasarray = (($_.split(':')[1]).trim()) -split '\s+'                 $dirs = [pscustomobject][ordered]@{                     total =  $lineasarray[0]                        copied =  $lineasarray[1]                       skipped =  $lineasarray[2]                       mismatch = $lineasarray[3]                          failed = $lineasarray[4]                          extras = $lineasarray[5]                  }             }             if ($_ -like "*files*"){                 $lineasarray = ($_.split(':')[1]).trim() -split '\s+'                 $files = [pscustomobject][ordered]@{                     total =  $lineasarray[0]                        copied =  $lineasarray[1]                       skipped =  $lineasarray[2]                       mismatch = $lineasarray[3]                          failed = $lineasarray[4]                          extras = $lineasarray[5]                  }             }             if ($_ -like "*times*"){                 $lineasarray = ($_.split(':',2)[1]).trim() -split '\s+'                 $times = [pscustomobject][ordered]@{                     total =  $lineasarray[0]                        copied =  $lineasarray[1]                          failed = $lineasarray[2]                          extras = $lineasarray[3]                  }             }         }          $obj = [pscustomobject]@{                 'source'      = $source                 'destination' = $destination                 'dirs'        = $dirs                 'files'       = $files                 'times'       = $times         }                        write-output $obj     } } 

i wanted make function parse footer lines $times special case since not have same columns of data.

with $times important difference how doing split. since string contains more 1 colon need account that. using other paramenter in .split() specify number of elements return.

$_.split(':',2)[1]

since these logs have output , no blanks row elements can assuming parsed elements of $lineasarray have 6 elements.

sample output

source      : \\share\source\ destination : \\share\target\ dirs        : @{total=2; copied=0; skipped=2; mismatch=0; failed=0; extras=0} files       : @{total=203; copied=0; skipped=203; mismatch=0; failed=0; extras=0} times       : @{total=0:00:00; copied=0:00:00; failed=0:00:00; extras=0:00:00} 

so if wanted total files copied can use dot notation.

(convertfrom-robocopylog c:\temp\log.log).files.total 203 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -