Skip to content

Instantly share code, notes, and snippets.

@pezhore pezhore/Get-OlkbOrder.ps1
Last active Nov 29, 2018

Embed
What would you like to do?
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 = '100005649',
[Switch] $TrackOrder = $true,
[System.String] $TrackingFile = "C:\temp\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
You can’t perform that action at this time.