Skip to content

Instantly share code, notes, and snippets.

@wgross
wgross / powershell_context_concept.ps1
Last active October 30, 2018 09:37
POC for powershell delegateish scriptblock with context. Possible use as some kind of builder pattern
class Context {
$Data
}
filter require_context {
if( $_ -is [Context]) {
$_
} else {
throw "missing context"
}
@wgross
wgross / AnsiEscapeFormat.ps1
Created June 14, 2018 07:41
Format text with ansi sequences in powershell
# https://github.com/bozho/AnsiColorOut/blob/master/AnsiColorOut/Console.cs
# https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences
$esc=[char]0x001b
filter esc {
param(
[ValidateSet("underline","italic")]
[string[]]$Format
)
# https://en.wikipedia.org/wiki/Box-drawing_character
$lightBox = @{
vert = [char]::ConvertFromUtf32(9474)
horz = [char]::ConvertFromUtf32(9472)
leftup = [char]::ConvertFromUtf32(9484)
leftdown = [char]::ConvertFromUtf32(9492)
rightdown = [char]::ConvertFromUtf32(9496)
rightup = [char]::ConvertFromUtf32(9488)
}
@wgross
wgross / SimpleLazy.cs
Last active July 23, 2017 15:54
Most simple lazy implementation. (Contains a Linqad Main methods for verification)
void Main()
{
var lazy = new SimpleLazy<string>(() =>
{
"calculating".Dump();
return "hello";
});
lazy.Value.Dump("1");
lazy.Value.Dump("2");
@wgross
wgross / htmlGenerator.ps1
Created July 20, 2017 18:30
powershell html generator syntax experiment
using namespace System.Xml.Linq
@(
"html"
"head"
"body"
)|Foreach-Object -Process {
@"
function global:$_ {
[CmdletBinding()]
@wgross
wgross / Measure-Coverage.ps1
Last active February 21, 2017 19:07
Runs OpenCover and ReportGenerator on a dotnet core test project
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
[ValidateScript({Test-Path -Path $_})]
$ProjectToMeasure = $PWD, # Allows to pipe in project directiories
[Parameter()]
[switch]$ShowResult
)
begin {
@wgross
wgross / LoggerExtensions.cs
Created February 19, 2017 02:33
Adding Callerinfo to Serilog Context
public static class LoggerExtensions
{
public static ILogger WithCallerInfo(this ILogger logger,
[CallerMemberName]string callerMemberName = null,
[CallerFilePath]string callerFilePath = null,
[CallerLineNumber]int callerLineNumber = 0)
{
return logger
// substituting 'Caller' with 'Source' because 'ForContext<T>()' adds property 'SourceContext'
.ForContext("SourceMemberName", callerMemberName)
@wgross
wgross / Format-Json.ps1
Created January 27, 2017 13:57
Format-Json witthout Newtonsoft.Json
# Taken from: https://github.com/PowerShell/PowerShell/issues/2736
function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {
$indent = 0;
($json -Split '\n' |
% {
if ($_ -match '[\}\]]') {
# This line contains ] or }, decrement the indentation level
$indent--
}
$line = (' ' * $indent * 2) + $_.TrimStart().Replace(': ', ': ')
@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)]
@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