Skip to content

Instantly share code, notes, and snippets.

View OtterKring's full-sized avatar

Maximilian Otter OtterKring

  • AGRANA Beteiligungs-AG
View GitHub Profile
@OtterKring
OtterKring / RIDDLE_DougFinke_20190831.ps1
Last active August 31, 2019 08:12
Riddle_DougFinke_20190831
$base = 10
$num = ''
$esc = [char]27
$red = "$esc[31m"
$blue = "$esc[34m"
$reset = "$esc[39m"
for ($i = 1; $i -lt 10; $i++) {
$num = $num + $i
(' ' * ($base-$i-1)) + $num + ' x ' + $red + [string]($base*0.8) + $reset + ' + ' + $blue + $i + $reset + ' = ' + [string]([int32]$num * ($base * 0.8) + $i)
}
@OtterKring
OtterKring / SelfSave_LastRunTime.ps1
Last active March 18, 2021 14:19
Powershell Self-Modification
#region SAVE_RUNTIME
#region SELFMODIFICATION
$NewLastRunTime = '18/03/2021 14:51:46' #MODIFY_HERE
#region SELFMODIFICATION
$LastRunTime = [datetime]::Parse($NewLastRunTime)
$Me = $MyInvocation.MyCommand.Source
$Myself = Get-Content $Me
$Myself `
@OtterKring
OtterKring / gist:3830dd4340d20e91da918f27553053d2
Created November 8, 2021 15:02
emulate functions in foreach parallel
# create the code usually written as function as scriptblock
# it can be run directly in the main script as any other scriptblock
$loopcode = {
param($user,$counter)
write-host ("User: {0} {1}" -f $user,$counter)
}
# convert the scriptblock to string to be able to reuse it in foreach -parallel
# functions cannot be used in foreach -parallel
$codestring = [string]$loopcode
### all this goes into the begin {} block of a cmdlet
# check and/or set function cache
# scope must be global to remain in memory until the function is called again
# variable will be names "cache_" + name of the function
# initial value will be an empty hashtable, so you can add data nodes as needed in your function
$CacheName = "cache_$($MyInvocation.InvocationName)"
if ( -not ( Get-Variable -Name $CacheName -Scope global -ErrorAction SilentlyContinue ) ) {
New-Variable -Name $CacheName -Value @{} -Scope global
}