Friday, 30 September 2011

Get-FolderObjects.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the WMI objects associated with a folder 
  4. .DESCRIPTION 
  5.     This script uses the Associators Of query 
  6. .NOTES 
  7.     File Name  : get-folderobjects.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13. .EXAMPLE 
  14.     Get-FolderObjects "C:\Foo" 
  15.         what's in C:\foo 
  16.     0 Volume objects 
  17.     0 Logical Disk objects 
  18.     10 Directory objects 
  19.     171 File objects 
  20.     0 Pagefile objects 
  21.     1 Security Settings 
  22.     0 Share objects 
  23.     0 other objects 
  24. .EXAMPLE 
  25.     Get-FolderObjects "C:\" 
  26.       what's in C:\ 
  27.     1 Volume objects 
  28.     1 Logical Disk objects 
  29.     18 Directory objects 
  30.     8 File objects 
  31.     1 Pagefile objects 
  32.     1 Security Settings 
  33.     1 Share objects 
  34.     0 other objects   
  35. #> 
  36. # Function to get folder WMI object associations 
  37. # Define function 
  38. Function Get-FolderObjects { 
  39. Param ( 
  40. $Folder  =   "c:\" 
  41. ) 
  42.  
  43. # Set query then get associations 
  44. $query  = "ASSOCIATORS OF {Win32_Directory.Name='$folder'}" 
  45. $objs = Gwmi -q $query 
  46. #    
  47. # Now group them 
  48. # Create empty arrays 
  49. $directory = @() 
  50. $datafile  = @() 
  51. $pagefile  = @() 
  52. $volume    = @() 
  53. $filesec   = @() 
  54. $share     = @() 
  55. $Logdisk   = @() 
  56. $unknown   = @() 
  57.  
  58. # Now fill the arrays 
  59. foreach ($obj in $objs) { 
  60.     switch  ($Obj.__class) { 
  61.       "Win32_Directory"   {$directory += $obj; break} 
  62.       "Cim_DataFile"      {$datafile  += $obj; break} 
  63.       "Win32_PageFile"    {$pagefile  += $obj; break} 
  64.       "Win32_Volume"      {$volume    += $obj; break} 
  65.       "Win32_LogicalDisk" {$logdisk   += $obj; break}  
  66.       "Win32_LogicalFileSecuritySetting" {$filesec += $obj; break} 
  67.       "Win32_share"       {$share     += $obj; break} 
  68.       default             {$unknown   += $obj; break} 
  69.     } 
  70. } 
  71.  
  72. # Display the output 
  73. " what's in $folder" 
  74. "{0} Volume objects"        -f $volume.count 
  75. "{0} Logical Disk objects"  -f $logdisk.count 
  76. "{0} Directory objects"     -f $directory.count 
  77. "{0} File objects"          -f $datafile.count 
  78. "{0} Pagefile objects"      -f $pagefile.count 
  79. "{0} Security Settings"     -f $filesec.count 
  80. "{0} Share objects"         -f $share.count 
  81. "{0} other objects"         -f $unknown.count 
  82. } 
  83.  
  84. # Here call function as an example 
  85. Get-FolderObjects "C:\" 
  86. "***" 
  87. Get-FolderObjects "C:\foo" 

No comments: