Automating the world one-liner at a time…
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.
[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]
Where are the notes stored? On the disk? Attached to each file?
They're attached to an object instance and are stored in memory until that object is garbage-collected. So - if you attach a note to a FileInfo object, it's attached to that object, not to the actual file on disk.
Windows PowerShell Technical Lead
I try to run this and get the following:
PS [LDNDWM322539] >$foo = dir .\test.ps1 | AttachMD5
Unable to find type [System.Management.Automation.MshObject]: make sure that the assembly containing this type is loaded.
At line:3 char:51
+ $mshObj = [System.Management.Automation.MshObject] <<<< $_;
New-Object : Cannot find type [System.Management.AUtomation.MshNoteProperty]: make sure the assembly containing this type is load
At line:4 char:19
+ $note = new-object <<<< System.Management.AUtomation.MshNoteProperty $name, $value
You cannot call a method on a null-valued expression.
At line:5 char:30
+ $mshObj.MshObject.Members.Add( <<<< $note);
I've try to [System.Reflection.Assembly]::LoadWithPartialName("System.Management.Automation.MshObject")
with no luck.
Any help would be great.