/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" YMCK "," Hosting Iron Python in MSH. "," MSH How can I Determine Which Text has been Select... "," Monad(MSH) Beta 3 is getting closer "," Monad Beta 3 is Coming soon and I got a MVP award "," Adding a Simple AccesRule to a file ACL in MSH "," Colorized MSH LS replacement. "," MSH GUI Viewer for Dataset Example "," More DataTables and joining them in Monad "," MSH Get-MSDN Function "," ")

Monday, January 09, 2006

 


Report MP3 count and size by User from MSH



This script will scan a local or remote disk for MP3 files, uses a Regex to extract the user from the Path, and than groups them by user to generate a Summary Report like this :


Report for MP3 files on .
Drive: G:


Number of MP3 files
688


Total Size (MB)
4745,23


Max size (MB)
142,51


MP3 Per User

User Count MB
---- ----- --
Usr001 141 998,52
Usr002 547 3746,62


the first part of the script (getting the list, and making the summary I did real quick)

an interesting is the convering to MB part, I'm using the .... (oops, how are they called) that MSH has defined for Binary Numbers, K = KiloByte etc., you can use them as a number.

MSH>1k
1024

MSH>1m
1048576

MSH>1g
1073741824


very handy.

but then I wanted to sort them by user, the username was in the path.
it was pretty easy to get them out using a regex but how to select on that.

Select-object lets you use expressions in the statement, but I got stuck here.

$mp | select { 
$_.name;($re.Match($_)).groups[1].value;[math]::round(($_.filesize / 1m) , 2
)}
MSH C:\WINDOWS\system32> $mp | select {
$_.name;($re.Match($_)).groups[1].value;[math]::round(($_.filesize / 1m) , 2
)}

$_.name;($re.Match($_)).groups[1].value;[math]::round(($_.filesize / 1m) , 2 )
------------------------------------------------------------------------------
{g:\userdata\home\User1\data\dikkie dik.mp3, User1, 2.06}


my properties where melted together and the values not realy usable anymore.
(I managed to fix the "melting" by splitting the scriptblock, but I already got a better solution : )

but again the NG quickly provided an answer, Keith Hill showed me the use of the "Expression=" - statement/structure to format the Calculated Properties.

so Now I can easy format and name the Calculated properties in Monad so the scriptproperties are much more usable after that.

I make a list with al the userdata needed for the Usage per user summary and place it in the $UserList with this method,

After that I group the $userfiles data by username, and then for every user I use the Group property (Created by the Group statement ) to create the Count and MB value again with the same method.

the final script looks like this :

$computer = "."
$drive = "G:"
$Extension = "MP3"

# RegExfilter for extracting Username from Path

$re = new regex("\\(usr.*?)\\")

# Seach the Files

$files = get-wmiobject -computer $computer cim_datafile -filter "Drive = '$drive' AND Extension = '$Extension'" 

# Summary 

"`nReport for $Extension files on $Computer`nDrive: $drive "
"`nNumber of $Extension files"
($files | Measure-Object ).count

"`nTotal Size (MB)"
[math]::round(($files | Measure-Object -property filesize -sum).sum / 1m , 2 )

"`nMax size (MB)"
[math]::round(($files | Measure-Object -property filesize -max).max / 1m , 2 ) 

# make list of MP3 with UserName extracted from path by Regex 

$Userfiles = $files | select @{expression={$_.filename}; name="Filename"},
             @{expression={[math]::round(($_.filesize / 1m) , 2 )}; name="RoundedSize"},
             @{expression={($re.Match($_)).groups[1].value}; name="UserName"}

# group them per user 

"`n$Extension Per User"
$Userfiles | group Username | select @{expression={$_.Name};Name="User"},
                               @{expression={($_.group | measure-object).count};Name="Count"},
                               @{expression={($_.group | measure-object -property RoundedSize -sum).sum};Name="MB"}


You can see how powerfull the usage of Select-Object, Measure-Object and Group-Object are Combined with the "Expression=" structure to name the generated properties.

enjoy,

Greetings /\/\o\/\/

Tags :


Comments:
Blogger Sung Meister
Did you write this script to monitor on which user in your company is downloading MP3's you like? :)

I have been wondering when you were going to blog that script. Great stuff~
 
Post a Comment



<< Home

Archives

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.
Google
 
Web mow001.blogspot.com

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