Last active
May 20, 2020 16:27
-
-
Save pezhore/2b70a16b52f0fcf82ead53fd117ba8c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-OlkbOrder { | |
<# | |
.SYNOPSIS | |
Checks for a given order on https://olkb.com/ to determine the current place in line for shipping. Optionally stores the | |
place in line in a file on disk. | |
.DESCRIPTION | |
Parses the current orders markdown file on github for https://olkb.com orders, searching for a given order number. When | |
found, the order number and place in line is output. If the option for tracking is provided, the current place in line | |
is compared to the previously stored place in line and the difference is displayed. | |
.EXAMPLE | |
PS C:\> Get-OlkBorder -OrderNumber 100005999 -TrackOrder:$false | |
Checks order number 100005999 for current place in line and outputs the result. The status is not persistent (i.e. is not | |
saved to disk) | |
.EXAMPLE | |
PS C:\> iex ((New-Object System.Net.WebClient).DownloadString('https://gist.githubusercontent.com/pezhore/2b70a16b52f0fcf82ead53fd117ba8c9/raw')); Get-OlkbOrder | |
Loads the function into memory from github and executes Get-OlkbOrder with default options. | |
#> | |
[CmdletBinding()] | |
param( | |
[System.String] $OrderNumber = '100007911', | |
[Switch] $TrackOrder = $true, | |
[System.String] $TrackingFile = "olkborder.xml" | |
) | |
begin { | |
# Attempt to load required modules | |
try { | |
Import-Module psmarkdown -ErrorAction Stop | |
} | |
catch { | |
Throw "Error loading psmarkdown module: $_" | |
} | |
} | |
process{ | |
# Try to grab the current raw order list | |
try { | |
$Raw = Invoke-WebRequest -Uri https://raw.githubusercontent.com/olkb/orders/master/README.md -ErrorAction Stop | |
} | |
catch { | |
Write-Debug "Something went wrong invoking web request. Debug?" | |
throw "Something went wrong invoking web request." | |
} | |
# Grab the content and convert it from markdown | |
$Content = $Raw.content | |
$PsObj = $Content | ConvertFrom-Markdown -WarningAction SilentlyContinue | |
# Find this order entry in the resulting object | |
$OrderEntry = ($PsObj -match $OrderNumber).H1 | |
# If we can't find the order, or if there's multiple orders, then throw an error. | |
if (! $OrderEntry.Count) { | |
Write-Debug "Order not found, debug?" | |
throw "Order not found" | |
} | |
elseif ($OrderEntry.count -ne 1) { | |
Write-Debug "Issue while filtering for order $OrderNumber, debug?" | |
throw "Issue while filtering for order $OrderNumber" | |
} | |
# Create a custom order object derived from the current date and the Order entry (in the format [Place in line].[Order Number]) | |
$Order = [PScustomObject] @{ | |
PlaceInLine = $OrderEntry.Split('.')[0] | |
OrderNumber = $OrderNumber | |
Date = Get-Date | |
} | |
# Output to the information stream the order/place in line | |
Write-Information -InformationAction Continue "Order: $($Order.OrderNumber), Place in line: $($Order.PlaceInLine)" | |
# If we're using persistent tracking, do more work | |
if ($TrackOrder) { | |
# If the tracking file doesn't exist, just export the order object to the tracking file. | |
if (!(Test-Path -Path $TrackingFile -Type Leaf)){ | |
$Order | Export-Clixml -Path $TrackingFile | |
} | |
else { | |
# If the tracking file does exist, grab the old status | |
$OldStatus = Import-Clixml -Path $TrackingFile | |
# Compare the old status to the current order object to find how many spots we've moved up | |
$PlaceProgress = $OldStatus.PlaceInLine - $Order.PlaceInLine | |
# Compare the old status to the current order object to find how long it has been since our last order update | |
$DateProgress = $Order.Date - $OldStatus.Date | |
# Output to the information stream | |
$Message = "You've moved up [ $PlaceProgress ] in $($DateProgress.Days) Days, $($DateProgress.Hours) Hours, $($DateProgress.Minutes) Minutes" | |
Write-Information -InformationAction Continue -MessageData $Message | |
# If we've moved up or down in our place in line, export the current order to the tracking file. | |
if ($PlaceProgress -ne 0) { | |
$Order | Export-Clixml -Path $TrackingFile | |
} | |
} | |
} | |
} | |
end{ | |
Write-Debug -Message "About to leave $($MyInvocation.MyCommand.Name), debug?" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment