Hardware Inventory - FolderSize on a Client

This is a script + mof edit, might be helpful for people needing to find "how much space" is used by specific folders on clients.  fyi, it's not currently designed to look at 'my documents', which is what I've had people ask me for in the past.  I had this script + mof edit for a different, specifically named folder.  But it might be a starting point for someone.

 

Save the below as a .vbs.  What to add to sms_def.mof is below.

 

On Error Resume Next
'----------------------------------------
'Syntax:   FolderSize.vbs "Path to Folder" "Another Folder" "Another folder up to 50"
'   Example:   cscript.exe FolderSize.vbs "c:\temp" "d:\some folder with spaces" "c:\program files\Really Important"
'----------------------------------------
'Add the following to sms_def.mof (remove the ' comment at the beginning of each line!
'//  <:[-<>>>>>>>>>>>Start>>-Folder Size-<<Start<<<<<<<<<>-]:>
'//`'`*._.*`'`*-
'//  Folder Size Reporting Class
'//`'`*._.*`'`*-
'#pragma namespace("\\\\.\\root\\cimv2\\SMS")
'#pragma deleteclass("SCCM_FolderSize",NOFAIL)
'
'[ SMS_Report     (TRUE),
'  SMS_Group_Name ("FolderSize"),
'  SMS_Class_ID   ("CUSTOM|FolderSize|1.0") ]
'class SCCM_FolderSize : SMS_Class_Template
'{
'  [SMS_Report (TRUE), key ] string Path;
'  [SMS_Report (TRUE), SMS_Units("KiloBytes")] uint32 KB;
'  [SMS_Report (TRUE), SMS_Units("Megabytes")] Uint32 MB;
'  [SMS_Report (TRUE)      ] string ScriptLastRan;
'};
'//  <:[-<>>>>>>>>>>>END>>-Folder Size-<<END<<<<<<<<<>-]:>

'----------------------------------------
'Initially created by Sherry Kissinger 6/10/2009
'Logic of the Script
'1) Grab the folder names from the wscript arguments
'2) For each folder, gather the data into an array
'3) Create the WMI Namespaces
'4) Populate WMI with the array data
'-----------------------------------------
'****************************************
'1) Grab the folder names from the wscript arguments
If Wscript.Arguments.Count = 0 Then
 'nothing to do
 wscript.quit(0)
End If

CountFolders = Wscript.Arguments.Count

Dim strObjects(50,3)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set sho = Wscript.CreateObject("Wscript.Shell")
'--------------------------------------------
'2) For each folder, gather the data into an array
'--------------------------------------------
For i = 0 To CountFolders-1
 Err.Clear
 Set objFolder = objFSO.GetFolder(WScript.Arguments(i))
 If Err.Number = 0 Then
  strObjects(i,0) = objFolder.Path  'folder path, value 1 of 4
  strObjects(i,1) = FormatNumber((objFolder.Size/1024), 2) 'KB, value 2 of 4
  strObjects(i,2) = FormatNumber((objFolder.Size/1024)/1024, 2) 'MB, value 3 of 4
  strObjects(i,3) = Now  'DateScriptRan, value 4 of 4
 Else
  strObjects(i,0) = WScript.Arguments(i) & " : Path not found"
  strObjects(i,3) = Now  'DateScriptRan, value 4 of 4
 End if
Next
'----------------------------
'3) Create the WMI Namespaces
'----------------------------

Dim wbemCimtypeSint16
Dim wbemCimtypeSint32
Dim wbemCimtypeReal32
Dim wbemCimtypeReal64
Dim wbemCimtypeString
Dim wbemCimtypeBoolean
Dim wbemCimtypeObject
Dim wbemCimtypeSint8
Dim wbemCimtypeUint8
Dim wbemCimtypeUint16
Dim wbemCimtypeUint32
Dim wbemCimtypeSint64
Dim wbemCimtypeUint64
Dim wbemCimtypeDateTime
Dim wbemCimtypeReference
Dim wbemCimtypeChar16

wbemCimtypeSint16 = 2
wbemCimtypeSint32 = 3
wbemCimtypeReal32 = 4
wbemCimtypeReal64 = 5
wbemCimtypeString = 8
wbemCimtypeBoolean = 11
wbemCimtypeObject = 13
wbemCimtypeSint8 = 16
wbemCimtypeUint8 = 17
wbemCimtypeUint16 = 18
wbemCimtypeUint32 = 19
wbemCimtypeSint64 = 20
wbemCimtypeUint64 = 21
wbemCimtypeDateTime = 101
wbemCimtypeReference = 102
wbemCimtypeChar16 = 103
Set oLocation = CreateObject("WbemScripting.SWbemLocator")

'Remove classes
Set oServices = oLocation.ConnectServer(, "root\cimv2")
set oNewObject = oServices.Get("SCCM_FolderSize")
oNewObject.Delete_

Set oServices = oLocation.ConnectServer(, "root\cimv2\SMS")
set oNewObject = oServices.Get("SCCM_FolderSize")
oNewObject.Delete_

'Create data class structure
Set oServices = oLocation.ConnectServer(, "root\cimv2")
Set oDataObject = oServices.Get
oDataObject.Path_.Class = "SCCM_FolderSize"
oDataObject.Properties_.add "Path", wbemCimtypeString
oDataObject.Properties_.add "KB", wbemCimtypeUint32
oDataObject.Properties_.add "MB", wbemCimtypeUint32
oDataObject.Properties_.add "ScriptLastRan", wbemCimtypeString
oDataObject.Properties_("Path").Qualifiers_.add "key", True
oDataObject.Put_


'------------------------------
'Add Instances to data class
Set oServices = oLocation.ConnectServer(, "root\cimv2")

For i=0 To CountFolders-1
 Set oNewObject = oServices.Get("SCCM_FolderSize").SpawnInstance_
        oNewObject.Path = strObjects(i,0)
        oNewObject.KB = strObjects(i,1)
        oNewObject.MB = strObjects(i,2)
        oNewObject.ScriptLastRan = strObjects(i,3)
        oNewObject.Put_
'Uncomment the following 4 lines to for troubleshooting interactively
 WScript.Echo strObjects(i,0) & ", " &_
              strObjects(i,1) & ", " &_
              strObjects(i,2) & ", " &_
              strObjects(i,3)
Next


'Create reporting class structure
Set oServices = oLocation.ConnectServer(, "root\cimv2\SMS")
Set oRptObject = oServices.Get("SMS_Class_Template").SpawnDerivedClass_

'Set Class Name and Qualifiers
oRptObject.Path_.Class = "SCCM_FolderSize"
oRptObject.Qualifiers_.Add "SMS_Report", True
oRptObject.Qualifiers_.Add "SMS_Group_Name", "FolderSize"
oRptObject.Qualifiers_.Add "SMS_Class_ID", "Custom|FolderSize|1.0"
          
'Add Reporting Class Properties
oRptObject.Properties_.Add("Path", wbemCimtypeString).Qualifiers_.Add "SMS_Report", True
oRptObject.Properties_.Add("KB", wbemCimtypeUint32).Qualifiers_.Add "SMS_Report", True
oRptObject.Properties_.Add("MB", wbemCimtypeUint32).Qualifiers_.Add "SMS_Report", True
oRptObject.Properties_.Add("ScriptLastRan", wbemCimtypeString).Qualifiers_.Add "SMS_Report", True
oRptObject.Properties_("Path" ).Qualifiers_.Add "key", True
oRptObject.Put_

WScript.quit

Published Monday, November 23, 2009 4:50 AM by skissinger

Comments

No Comments