Where to find the System.Management.Automation assembly

Recent deployments of powershell don’t come with this assembly, which you need to write cmdlets in C#.

You need to install the powershell 2.0 sdk and look for:

$(ProgramFiles)Reference AssembliesMicrosoftWindowsPowerShellv1.0System.Management.Automation.dll

Here is a simple cmdlet:

using System.Management.Automation;

namespace SimpleGreeting
{
[Cmdlet(VerbsCommunications.Send, “Greeting”)]
public class SendGreetingCommand : Cmdlet
{
[Parameter(Mandatory=true)]
public string Name { get; set; }

protected override void ProcessRecord()
{
WriteObject(“Hello” + Name + “!”);
}
}

}

And here is the installer required:

using System.Management.Automation;
using System.ComponentModel;

namespace SimpleGreeting
{
[RunInstaller(true)]
public class RegisterSimpleGreeting : PSSnapIn
{
public override string Description
{
get { return “This is a demo snapin”; }
}

public override string Name
{
get { return “SendGreetingSnapin”; }
}

public override string Vendor
{
get { return “C.J Eyre Harlequin Design”; }
}
}
}

You need to use installutil to register the snapin.

Redirecting Output and Error Streams

This is the code needed to capture both the output and error streams from a console mode application.

using System;
using System.Text;
using System.Diagnostics;

namespace RedirectOutput
{
class Program
{
static StringBuilder errorBufffer = new StringBuilder();
static StringBuilder outputBufffer = new StringBuilder();

static void Main(string[] args)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.FileName = “cmd.exe”;
psi.Arguments = “/C dir”;
psi.WorkingDirectory = @”c:photos”;
using (Process p = new Process())
{
p.StartInfo = psi;
p.EnableRaisingEvents = true;
p.OutputDataReceived += p_OutputDataReceived;
p.ErrorDataReceived += p_ErrorDataReceived;

p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();
}
Console.WriteLine(“Error”);
Console.WriteLine(errorBufffer.ToString());
Console.WriteLine(“Output”);
Console.WriteLine(outputBufffer.ToString());

Console.ReadLine();
}

static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
outputBufffer.AppendLine(e.Data);
}

static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
errorBufffer.AppendLine(e.Data);
}
}
}

Mercurial HG

This is my new favorite version control system.

I personally like to develop under the cover of a version control system.  I don’t consider professional development to be occurring unless one is being used.

Hg is great for quick refactoring developments.  It’s a distributed vcs (like git) so that the application does not enforce a centralised server.

This means that you can commit multiple changes to your local repository before pushing these up to the shared server.

You can also push changesets from developer to developer without going via a central server!

This is wonderful for collaborative development or for those developers who code on the train or plane!

This neatly solves the problems that the PVCS promotion model handles (i.e. how to check in changes without breaking the tested build).

Since you can freely clone a repository refactoring becomes a breeze.

Clone then hack away (checking in whenever it actually compiles and runs the tests).  The cloned repository can be thrown away or merged back into the master (or just your current working copy).

Sample of NUnit.Mock

using NUnit.Framework;
using NUnit.Mocks;

namespace ConsoleApplication1
{
[TestFixture]
public class Class1
{
public interface IExecutable
{
void Execute();
}

[Test]
public void ThisIsATest()
{
// This is not a good test of a mock but it shows how it works.
// The mock should be a Dependend Upon Component not the Subject Of Test
DynamicMock mockExecute = new DynamicMock(typeof(IExecutable));
mockExecute.Expect(“Execute”);
((IExecutable)mockExecute.MockInstance).Execute();
mockExecute.Verify();
}
}
}