Out-Clip

Security Briefs

Syndication

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
Filed under:

Comments

Paul-Marcel St-Onge wrote re: Out-Clip
on 12-07-2006 9:03 AM
I've gotten hooked on PowerShell as well; bit of a steep learning curve, but worth the result at the end.

I liked your out-clip cmdlet. I've been using one from a set of community extensions posted on CodePlex for awhile besides a few of my own domain-specific cmdlets. Here's the link to that set of community cmdlets:

http://www.codeplex.com/Wiki/View.aspx?ProjectName=PowerShellCX
Toma wrote re: Out-Clip
on 12-08-2006 1:28 AM
May be a little bit formatting of the code is needed?
Kris wrote re: Out-Clip
on 12-08-2006 4:51 PM
Yes please, could you format the code. It is a bit difficult to follow everthing in one line.
Keith Brown wrote re: Out-Clip
on 12-08-2006 5:35 PM
Eww, nasty! Sorry, my blog posting software apparently malfunctioned there. I didn't mean to post it looking like that. I'm going to go fix it now :)
Tech Talk Blog wrote Blog Clippings #4
on 12-10-2006 3:33 AM
Here they are: It looks like computers have finally got it over the humans with the defeat of Vladimir...