Skip to content

Instantly share code, notes, and snippets.

@flakshack
Created January 11, 2017 16:01
Show Gist options
  • Select an option

  • Save flakshack/06a5fdbe6006f206ff75ace12a44ff38 to your computer and use it in GitHub Desktop.

Select an option

Save flakshack/06a5fdbe6006f206ff75ace12a44ff38 to your computer and use it in GitHub Desktop.

Revisions

  1. flakshack created this gist Jan 11, 2017.
    43 changes: 43 additions & 0 deletions DiskUsedGB.vbs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    'On Error Resume Next
    CONST GB = 1073741824

    Dim oAPI, oBag
    Set oAPI = CreateObject("MOM.ScriptAPI")
    Set oArgs = WScript.Arguments
    drive = oArgs(0)

    strComputer = "."
    Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    ' If the drive designation is longer than 2 characters, it is a mount point.
    ' We have to use a different command to gather the size of a mount point.
    If len(drive) > 2 Then
    ' Append a \ on the end of the path if it is not there to match what Win32_Volume uses
    If right(drive, 1) <> "\" Then
    drive = drive & "\"
    End If
    'The WMI query uses backslashes for escapes so we have to duplicate each
    drive = replace(drive, "\", "\\")

    Set oVolumes = oWMI.ExecQuery ("SELECT * FROM Win32_Volume Where Name= '" & drive & "'")
    For Each oVolume In oVolumes
    diskSize = CDbl(oVolume.capacity)
    diskFree = CDbl(oVolume.freespace)
    diskUsedGB = round((diskSize - diskFree)/GB, 2)

    Next

    Else
    Set oDisks = oWMI.ExecQuery("Select * from Win32_LogicalDisk Where DeviceID= '" & drive & "'")
    For each oDisk in oDisks
    diskSize = CDbl(oDisk.size)
    diskFree = CDbl(oDisk.freespace)
    diskUsedGB = round((diskSize - diskFree)/GB, 2)
    Next

    End If

    ' Return the value to SCOM
    Set oBag = oAPI.CreatePropertyBag()
    Call oBag.AddValue("DiskUsed",diskUsedGB)
    Call oAPI.Return(oBag)