Skip to content
Permalink
Browse files

Update from Forestry.io

Sheldon Hull updated content/docs/powershell.md
  • Loading branch information...
sheldonhull authored and Forestry.io committed May 14, 2019
1 parent 04de6ea commit d160beae901eee7dae8227c92df04a4e4dfbac2b
Showing with 28 additions and 9 deletions.
  1. +28 −9 content/docs/powershell.md
@@ -1,16 +1,17 @@
---
title: "powershell"
slug: "powershell"
date: '2019-03-19'
last_modified_at: '2019-03-19'
title: powershell
slug: powershell
date: 2019-03-19
last_modified_at: 2019-03-19
toc: true
excerpt: A cheatsheet for some interesting PowerShell related concepts that might benefit others looking for some tips and tricks
permalink: /docs/powershell
excerpt: A cheatsheet for some interesting PowerShell related concepts that might
benefit others looking for some tips and tricks
permalink: "/docs/powershell"
tags:
- development
- powershell
---
- development
- powershell

---
## String Formatting

| Type | Example | Output | Notes |
@@ -24,3 +25,21 @@ tags:
|---------------------|---------|-------------------|---------------------|-------------------------------------------|
| scientific notation | Decimal | 2.19095E+08 / 1MB | 208.945274353027 MB | Native PowerShell, supports 1MB, 1KB, 1GB |


## Date & Time Conversion

Converting dates to Unix Epoc time can be challenging without using the correct .NET classes. There is some built in functionality for converting dates such as `(Get-Date).ToUniversalTime() -UFormat '%s'` but this can have problems with time zone offsets. A more consistent approach would be to leverage the following. This was very helpful to me in working with Grafana and InfluxDb which commonly leverage Unix Epoc time format with seconds or milliseconds precision.

```powershell
$CurrentTime = [DateTimeOffset]::new([datetime]::now,[DateTimeOffset]::Now.Offset);
# Unix Epoc time starts from this date
$UnixStartTime = [DateTimeOffset]::new(1970, 1, 1, 0, 0, 0,[DateTimeOffset]::Now.Offset);
# To Use This Now On Timestamp you could run the following
$UnixTimeInMilliseconds = [Math]::Floor( ((get-date $CurrentTime) - $UnixStartTime).TotalMilliseconds)
# To Use with Different Time just change the `$CurrentTime` to another value like so
$UnixTimeInMilliseconds = [Math]::Floor( ((get-date $MyDateValue) - $UnixStartTime).TotalMilliseconds)
```

0 comments on commit d160bea

Please sign in to comment.
You can’t perform that action at this time.