modules/FileProcessor/FileProcessor.psm1
function Import-File(){ [CmdletBinding()] param( [string] $file,[bool] $convert, [string] $timezone ) $f = [IO.FileInfo]$file $extension = $f.Extension if ($extension -eq '.*') { if (Test-Path $file.Replace('.*', '.xml')) { $extension = '.xml' } if (Test-Path $file.Replace('.*', '.csv')) { $extension = '.csv' } if (Test-Path $file.Replace('.*', '.json')) { $extension = '.json' } } switch ($extension) { '.xml' { Import-Clixml -Path $fileName } '.csv' { ImportCsv -file $file -convert $convert -timezone $timezone } '.json' { ImportJson -file $file -convert $convert -timezone $timezone } } } function ConvertFrom-LineData([PSCustomObject] $line){ if ($line.data) { $line | Add-Member 'content' (ConvertFrom-Json -inputObject $line.data) } else { $line | Add-Member 'content' '' } $line.data = $null return $line } function ImportCsv(){ [CmdletBinding()] param( [string] $file,[bool] $convert, [string] $timezone ) $content = Get-Content $file $header=$null $delimiter=',' $fileContent = @() foreach ($line in $content) { if (-not $header){ $header = $line.Split($delimiter) } else { if ($line){ $lineObj = ($line | ConvertFrom-Csv -Header $header -Delimiter $delimiter) if ($convert) { $lineObj = (ConvertLineData $lineObj) } $lineObj.created_at = (ConvertLogDate -date $lineObj.created_at -timezone $timezone) $lineObj.updated_at = (ConvertLogDate -date $lineObj.updated_at -timezone $timezone) if ($lineObj.created_at -eq '') { $lineObj.created_at = $lineObj.updated_at} $fileContent += $lineObj } } } return $fileContent } function ImportJson(){ [CmdletBinding()] param( [string] $file,[bool] $convert, [string] $timezone ) $content = (Get-Content $file | ConvertFrom-Json) foreach ($row in $content) { if ($convert) { $row = (ConvertLineData $row) } $row.created_at = (ConvertLogDate -date $row.created_at -timezone $timezone) $row.updated_at = (ConvertLogDate -date $row.updated_at -timezone $timezone) if ($row.created_at -eq '') { $row.created_at = $row.updated_at} } return $content } function ConvertLogDate([string] $date, [string] $timezone){ return [datetime]($date + ' ' + $timezone) } |