Here's the code that I ended up with for OutClip:
using System;
using System.Text;
using System.Management.Automation;
using System.Windows.Forms;
using System.Threading;
[Cmdlet(VerbsData.Out, "Clip")]
public class OutClip : Cmdlet {
StringBuilder sb;
PSObject inputObject;
[Parameter(ValueFromPipeline=true)]
public PSObject InputObject {
get { return inputObject; }
set { inputObject = value; }
}
protected override void BeginProcessing() {
base.BeginProcessing();
sb = new StringBuilder();
}
protected override void ProcessRecord() {
base.ProcessRecord();
if (null != inputObject) {
// add a line break between objects, but not for the last object
// that should allow one-liners to come through
// without us adding a line-break, which is often inconvenient
if (sb.Length > 0) sb.AppendLine();
sb.Append(inputObject.BaseObject.ToString());
}
}
protected override void EndProcessing() {
base.EndProcessing();
Thread thread = new Thread(delegate() {
Clipboard.SetDataObject(sb.ToString(), true);
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
}
And here's the code for the snapin/installer
using System;
using System.Management.Automation;
using System.ComponentModel;
[RunInstaller(true)]
public class MySnapIn : PSSnapIn {
public override string Name {
get { return "KeithsUtilities"; }
}
public override string Vendor {
get { return "Pluralsight LLC"; }
}
public override string Description {
get { return "A set of PowerShell cmdlets written by Keith Brown"; }
}
}
Edit (9 Dec 2006): Fixed blog formatting and updated the code a little bit.
Posted
Dec 07 2006, 01:48 AM
by
keith-brown