Skip to content

Instantly share code, notes, and snippets.

@idavis
idavis / Use-Impersonation.ps1
Created March 5, 2011 18:52
Impersonate a user and execute a script block as that user
param( [ScriptBlock] $scriptBlock )
<#
.SYNOPSIS
Impersonates a user and executes a script block as that user. This is an interactive script
and a window will open in order to securely capture credentials.
.EXAMPLE
Use-Impersonation.ps1 {Get-ChildItem 'C:\' | Foreach { Write-Host $_.Name }}
This writes the contents of 'C:\' impersonating the user that is entered.
#>
function Enable-FusionLog {
param($logPath = "C:\Temp\Fusion")
if(!(Test-Path $logPath)) {
New-Item -ItemType Directory -Path $logPath
}
$fusionRoot = "HKLM:Software\Microsoft\Fusion"
function Set-FusionKey($name, $value, $type) {
if(!(Test-Path (Join-Path $fusionRoot $name))) {
(New-ItemProperty $fusionRoot -name $name -propertyType $type -ErrorAction Stop) | Out-Null
@idavis
idavis / Jagged
Last active January 9, 2018 16:04
Matrix Multiplication Benchmarks
namespace MatrixMultiplication
{
public static class Jagged
{
public static double[][] Multiply( int N )
{
var C = new double[N][];
var A = new double[N][];
var B = new double[N][];
Util.Initialize( N, A, B, C );
@idavis
idavis / Regex.ps1
Created August 21, 2012 18:14
Ruby Style Regex Matching in Powershell
function =~ {
param([regex]$regex, [switch]$debug, [switch]$caseSensitive)
process {
$matches = $null
$mached = $false
if($caseSensitive) {
$matched = $_ -cmatch $regex
} else {
$matched = $_ -match $regex
}
@idavis
idavis / gist:ca8f67b1c1ab2f5d6fe7ab6f889a314a
Created July 30, 2016 02:51
Crypto Errors with .NET Command Line Tools for OS X
idavis$ brew link openssl --force
Warning: Refusing to link: openssl
Linking keg-only OpenSSL means you may end up linking against the insecure,
deprecated system version while using the headers from the Homebrew version.
Instead, pass the full include/library paths to your compiler e.g.:
-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib
=============================================================================
=============================================================================
@idavis
idavis / ReShape.ps1
Last active May 21, 2016 22:17
Implementing APL shape/rank (⍴/⍴⍴) functions in PowerShell. This is a rough first cut.
# APL Rank
filter ⍴⍴ {
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$false)] $ω
)
process {
if($ω -is [system.array]){
$ω.rank
} else{
"⍬"
@idavis
idavis / LateToTheParty.cs
Created February 15, 2012 18:46
True Late Binding
public class Foo {
public void Bar() {
if(IsBaz()) {
Console.WriteLine("CRAP");
} else {
Console.WriteLine("W00T");
}
}
public bool IsBaz() {
@idavis
idavis / sample1.ps1
Created July 27, 2012 21:49
PowerShell like JavaScript
$foo = @{
Message = "This is a pre-recorded message"
}
$foo.say = {
param([string]$message)
$speaker = new-object -com SAPI.SpVoice
($speaker.Speak($message, 1)) | out-null
}
@idavis
idavis / gist:4192723
Created December 3, 2012 04:36
Fresh clone of FubuMvc, trying to build
> git clone git@github.com:idavis/fubumvc.git
Cloning into 'fubumvc'...
remote: Counting objects: 57191, done.
remote: Compressing objects: 100% (11208/11208), done.
remote: Total 57191 (delta 45719), reused 56278 (delta 44979)
Receiving objects: 100% (57191/57191), 61.02 MiB | 327 KiB/s, done.
Resolving deltas: 100% (45719/45719), done.
> cd .\fubumvc
[master]> ls
@idavis
idavis / Fizz-Buzz.ps1
Created November 21, 2012 23:15
FizzBuzz with PowerShell
filter Fizz-Buzz {
process {
$output = switch($_) {
{$_ % 3 -eq 0} {"Fizz"}
{$_ % 5 -eq 0} {"Buzz"}
default {$_}
}
$output -join ""
}
}