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
@IISResetMe
IISResetMe / start-wait.ps1
Created January 29, 2016 18:08
Start-Process -Wait (for the children)
# Child process exe, opens notepad.exe
$null = Add-Type -TypeDefinition 'static class Test{static void Main(string[] args){System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(@"C:\windows\system32\notepad.exe"));}}' -OutputAssembly child.exe
# Parent process exe, opens child process
$null = Add-Type -TypeDefinition $('static class Test{static void Main(string[] args){System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(@"' + (Get-Item child.exe).FullName + '"));}}') -OutputAssembly parent.exe
# Wait for Parent to "complete"
$p = Start-Process .\parent.exe -Wait -PassThru
@IISResetMe
IISResetMe / join.vba
Created February 4, 2016 15:25
Better join for Excel
Function Join(InputRange As Range, Optional Separator As String = ",") As String
Dim CurrentRange As Range
Dim FinalString As String
For Each CurrentRange In InputRange
If CurrentRange.Text <> Separator Then
FinalString = FinalString & CurrentRange.Text & Separator
End If
Next
Join = Left(FinalString, Len(FinalString) - Len(Separator))
End Function
param([Parameter(Position = 0)][string] $Path = 'C:\Users\grave\Downloads\ChessData-master\')
$code = @{
Name = 'ResultCounter'
Namespace = 'ChessData'
PassThru = $true
UsingNamespace = @(
'System.Collections.Concurrent',
'System.IO',
'System.Threading.Tasks'
)
@IISResetMe
IISResetMe / crazyslowadd.ps1
Last active April 1, 2016 11:46
op_Addition aint yer friend
Measure-Command{
$startNumber = 0
$endNumber = 20000
$random = New-Object System.Random
$set = New-Object 'System.Collections.Generic.HashSet[int]'
$limit = $endNumber - $startNumber
$sequence = while($set.Count -lt $limit)
@IISResetMe
IISResetMe / ZeroMemShuffling.ps1
Created April 24, 2016 13:23
Shuffling collections without choking on memory
<#
Optimizing for low memory footprints
A popular choice for randomizing collection ordering in PowerShell is this simple and effective pipeline:
$array |Sort-Object {Get-Random}
An equivalent super-easy-to-implement algorithm that comes to mind is the "Fisher-Yates Shuffle"
From Wikipedia (https://en.wikipedia.org/wiki/Fisher–Yates_shuffle):
@IISResetMe
IISResetMe / here-string.ps1
Created May 30, 2016 13:29
Here-string first and last
$thisCode = @"
is perfectly
"@ -split "\s+" # valid
@IISResetMe
IISResetMe / Resume-Workflow.ps1
Created July 29, 2016 11:40
PowerShell Workflow resumption with Checkpoint-Worflow
# define workflow
workflow test
{
$i = 0
while($i -lt 10){
$i++
"$i"
Start-Sleep -Seconds 5
Checkpoint-Workflow
}
@IISResetMe
IISResetMe / New-CounterString.ps1
Last active August 18, 2016 11:08
PowerShell counter-string generator
<#
PowerShell port of James Bach's counterstring perl function (http://www.satisfice.com/tools.shtml)
Paraphrased from the original help text:
New-CounterString -Length $n -Char $c; produces a special string of length $n that counts its own characters.
"New-CounterString 10" would produce "*3*5*7*10*" which is a ten-character long string, such that each asterisk is at a position in the string equal to the number that precedes it.
This is useful for pasting into fields that cut off text, so that you can tell how many characters were actually pasted. You can specify a separator other than asterisk. "New-CounterString 15 A" would produce "A3A5A7A9A12A15A"
#>
function New-CounterString
{
@IISResetMe
IISResetMe / SBvClosure.ps1
Created August 30, 2016 00:18
ScriptBlock v Closure
function Get-ScriptBlock {
$var = "Success!"
return { $var }
}
function Get-Closure {
$var = "Success!"
return { $var }.GetNewClosure()
}
@IISResetMe
IISResetMe / Get-HammingDistance.ps1
Created September 20, 2016 16:02
Linq extension methods in Powershell
function Get-HammingDistance
{
param(
[Parameter(Mandatory)]
[string]$String1,
[Parameter(Mandatory)]
[string]$String2
)
if($String1.Length -ne $String2.Length)