-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert-FromUnixDate.ps1
More file actions
99 lines (90 loc) · 3.11 KB
/
Convert-FromUnixDate.ps1
File metadata and controls
99 lines (90 loc) · 3.11 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#Requires -Version 2.0
<#
.Synopsis
Convert Unix epoch time
.DESCRIPTION
Convert Unix epoch time to human readable timestamp.
Possible inputs are a single Epoch value or CSV file.
By default, the script expects the CSV to have the fields "Start Time" and "Stop Time". These can be modified with the -Columns parameter
.EXAMPLE
Convert-FromUnixDate -Epoch 1508242825
.EXAMPLE
Convert-FromUnixDate -CSV C:\path\to\file.csv
.EXAMPLE
Convert-FromUnixDate -CSV .\logs.csv -Verbose -Columns "Start Time","Stop Time"
.EXAMPLE
Convert-FromUnixDate -CSV C:\path\to\file.csv | Export-Csv .\converted.csv -NoTypeInformation
.EXAMPLE
$times = 1508242825,1508242825
foreach ($time in $times) { Convert-FromUnixDate -Epoch $time }
.NOTES
Author: haam3r
Version: 1.0
#>
function Convert-FromUnixDate
{
[CmdletBinding()]
[Alias()]
[OutputType([int])]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0,
HelpMessage='Single epoch value',
ParameterSetName='SingleValue')]
[int]$Epoch,
[Parameter(Mandatory=$false,
Position=1,
ValueFromPipelineByPropertyName=$True,
HelpMessage='Source CSV file',
ParameterSetName='ImportCSV')]
[ValidateScript({Test-Path $_ -PathType 'Leaf'})]
[Alias("C")]
[string]$CSV,
[Parameter(Mandatory=$false,
Position=2,
ValueFromPipelineByPropertyName=$True,
HelpMessage='Cloumns to convert',
ParameterSetName='ImportCSV')]
$Columns = @("Start Time","Stop Time")
)
Begin {
# Set Unix beginning of epoch time
[datetime]$Origin = '1970-01-01 00:00:00'
}
Process {
if ($Epoch) {
Write-Verbose "Got a single epoch time value of $Epoch"
$Converted = [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($Epoch))
$Report += @(Get-Date -Format "yyyy-MM-dd HH:mm:ss" $Converted)
}
elseif ($CSV) {
Write-Verbose "Importing from $CSV"
$Logs = Import-Csv -Path $CSV -Delimiter ","
# Check if specified columns are present in the CSV
$Headers = $Logs | Get-Member
foreach($Column in $Columns) {
if($Headers.Name -contains "$Column") {
Write-Verbose "Found Column with Name: $Column"
}
else {
throw "Could not find column header named: $Column"
}
}
Write-Verbose "Converting timestamps"
foreach ($Log in $Logs) {
foreach ($Column in $Columns) {
$Log.$Column = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $Origin.AddSeconds($Log.$Column)
}
$Report += @($Log)
}
}
}
End
{
Write-Verbose "Processed $($Report.Count) rows"
$Report
}
}