A N

June 10, 2016

Convert RoboCopy log details to Powershell Object

Filed under: Tips — donrsh @ 11:53 am
<#	
	 Created on:   	2016-06-08 
	 Created by:   	Arash Nabi
	 Email: 	arash@nabi.nu 
#>
$TimeDay = get-date -Format "yyyyMMdd"
$xResults = @()
$xErr = @()
foreach ($x in (get-childitem -path "C:\temp\log\$TimeDay"))
{
  

$LogFile = Get-Content -Path $x.FullName

foreach ($Lines in $LogFile)
{
	
	$Lines | Foreach {
        if ($_ -match 'ERROR :')
            { $xError = $_.SubString(8)
              $xTime  = $x.CreationTime  
                $P = [ordered]@{
				Error = $xError
                Time  = $xTime
                }
                $xErr += New-Object -TypeName PSObject -Property $P
            }
        
        if ($_ -match '   Source : ') { $Source = $_.Substring(12, $_.Length - 12) }
		if ($_ -match '     Dest : ') { $Destination = $_.Substring(12, $_.Length - 12) }
		if ($_ -match '    Dirs :')
		{
			$DirTotal = [Long]$_.Substring(12, 10).Trim()
			$DirCopied = [Long]$_.Substring(22, 10).Trim()
			$DirFailed = [Long]$_.Substring(52, 10).Trim()
		}
		if ($_ -match '   Files : ' -and $_.Length -gt 60)
		{
			$FileTotal = [Long]$_.Substring(12, 10).Trim()
			$FileCopied = [Long]$_.Substring(22, 10).Trim()
			$FileFailed = [Long]$_.Substring(52, 10).Trim()
		}
		if ($_ -match '   Times :') { $Duration = [TimeSpan]$_.Substring(12, 10).Trim() }
		if ($_ -match '   Ended : ')
		{
			$Ended = $_.Substring(11, $_.Length - 11).Trim()
			$Properties = [ordered]@{
				Source = $Source
				Destination = $Destination
				DirTotal = $DirTotal
				NewDirCopied = $DirCopied
				DirFailed = $DirFailed
				FileTotal = $FileTotal
				NewFileCopied = $FileCopied
				FileFailed = $FileFailed
				Ended = $Ended
                }
			$xResults += New-Object -TypeName PSObject -Property $Properties
		}
	}


}
}
$xResults | Format-Table -AutoSize

RoboCopy Powershell Object Results

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: