Skip to content

Instantly share code, notes, and snippets.

View piers7's full-sized avatar

Piers Williams piers7

View GitHub Profile
@piers7
piers7 / gist:6374578
Created August 29, 2013 05:38
Create environment variables for common BI build prerequisites (helps target builds in TeamCity)
<#
.Synopsis
Adds/sets environment variables on the local machine for common build dependencies
#>
$ErrorActionPreference = 'stop';
$programFiles32 = $env:ProgramFiles
if (Test-Path environment::"ProgramFiles(x86)") { $programFiles32 = (gi "Env:ProgramFiles(x86)").Value };
# Accumulate the variables that we will be creating
$environmentVars = @{}
@piers7
piers7 / Load-TeamCityProperties.ps1
Created September 4, 2013 05:12
Load TeamCity system properties into PowerShell variable scope
<#
.Synopsis
Loads TeamCity system build properties into the current scope
Unless forced, doesn't do anything if not running under TeamCity
#>
param(
$prefix = 'TeamCity.',
$file = $env:TEAMCITY_BUILD_PROPERTIES_FILE + ".xml",
[switch] $inTeamCity = (![String]::IsNullOrEmpty($env:TEAMCITY_VERSION))
)
@piers7
piers7 / gist:91141f39715a2ec133e5
Last active August 29, 2015 14:21
Interpreting SQL server CDC LSNs in C# / .net
// https://gist.github.com/piers7/91141f39715a2ec133e5
// Example of how to interpret SQL server CDC LSNs in C# / .Net
// This is required when polling a server for updates in order to determine
// if a previously stored LSN is still valid (ie > min LSN available)
// Requires .Net 4 (or equivilent BigInteger implementation)
// Sample is a Linqpad script, but you get the idea
// NB: That SQL uses big-endian representation for it's LSNs is not
// (as best I know) something they guarantee not to change
@piers7
piers7 / LogInjectionModule
Last active January 6, 2016 03:01
Generic Autofac logging injector
using System;
using System.Linq;
using Autofac;
using Autofac.Core;
namespace Autofac.Logging
{
/// <summary>
/// Sets up automatic DI for service dependencies on <typeparamref name="TLogger"/>,
/// via an external factory that resolves based on the type of the resolver
@piers7
piers7 / gist:7632dcaf8066c4b03578
Created July 20, 2015 08:51
Recursive Projection in Linq
/// <summary>
/// Performs a projection on a heirachical sequence, without flattening
/// </summary>
/// <remarks>ie given an input type of X, each item of which has children which are also of type X,
/// execute a single selector across all X in the tree to retrieve a new target heirachy
/// </remarks>
public static IEnumerable<TOut> SelectRecurse<TIn,TOut>(IEnumerable<TIn> items, Func<TIn, IEnumerable<TIn>> childSelector, Func<TIn, IEnumerable<TOut>, TOut> selector){
foreach(var item in items){
var children = childSelector(item);
# .Synopsys
# Sets up Beyond Compare professional as Diff tool for all instances of Visual Studio on this PC
# If you don't use TFS, change the sccProvider as appropriate
[CmdLetBinding()]
param(
$bcPath = 'C:\Program Files (x86)\Beyond Compare 3\BComp.exe',
$sccProvider = 'TeamFoundation'
)
$ErrorActionPreference = 'stop';
@piers7
piers7 / Get-SqlServerDefaultDataPath.ps1
Created November 12, 2015 02:47
Uses SMO to determine the path that new databases will be created in
Enter file contents hereparam(
[Parameter(Mandatory=$true)] [string]$sqlServer
)
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") > $null
$smoServer = new-object Microsoft.SqlServer.Management.Smo.Server $sqlServer
$str = $smoServer.DefaultFile # or .DefaultLog for tlog files
if ($str)
{
:on error exit
:setvar databaseName OctopusDeploy
:setvar serviceAccount DOMAIN\ACCOUNT
/*
Creates a blank database suitable for use with Octopus Deploy
See http://docs.octopusdeploy.com/display/OD/SQL+Server+Database+Requirements
Important bits:
- must be CASE INSENSITIVE
- must setup Octopus service account as dbo_owner
:on error exit
:setvar databaseName TeamCity
:setvar serviceAccount DOMAIN\ACCOUNT
/*
Creates a blank database suitable for use with TeamCity
See https://confluence.jetbrains.com/display/TCD9/Setting+up+an+External+Database#SettingupanExternalDatabase-MicrosoftSQLServer
Important bit is to get the case-sensitive colation correct (_CS_AS),
which is required for non-Windows build agents (if ever required in future)
@piers7
piers7 / exec.ps1
Created November 18, 2015 01:06
PowerShell - reliably detect failure from external scripts
<#
.Synopsis
Detects failure from an external script has failed based on both $? and $LASTEXITCODE checking
Tested with a .net console app in 4 modes:
- no stderr, exit 0 (should be the only passing case)
- stderr, exit 0 ($? should indicate failure)
- no stderr, exit 1 ($LASTEXITCODE should indicate failure)
- stderr, exit 1 (both $? and $LASTEXITCODE should indicate failure)
See http://stackoverflow.com/questions/10666101/lastexitcode-0-but-false-in-powershell-redirecting-stderr-to-stdout-gives-n for background
#>