Skip to content

Instantly share code, notes, and snippets.

@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)
@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 / 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
<#
.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
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.
// 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);
}
}
@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)))
@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 / LinqXml.psm1
Last active February 19, 2016 13:03
Powershell functions to create some of the Linq2Xml classes like Xdocument, XElement, XNamespace, XName, XAttribute etc...
function XName {
[CmdletBinding()]
param(
[Parameter(Mandatory,Position=0)]
[ValidateNotNullOrEmpty()]
[string]$LocalName
)
process {
[System.Xml.Linq.XName]$LocalName
@wgross
wgross / Enable-Ansi.ps1
Last active August 24, 2017 11:45
Get current weather from wttr.in in powershell. These scripts are based in the post at http://my-devnull.de/tag/wttr-in/.
# To enable ANSI sequences in a PowerShell console run the following commands.
# After that you can use wttr.in in you PowerShell just lake that:
# (curl http://wttr.in/ -UserAgent "curl" ).Content
#
# More on it:
# http://stknohg.hatenablog.jp/entry/2016/02/22/195644 (jp)
#
Add-Type -Namespace Win32 -Name NativeMethods -ErrorAction SilentlyContinue -MemberDefinition @"
[DllImport("kernel32.dll", SetLastError=true)]