A while back I mentioned that there was a way of adding notes to objects in the shell. Here is one way to do it:
## Adds a note to the pipeline input.
filter AddNote([string] $name, $value)
$mshObj = [System.Management.Automation.MshObject] $_;
$note = new-object System.Management.AUtomation.MshNoteProperty $name, $value
As you can see I made it’s a filter that will add notes to any object that is piped through it.
The first line makes sure that the object is wrapped in an MshObject. As it turns out, for performance reasons, some objects aren’t immediately wrapped by an MshObject. I want to be sure that the object is wrapped by one so I cast it to MshObject. To tell you the truth, in the current build my filter seems to work fine without this line. However, there have been bugfixes around this so I’ll keep this line around to be sure that it works with the last public drop. The next line is self explanatory. It creates an MshNoteProperty object, passing the constructor the name of the note and the value of that note. Then we add that note to the MshObject, using that MshObject property I mentioned previously. And finally the object is returned. That way we can pipe an object through several AddNote filters to add more than one note.
Here is a relatively useless example of its use:
MSH>$process = gps a* | AddNote Owner Me | AddNote DateOfSnapshot ([Datetime]::Now)
MSH>$process | ft ProcessName, Owner, DateOfSnapshot
ProcessName Owner DateOfSnapshot
----------- ----- --------------
AdtAgent Me 9/7/2005 1:53:42 PM
alg Me 9/7/2005 1:53:42 PM
MSH>$a = "foobar"
MSH>$a = $a | AddNote CreatedBy Me
So… now you’ve seen how to add notes. Feel free to use this in your scripts. Soon I’ll post a script which makes much better use of notes.