Skip to content

Instantly share code, notes, and snippets.

View IISResetMe's full-sized avatar

Mathias R. Jessen IISResetMe

  • Booking.com
  • Netherlands
View GitHub Profile
body *:not(code) { font-family: arial !important; }
code {font-family: monospace !important;}
@IISResetMe
IISResetMe / Get-InvocationQuote.ps1
Created December 31, 2020 14:21
Quote repeatable command invocation from [InvocationInfo] for simple parameter types
function Get-InvocationQuote
{
param([System.Management.Automation.InvocationInfo]$Invocation)
$cmdText = $Invocation.InvocationName
foreach($param in $Invocation.BoundParameters.GetEnumerator()){
$name = $param.Key
$value = switch($param.Value){
{$_ -is [string]} {
# Quote and escape all string values as sigle-quoted literals
@IISResetMe
IISResetMe / fib.ps1
Created December 31, 2020 02:28
Nega-fibonacci with (non-optimal) caching
# Z-fibonacci (or nega-fibonacci, see https://en.wikipedia.org/wiki/Fibonacci_number#Sequence_properties):
# F(n-2) = F(n) - F(n-1)
$function:fib = & {
$cache = [System.Collections.Generic.Dictionary[int,bigint]]::new()
-1..1 |Foreach-Object {
$cache.Add($_, $_)
}
return {
@IISResetMe
IISResetMe / MagicNumber.class.ps1
Created December 19, 2020 21:18
ECMA-335 SpecialName-based operator overloading in PowerShell classes
class MagicNumber
{
hidden [int] $_value
MagicNumber([int]$value)
{
$this._value = $value
}
# ECMA-335 I.10.3.2
function ConvertTo-List
{
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[object[]]$InputObject
)
begin {
$list = [System.Collections.Generic.List[object]]::new()
}
function Fake-Progress
{
$PSDefaultParameterValues['Write-Progress:Activity']='Making stuff up'
Write-Progress -Status 'Starting out' -PercentComplete 5
10..45|%{
Write-Progress -Status 'Doing stuff' -PercentComplete $_
Start-Sleep -Milliseconds 100
}
function Get-XmlFileTreeAppend
{
param(
[Parameter(ParameterSetName='Path', Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string[]]
${Path},
[Parameter(ParameterSetName='LiteralPath', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('PSPath')]
[string[]]
function Invoke-RegexTerror
{
[CmdletBinding(DefaultParameterSetName = 'ToBeOrNotToBe')]
param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[int]$Count,
[Parameter(ParameterSetName = 'ToBeOrNotToBe')]
[switch]$Shakespeare,
function Get-ADLockedOutUser {
[CmdletBinding(DefaultParameterSetName = 'MultiDC')]
param(
[Parameter(Mandatory = $false, ParameterSetName = 'MultiDC')]
[string[]]$DCFilter = @('*'),
[Parameter(Mandatory = $true, ParameterSetName = 'PDCOnly')]
[switch]$PDCOnly
)
using namespace System.Net.Sockets
using namespace System.Net.Security
using namespace System.Security.Cryptography.X509Certificates
function ConvertFrom-X509Certificate {
param(
[Parameter(ValueFromPipeline)]
[X509Certificate2]$Certificate
)