This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Csv-Filer.ps1
68 lines (58 loc) · 2.84 KB
/
Csv-Filer.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<#
.Synopsis
This function is used for generating csv output from a log file that contains
visual studio debug info, and cleans text content for uploading to a log analysis service
.Description
The script might need to be run on-demand or via a scheduled task per modified folder or changed file event
It exports debug info and creates the appropriate csv formatted output files
NOTE: Generated file with size larger than 64K is transferred to loggly service,
otherwise exported to csv and opened via local text editor
Default target dir is: C:\%APPDATA%\
.Parameter smallsize
Used to identify the output file size either as uploaded or not uploaded
.Example
archive-logs -smallsize <-- Outputs of size smaller than 64k limit
archive-logs <-- Outputs of size larger or equal than 64k limit
Delimited debug info file format (shown in *.log output file):
-------------------------------------------------------------------------------------
TimeCreated|ComputerName|Problem|Empty|Class|FileName|ExceptionType|ExcepMessage|StackTrace|LOC <-- Header
yyyy-MM-dd HH:mm:ss|ComputerName|ExceptionType|freetext|ComponentName|MethodName|ExceptionText|StackTraceMsg:#lineNumber|#lineNumber <-- Contents
.
.
.
NOTE: You might require additional setup for data cleanup on loggly services site (based on requirements, input data format)
#>
function archive-logs {
param([switch]$smallsize)
$initialpath = "$env:APPDATA\output1.csv"
if (-not(Test-Path -PathType Leaf $initialpath) ) {
break;
}
$cont = (Get-Content -Path $initialpath | Select -Unique)
Set-Content -Value $cont -Path "$env:APPDATA\logs-out.csv"
$csvtemp = Import-csv -Path "$env:APPDATA\logs-out.csv" -Delimiter '|'
$lines = @()
$csvtemp | %{ $lines += [Regex]::Match($_.StackTrace,"(?<=:line )[0-9]{1,}"); }
foreach($rec in $csvtemp) {
$rec.LOC = $lines[[Array]::IndexOf($csvtemp,$rec)];
}
$num = $([System.Random]::new().Next(0,9999));
$finalpath = "$env:APPDATA\csvexcept-$num.csv"
$csvtemp | Export-Csv -Path ".\csvexcept-out.csv" -Delimiter ',' -NoTypeInformation -Encoding Default
Get-Content -Path ".\csvexcept-out.csv" | %{$_ -replace '"',''} | Set-Content -Path $finalpath
Remove-Item -Path ".\csvexcept-out.csv"
Remove-Item -Path "$env:APPDATA\output1.csv"
mkdir -Path "$env:APPDATA\csv-$num" -Force
Move-Item -Path $finalpath -Destination "$env:APPDATA\csv-$num"
$filingpath = "$env:APPDATA\csv-$num\csvexcept-$num.csv"
if ($smallsize) {
$appropsize = ( (Get-Item $filingpath).Length -ge 1KB)
}
$appropsize = ( (Get-Item $filingpath).Length -ge 64KB)
if ( (Test-Path -PathType Leaf $filingpath) -and ($appropsize) ) {
$ulpath = "$env:APPDATA\I386\curl.exe"
$urlserv = "http://logs-01.loggly.com/bulk/ab48807b-4cfb-452e-8371-f9eab2134e32/tag/file_upload"
Start-Process -FilePath $ulpath -ArgumentList "-X POST -T $filingpath $urlserv"
}else { Start-Process "notepad.exe" -FilePath $filingpath }
}
archive-logs -smallsize