Skip to content

Instantly share code, notes, and snippets.

@wgross
wgross / SystemDateTimeExtensions.cs
Last active January 23, 2017 07:16
Some useful extensions of System.DateTime
namespace System
{
public static class DateTimeExtensions
{
public static string ToIso8601String(this DateTime thisDateTime)
{
return thisDateTime.ToString("yyyy-MM-ddTHH:mm:ss.fffK");
}
public static string ToIso8601String(this DateTimeOffset thisDateTime)
@wgross
wgross / New-ProxyCommand.ps1
Created June 24, 2015 10:06
Creates a piece of powershell code wrapping a call to an exsisting cmdlet. The original cmdlets parameters are provided by the proxy cmdlet based on the metedata of the existing cmdlet.
param(
$CommandName
)
[System.Management.Automation.ProxyCommand]::Create(
(New-Object System.Management.Automation.CommandMetaData (Get-Command $CommandName)))
// add reference to project:
// <Reference Include="Microsoft.PowerShell.ConsoleHost"/>
// <Reference Include="System.Management.Automation"/>
class Program
{
private static void Main(string[] args)
{
ConsoleShell.Start(RunspaceConfiguration.Create(),bannerText:"Powershell Host",helpText:string.Empty, args:args);
}
}
function elevated {
<#
.SYNOPSIS
Runs a scriptblock in an elevated shell
.DESCRIPTION
A given script block is executed in a an elevated powershell, if the current host isn't elevated.
The elevated shell location is moved to the current directory. A profiole isn't loaded for the
elevated shell because of performance reasons.
The output of the shell is captured and exported as CLI-Xml to a temorary file and imported by the
original shell after the the elevated process ended.
<#
.SYNOPSIS
Make a download with webclient using the DefaultNetworkCredentials to authenticate at proxy.
Afterwards all new webclient based cmdlets can pass the proxy.
.DESCRIPTION
Make a download with webclient using the DefaultNetworkCredentials to authenticate at proxy.
Afterwards all new webclient based cmdlets can pass the proxy.
.EXAMPLE
@wgross
wgross / Memoize.cs
Created June 6, 2014 08:23
Memoization of delegates calculation result using strong or weak references. Extension to multiple parameters with partial application. The memoization container can be changed by implementing a custom memoization container factory.
using System;
using System.Collections.Generic;
namespace Memoization
{
public static class Memoize
{
#region Create memoization containers
public interface IContainerFactory
@wgross
wgross / SimplePowershellHost.cs
Created May 9, 2014 08:00
Create a simple Powershell Console Host
// from c:/Program Files (x86)/ReferenceAssemblies/Microsoft/WindowsPowershell/Microsft.PowerShell.ConsoleHost.Dll
using Microsoft.PowerShell;
// from c:/Program Files (x86)/ReferenceAssemblies/Microsoft/WindowsPowershell/System.Management.Automation.Dll
using System.Management.Automation;
using System.Management.Automation.Runspaces;
class Program
{
static void Main(string[] args)
{
@wgross
wgross / ScopeGuard.cs
Last active January 23, 2017 06:48
Provides a code section where a specified piece of code is executed on leave (and enter optionally)
public sealed class ScopeGuard : IDisposable
{
#region Construction and initialization of this instance
public static ScopeGuard WithOnLeave(Action onLeave)
{
return new ScopeGuard(onLeave);
}
private ScopeGuard(Action onLeave)