This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
In Monad Beta 3 there is a new command,
Add-Member
This command lets you add you Custom Members to an MSHObject (the wrapper MSH uses).
It works a bit like adding ScriptMethods and Properties in the Typedata (See :
Update-TypeData (Democracy to the types)), but now you can do this on-the-fly.
Also it's a bit like using the Select-Object statement to make Custom Properties, I used before here :
Report MP3 count and size by User from MSH , I going to use the Same Data (A list of MP3 files on a Disk) to show some Examples of Add-Member to get some more info from them as Artist, Title and add a Play Method.
First lets get the list :
$computer = "."
$drive = "G:"
$Extension = "MP3"
# get the MP3 Files
$files = get-wmiobject -computer $computer cim_datafile -filter "Drive = '$drive' AND Extension = '$Extension'"
MSH>$files | ft
Compressed Encrypted Size Hidden Name Readable System File Version Writeable
---------- --------- ---- ------ ---- -------- ----------- ------- ---------
False False False g:\monad\74q... True True
False False False g:\mp3\80-12... True True
Now we have a list of MP3 files, but I do not like the Standard Output.
For MP3 files I would like Other Properties,
like Author, that the standard output does not provide but as I know that the Shell.Application COM-object can provide this Info I can Add it with Add-Member using a scriptProperty, like this :
# Add a Script Property
$files | add-member -Type scriptProperty "Author" {
$shell = new-object -com shell.application
$Folder = $shell.Namespace($this.drive + $this.path)
$file = $Folder.Items().Item($this.filename + "." + $this.extension)
$Property = $Folder.GetDetailsOf($file,9)
return $Property
} -force
Now I added that ScriptProperty and can do like this :
MSH>$files[50].author
Dire Straits
Now every time I ask for the Author scriptproperty like that, the scriptMethod gets called, and gets the Author.
I would like to add some more properties, but would not like every time I check them the Script will run, So I will add some more properties but now I use a NoteProperty, that I fill only one time with the script(I add an ID too, you see why later ;-)).
# Add some More Properties, but use NoteProperties this Time
$shell = new-object -com shell.application
$i = 0
foreach ($file in $files) {
add-member -in $file -type noteProperty -name "ID" $i -force
foreach ($prop in (@{Title = 10;Artist = 9;AlbumTitle = 17}).getEnumerator()) {
$oFolder = $shell.Namespace($file.drive + $file.path)
$oFile = $oFolder.Items().Item($file.filename + "." + $file.extension)
$Property = $oFolder.GetDetailsOf($oFile,$script:prop.Value)
add-member -in $file -type noteProperty -name $prop.key $Property -force
}
$i += 1
}
Now I added some more Properties, If you do a get-member you can see that,
but We can make it even more Handy we can group our properties in a propertyset.
# Make a PropertySet from them
$files | add-member --type PropertySet "Mp3Info" ([string[]]("ID","Title","Artist","FileType","AlbumTitle","Path")) -force
MSH>$files[50] | select mp3info
ID : 50
Title : Skateaway
Artist : Dire Straits
FileType : MP3 Format Sound
AlbumTitle : Making Movies
Path : \mp3\dire straits\dire straits (1980) - making movies\
Nice or not ?,
but Wait we are not ready yet, We can also add ScriptMethods, so to make it even more handy, let's Add this :
# Add The Play Method
$files | add-member -Type scriptMethod "Play" {$WMP = new -com "WMPlayer.OCX";$wmp.openplayer($this.name)}
Now we can Also call the PLay() method on the MP3 file to play it in Windows Media PLayer.
# play SurfCity from the GhostTones
($files | where {$_.artist -eq "The Ghosttones" -and $_.Title -eq "Surf City"}).play()
Cool or not ?,
this just plays 1 file, but as you can also make playlist etc from here, for more info about using the windows MediaPlayer here :
Monad Really Does RockNow you can also see why I did add the ID value :
MSH>$files | where {$_.artist -like "*Brood*"} | ft mp3info
ID Title Artist FileType AlbumTitle Path
-- ----- ------ -------- ---------- ----
623 Never Be Clever Herman Brood MP3 Format Sound Nederpop Top 100 Go... \MP3\NL\Neder....
647 Still Believe Herman Brood & His ... MP3 Format Sound Nederpop Top 100 Go... \MP3\NL\Neder....
666 Saturday Night Herman Brood & His ... MP3 Format Sound Nederpop Top 100 Go... \MP3\NL\Neder....
MSH>$files[623].play()
You can see Add-Member is a very Powerfull way to extend Objects in MSH.
And remember if you want them permanent you can also use the MSHXML files and update-typedata.
gr /\/\o\/\/
Tags : Monad msh