Skip to content

Instantly share code, notes, and snippets.

@midnightfreddie
midnightfreddie / JoinUrlParams.ps1
Created December 10, 2016 19:39
ISE scratch work example of using hashtable to create URL parameters. Was in reply to somewhere on reddit
$Parameters = @{
a = '{14}'
b = '{22}'
}
$UrlParameters = ($Parameters.Keys | ForEach-Object {
"{0}={1}" -f [System.Web.HttpUtility]::UrlEncode($_), [System.Web.HttpUtility]::UrlEncode($Parameters[$_])
}) -join "&"
$Uri = "https://<rootURI>/calc/add?$UrlParameters"
$NinjaJs = @"
var NinjaQuerySelectorAll = function(selector) {
nodeList = document.querySelectorAll(selector);
output = [];
for (i=0; i < nodeList.length; i++) output.push(nodeList.item(i));
console.log("Ninja JS function output");
console.log(output);
return output;
}
"@
$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
# Given an array of rss xml document objects, return one rss document object with all the items sorted by date, descending
# Because of auto-deserialization, may need to build xml doc arrays with @(,$XmlDoc) trick
function Invoke-CombineRssXml {
[cmdletbinding()]
param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[xml[]]$Xml
)
begin {
Write-Verbose "Combining rss xml documents"
# This just puts each array in a vertical column of a table.
# The rows are not related data unless the source data is keyed on array index
$Arr1 = @(1, 2, 3, 4, 5)
$Arr2 = @(1, 2, 3)
$Arr3 = @(1, 2, 3, 4, 5, 6)
$Arr4 = @(1, 2, 3, 4, 5, 6, 7)
$Arr5 = @(1, 2)
$Arr6 = @(1, 2, 3, 4)
<#
.Synopsis
Roll dice
.DESCRIPTION
Given a role-playing description of dice, e.g. "2d6", return an object
with the randomly rolled results
.EXAMPLE
Invoke-Dice 2d6
.EXAMPLE
Invoke-Dice 2d6 -Bonus 3
@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
@midnightfreddie
midnightfreddie / FirstWeekdayAfterWeekday.ps1
Created November 8, 2017 19:45
Returns a date object of the first weekday of the month following another weekday, e.g. first Monday after Friday. In reply to https://www.reddit.com/r/PowerShell/comments/7bda01/calculate_first_monday_after_first_friday_of/
function Compare-WeekDays {
# Return days between weekdays
param (
[Parameter(Mandatory=$true)]
[System.DayOfWeek]$Minuend,
[Parameter(Mandatory=$true)]
[System.DayOfWeek]$Subtrahend
)
# In case they change how many days are in a week :)
$DaysInWeek = [System.Enum]::GetNames('System.DayOfWeek').Count
@midnightfreddie
midnightfreddie / ExportRegistry.ps1
Created November 8, 2017 05:08
Export remote registry in the same fashion as RegEdit's export function. In reply to https://www.reddit.com/r/PowerShell/comments/7bht8c/exporting_registries/
function Export-Registry {
Param (
$ComputerName = "iis",
$Key = "hklm\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
$LocalPath = "."
)
$DateText = (Get-Date -Format s) -replace ':', '-'
$FileName = "$DateText-$ComputerName.reg"
# PowerShell 5 and above
Enum MyConColor {
Black
Red
Green
Yellow
Blue
Magenta
Cyan
White