Skip to content

Instantly share code, notes, and snippets.

@midnightfreddie
midnightfreddie / Hashset.ps1
Created May 23, 2016 03:58
Random useless code that I want to remember for hashset and the return trick
function myfunc{
$myvar = new-object 'System.Collections.Generic.HashSet[string]'
# [void]$$myvar.add("value")
$myvar.add("value") | Out-Null
$myvar.add("value2") | Out-Null
# Write-Host $($myvar.GetType())
@(,$myvar)
}
(myfunc).GetType()
$returnedvalue = myfunc
Add-Type -TypeDefinition @"
public enum PcType {
DESKTOP,
LAPTOP
}
"@
$PcChoices = [System.Enum]::GetValues([type]"PcType")
# '^(DESKTOP|LAPTOP)\d{6}$' , but this lets us add/modify enum choices later with no code changes
$RegExMatch = "^({0})\d{{6}}$" -f ( ( $PcChoices | ForEach-Object { $_.ToString() } ) -join "|")
#$StockList = Get-Content -Path "stocks.json" | ConvertFrom-Json
$StockList = '[{ "TICK": "FDX", "EXCH": "NYSE" }, { "TICK": "G", "EXCH": "NYSE" }]' | ConvertFrom-Json
# ForEach ($Stock In $StockList)
$StockList | ForEach-Object {
$Stock = $_
$Sym = $Stock.TICK
$Exc = $Stock.EXCH
$RawData = Invoke-WebRequest "http://finance.google.com/finance/info?q=$Exc%3a$Sym"
# Dot-source function file
. .\BusinessDays.ps1
Describe 'Get-IsWeekDay' {
@{ Date = "2016-05-15"; IsWeekDay = $false},
@{ Date = "2016-05-16"; IsWeekDay = $true },
@{ Date = "2016-05-17"; IsWeekDay = $true },
@{ Date = "2016-05-18"; IsWeekDay = $true },
@{ Date = "2016-05-19"; IsWeekDay = $true },
@{ Date = "2016-05-20"; IsWeekDay = $true },
@midnightfreddie
midnightfreddie / SillyCombiningUnicode.ps1
Created May 4, 2016 02:43
Random silliness: Adding a combining glyph to every a-z character in a string.
# Silliness. U0300 - U0036F are combining unicode characters
# https://en.wikipedia.org/wiki/Combining_Diacritical_Marks
$String = "The quick, brown fox jumped over the lazy dog"
$Combining = 0x0300 .. 0x036F | ForEach-Object { [char]$_ } | Sort-Object { Get-Random }
$i = 0
($String.ToCharArray() | ForEach-Object { $_ -replace '[A-Za-z]', "`$0$($Combining[$i++])" }) -join ""
$Uri = "http://midnightfreddie.com/reddit/simpletable.html"
$InfoPage = Invoke-Webrequest -Uri $Uri
# Iterate over each <tbody> which contain all the body rows for each table
$InfoPage.ParsedHtml.getElementsByTagName("tbody") | ForEach-Object {
$Headers = $null
# Might need to uncomment the following line depending on table being parsed
@midnightfreddie
midnightfreddie / graylog-snippet.ps1
Last active April 18, 2016 13:45
Attempt at manipulating Powershell data for GELF Graylog input, in reply to https://www.reddit.com/r/devops/comments/4f9e7x/scraping_apache_weblogs_and_shipping_them_to/
# This isn't tested much as I don't have the original data, but I've tested varios snippets, and they seem to work
# $GraylogUrl = "http://example.tld:12201/gelf"
$GraylogUdpPort = 12201
$GraylogUdpHost = "example.tld"
$UDPclient = new-Object System.Net.Sockets.UdpClient
$UDPclient.Connect($GraylogUdpHost, $GraylogUdpPort)
$Enc = [system.Text.Encoding]::UTF8
function Get-Hype {
param (
[switch]$CleganeBowl,
[switch]$KingsMoot
)
New-Object psobject -Property ([ordered]@{
CleganeBowl = $CleganeBowl
CleganeBowlIsPresent = $CleganeBowl.IsPresent
CleganeBowlBound = $PSBoundParameters.ContainsKey('CleganeBowl')
KingsMoot = $KingsMoot
# Get the weather
$wttr = Invoke-WebRequest http://wttr.in/
# Regex to match lines with no data in them, including blank lines and decorative borders
$RegExNoData = '^[ ─┌┬┤├┐┼└┴┘]*$'
# This is used to match the phase line and exclude it.
# Also used to create an array of weather data for each date
$PhasesOfDay = @("Morning", "Noon", "Evening", "Night")
# In reply to https://www.reddit.com/r/PowerShell/comments/4a08hk/importcsv_with_as_a_hashtable/
# Public Google Form at https://docs.google.com/a/jimnelson.us/forms/d/1dYmWTcw3c3q9p3HGWGvbNDvXWekUEKQfgsecZOpsV-k
# Results are at https://docs.google.com/spreadsheets/d/1G7J8qCL4gQ_NSYLiGE_3dauSaa75ppzPmcO-Rl7yV6w
[cmdletbinding()]
param(
$SheetKey = "1G7J8qCL4gQ_NSYLiGE_3dauSaa75ppzPmcO-Rl7yV6w",
$MaxRows = 5