Skip to content

Instantly share code, notes, and snippets.

@xcud
xcud / Get-Rights.ps1
Last active August 29, 2015 14:09
Get-Rights
$source = @"
using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Collections.Generic;
public static class ADPermissions {
public static string[] EnumRights()
{
@xcud
xcud / IsImage
Created January 29, 2015 06:55
Quickly determine whether a given path is an image
public static bool IsImage(string path)
{
Dictionary<string, string[]> knownImageHeaders = new Dictionary<string, string[]>()
{
{"jpg", new [] { "FF", "D8" } },
{"bmp", new [] { "42", "4D" } },
{"gif", new [] { "47", "49", "46" } },
{"tif", new [] { "49", "49", "2A" } },
{"png", new [] { "89", "50", "4E", "47", "0D", "0A", "1A", "0A" } },
};
@xcud
xcud / Load-Assembly.ps1
Created January 31, 2013 15:30
Pipeable shortcut for [reflection.assembly]::LoadFrom
<#
.Synopsis
Pipeable shortcut for [reflection.assembly]::LoadFrom.
.Example
PS> dir c:\foo.dll | Load-Assembly
GAC Version Location
--- ------- --------
False v2.0.50727 C:\foo.dll
@xcud
xcud / Get-NetworkStatistics.ps1
Last active December 12, 2015 03:49
Powershellized netstat
<#
.Synopsis
PowerShellized netstat. Building on Shay Levy's work
Shay Levy's Blog => http://blogs.microsoft.co.il/blogs/scriptfanatic/
.Example
PS> Get-NetworkStatistics skype | ft -AutoSize
Protocol LocalAddress LocalPort RemoteAddress RemotePort State ProcessName PID
-------- ------------ --------- ------------- ---------- ----- ----------- ---
@xcud
xcud / ParsedArgs.cs
Last active December 12, 2015 04:28
Simple string[] args => Dictionary<string, object> parser
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Xcud
{
/// <summary>
/// Simple command line argument parser.
@xcud
xcud / SimpleGAC.cs
Created February 5, 2013 12:58
A simple wrapper for GAC.cs from Microsoft KB Article KB317540
// System.GAC (a .Net wrapper for Fusion) implementation was too messy. Simplified here.
//
// Note, installing an assembly with the REFRESH flag specified does NOT work as advertised.
// Overwriting old GAC'd assemblies must be done manually as implemented below.
//
using System.Reflection;
using System.Text;
namespace Xcud.System.GAC
@xcud
xcud / Out-PDF.ps1
Created February 7, 2013 21:41
Writes a string to a PDF via Microsoft Word automation
<#
.Synopsis
Writes a string to a PDF via Microsoft Word automation
.Example
PS> ps | out-string | out-pdf -Path ".\processes.pdf"
#>
function Out-PDF() {
param(
[Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$true)] $Text,
@xcud
xcud / Get-Content.ps1
Created February 14, 2013 19:15
Extend Get-Content to include a -Fast parameter; much less flexible but much more performant
<#
.Synopsis
Extend Get-Content to include a -Fast parameter; much less flexible but more performant
.Example
PS> (Measure-Command { Get-Content .\data.xml} ).TotalMilliseconds
1609.3267
PS> (Measure-Command { Get-Content .\data.xml -Fast} ).TotalMilliseconds
39.2511
#>
@xcud
xcud / Implement-Verbose.ps1
Last active December 13, 2015 19:18
Demonstrate built-in Verbose support
<#
.Synopsis
Demonstrate built-in Verbose support
.Example
PS> Correct-Example -Verbose
VERBOSE: This works
PS> Incorrect-Example -Verbose
PS> Incorrect-Example2 -Verbose
@xcud
xcud / DynamicWebServiceProxy.cs
Created February 22, 2013 16:10
I'm spoiled by PowerShell's New-WebServiceProxy. I want to poke around at a WCF but I don't want to precompile a WSDL, or add a service reference, or include a contract interface that'll break on compile when it changes. Let me just interact with the web services reflectively like I do in PowerShell. The performance profile is terrible. It does …
using System;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Reflection;
namespace Xcud.Net
{
/// <example>
/// DynamicWebServiceProxy proxy = new DynamicWebServiceProxy(uri);