Public/ConvertTo-ALHDateTime.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 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
<#PSScriptInfo
.VERSION 1.2.0 .GUID 044cd38b-8558-4820-b412-c56121c12018 .AUTHOR Dieter Koch .COMPANYNAME .COPYRIGHT (c) 2021-2023 Dieter Koch .TAGS .LICENSEURI https://github.com/admins-little-helper/ALH/blob/main/LICENSE .PROJECTURI https://github.com/admins-little-helper/ALH .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES 1.0.0 Initial release 1.1.0 Added parameter 'AsLocalTime' 1.2.0 Added pipeline support. Set defaut value to current date/time for parameter 'DateTime' #> <# .DESCRIPTION Contains a function to convert miliseconds since 1970 or 1601 or ticks since 1601 to a DateTime value. #> function ConvertTo-ALHDateTime { <# .SYNOPSIS Converts miliseconds since 1970 or 1601 or ticks since 1601 to a DateTime value. .DESCRIPTION Converts miliseconds since 1970 or 1601 or ticks since 1601 to a DateTime value (by defaults in UTC time). The function returns $null .PARAMETER DateTimeValue Miliseconds since 01.01.1700 00:00:00 or miliseconds since 01.01.1601 00:00:00 or ADDateTime value. .PARAMETER AsLocalTime If specified, the resulting DateTime value will be interpreted in local time zone, instead of UTC. .EXAMPLE ConvertTo-ALHDateTime -DateTimeValue 13270022798437 DateTimeBase1970 DateTimeBase1601 DateTimeBaseAD ---------------- ---------------- -------------- 06.07.2390 03:26:38 06.07.2021 03:26:38 16.01.1601 08:36:42 Convert a int64 value representing an miliseconds since 01.01.1601 to date/time in UTC format. .EXAMPLE ConvertTo-ALHDateTime -DateTimeValue 13270022798437 DateTimeBase1970 DateTimeBase1601 DateTimeBaseAD ---------------- ---------------- -------------- 06.07.2390 03:26:38 06.07.2021 03:26:38 16.01.1601 08:36:42 Convert a int64 value representing an miliseconds since 01.01.1601 to date/time in UTC format. .EXAMPLE (Get-ADUser -Filter * -Property lastLogonTimeStamp).lastLogonTimeStamp | ConvertTo-ALHDateTime DateTimeBase1970 DateTimeBase1601 DateTimeBaseAD ---------------- ---------------- -------------- 04.11.2022 09:43:10 04.11.2022 09:43:10 23.02.2020 17:59:40 23.02.2020 17:59:40 23.11.2015 05:57:05 09.11.2022 11:14:48 This example shows how to retrieve the lastLogonTimeStamp for all users in Active Directory and get the DateTime value of for it. .INPUTS System.Int64 .OUTPUTS PSCustomObject .NOTES Author: Dieter Koch Email: diko@admins-little-helper.de .LINK https://github.com/admins-little-helper/ALH/blob/main/Help/ConvertTo-ALHDateTime.txt #> [CmdletBinding()] param ( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] [double[]][AllowNull()] $DateTimeValue, [switch] $AsLocalTime ) begin { $DT1970 = Get-Date -Date "1970-01-01T00:00:00" $DT1601 = Get-Date -Date "1601-01-01T00:00:00" $DateTimeConverted = [PSCustomObject]@{ DateTimeBase1970 = $DT1970.AddMilliseconds($DateTimeValueElement) DateTimeBase1601 = $DT1601.AddMilliseconds($DateTimeValueElement) DateTimeBaseAD = [DateTime]::FromFileTime($DateTimeValueElement) } } process { if ($null -eq $DateTimeValue) { # return a PSCustomObject with $null values for each property $DateTimeConverted } else { foreach ($DateTimeValueElement in $DateTimeValue) { try { if (($DateTimeValueElement * 10000) -lt [DateTime]::MaxValue.Ticks) { [datetime]$LocalDTBase1970 = $DT1970.AddMilliseconds($DateTimeValueElement) [datetime]$LocalDTBase1601 = $DT1601.AddMilliseconds($DateTimeValueElement) } } catch { $_ } try { [datetime]$LocalDTBaseAD = [DateTime]::FromFileTime($DateTimeValueElement) } catch { $_ } if ($AsLocalTime.IsPresent) { $DateTimeConverted.DateTimeBase1970 = $( if ($null -ne $LocalDTBase1970) { $LocalDTBase1970 } ) $DateTimeConverted.DateTimeBase1601 = $( if ($null -ne $LocalDTBase1601) { $LocalDTBase1601 } ) $DateTimeConverted.DateTimeBaseAD = $( if ($null -ne $LocalDTBaseAD) { $LocalDTBaseAD } ) } else { $DateTimeConverted.DateTimeBase1970 = $( if ($null -ne $LocalDTBase1970) { $LocalDTBase1970.ToUniversalTime() } ) $DateTimeConverted.DateTimeBase1601 = $( if ($null -ne $LocalDTBase1601) { $LocalDTBase1601.ToUniversalTime() } ) $DateTimeConverted.DateTimeBaseAD = $( if ($null -ne $LocalDTBaseAD) { $LocalDTBaseAD.ToUniversalTime() } ) } $DateTimeConverted } } } } #region EndOfScript <# ################################################################################ ################################################################################ # # ______ _ __ _____ _ _ # | ____| | | / _| / ____| (_) | | # | |__ _ __ __| | ___ | |_ | (___ ___ _ __ _ _ __ | |_ # | __| | '_ \ / _` | / _ \| _| \___ \ / __| '__| | '_ \| __| # | |____| | | | (_| | | (_) | | ____) | (__| | | | |_) | |_ # |______|_| |_|\__,_| \___/|_| |_____/ \___|_| |_| .__/ \__| # | | # |_| ################################################################################ ################################################################################ # created with help of http://patorjk.com/software/taag/ #> #endregion |