/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" MSH snap-in to Translate IADsLargeInteger to Int64... "," Commandline editing with MSH. "," MSH Scroll-host function "," Series about Monad on Computerworld "," Watching FSRM Quota and filescreen events from Mon... "," More ACL and MSH on MSH for Fun "," Jelle "," Monad and "Jeugd sentiment" "," Monad Beta 3 Docs changes "," Get SpecialFolder Locations in Monad "," ")

Sunday, January 22, 2006


Some fun With Monads Add-Member, MP3 files and COM

In Monad Beta 3 there is a new command,


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 :

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 Rock

Now 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....

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 :

Comments: Post a Comment

Links to this post:

Create a Link

<< Home


October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?