Const BYTES_IN_MB = 1048576 '=2^20
Const LOCAL = 3
Call Main
Sub Main()
Dim THRESHOLD_PCT_RED
Dim THRESHOLD_PCT_YELLOW
Dim THRESHOLD_SYSTEM_PCT_RED
Dim THRESHOLD_SYSTEM_PCT_YELLOW
Dim THRESHOLD_MB_RED
Dim THRESHOLD_MB_YELLOW
Dim THRESHOLD_SYSTEM_MB_RED
Dim THRESHOLD_SYSTEM_MB_YELLOW
Dim DiskLabel
Dim oArgs
Set oArgs = WScript.Arguments
if oArgs.Count <> 10 Then
WScript.Quit
End If
Dim oAPI, oBag
Set oAPI = MOMCreateObject("MOM.ScriptAPI")
If Err.number <> 0 Or IsNull(oAPI) or IsEmpty(oAPI) Then
ThrowScriptError "Error While Creating ScriptAPI object", Err
Exit Sub
End If
set oBag = oAPI.CreatePropertyBag()
If Err.number <> 0 Or IsNull(oBag) or IsEmpty(oBag) Then
ThrowScriptError "Error While Creating PropertyBag object", Err
Exit Sub
End If
If IsVolumeInfoSupported Then
Set oWmiDiskSet = WMIGetInstance("winmgmts:\\" + TargetComputer & "\root\cimv2", "Win32_Volume")
Else
Set oWmiDiskSet = WMIExecQuery("winmgmts:\\" + TargetComputer & "\root\cimv2", "select * from Win32_LogicalDisk where (DriveType=3 or DriveType=6) and FileSystem!=null")
End If
If IsObject(oWmiDiskSet) Then
For Each oWmiDisk in oWmiDiskSet
'WScript.echo "Disk is " & oWmiDisk.DriveLetter
If oWmiDisk.DriveType = LOCAL Then
Dim sDriveLetter, nFreeSpace, nMaxSize, nPctFree, nMBFree
' Calculate Free Space
nFreeSpace = oWmiDisk.FreeSpace
'WScript.echo "Disk is " & oWmiDisk.DriveLetter & "Free Space is " & nFreeSpace
If IsNull(nFreeSpace) Then _
nFreeSpace = 0
If IsVolumeInfoSupported Then
sDriveLetter = oWmiDisk.DriveLetter
nMaxSize = oWmiDisk.Capacity
If IsNull(sDriveLetter) Then
sDriveLetter = oWmiDisk.Name
sDriveLetter = Left(sDriveLetter, Len(sDriveLetter)-1)
End If
Else
sDriveLetter = oWmiDisk.DeviceId
nMaxSize = oWmiDisk.Size
End If
If StrComp(DiskLabel,sDriveLetter) = 0 Then
If Not IsNull(nMaxSize) And nMaxSize > 0 Then
'
' Drive is formatted - if we dont get a maxsize, dont report on drive
'
nPctFree = Round(nFreeSpace / nMaxSize * 100, 0)
nMBFree = Round(nFreeSpace / BYTES_IN_MB, 0)
If nPctFree < CInt(THRESHOLD_SYSTEM_PCT_RED) And nMBFree < CLng(THRESHOLD_SYSTEM_MB_RED) And Is_System_Drive(sDriveLetter) Then
oBag.AddValue "State", "BAD"
ElseIf nPctFree < CInt(THRESHOLD_SYSTEM_PCT_YELLOW) And nMBFree < CLng(THRESHOLD_SYSTEM_MB_YELLOW) And Is_System_Drive(sDriveLetter) Then
oBag.AddValue "State", "WARNING"
ElseIf nPctFree < CInt(THRESHOLD_PCT_RED) And nMBFree < CLng(THRESHOLD_MB_RED) And Not Is_System_Drive(sDriveLetter) Then
oBag.AddValue "State", "BAD"
ElseIf nPctFree < CInt(THRESHOLD_PCT_YELLOW) And nMBFree < CLng(THRESHOLD_MB_Yellow) And Not Is_System_Drive(sDriveLetter) Then
oBag.AddValue "State", "WARNING"
Else
oBag.AddValue "State", "OK"
End If
oBag.AddValue "PctFree", nPctFree & ""
oBag.AddValue "MBFree", nMBFree & ""
oAPI.AddItem oBag
oAPI.ReturnItems
Exit Sub
Else
'
' If the MaxSize is 0 or less, then the drive is unformatted.
' Do not report on unformatted drives
'
WScript.echo "Drive not formatted " & sDriveLetter
ThrowScriptError " * " & sDriveLetter & " + Drive not formatted." , Err
Exit Sub
End If ' Is Null Check
End If ' Lable Check
End If ' Drive Type
Next
End If
End Sub
Function ThrowScriptErrorNoAbort(ByVal sMessage, ByVal oErr)
'
' ThrowScriptError :: Creates an event and sends it back to the mom server
'
'
Dim sErrDescription, sErrNumber
sErrDescription = oErr.Description
sErrNumber = oErr.Number
Wscript.echo "Error Message is " & sErrDescription & " Error number is " & sErrNumber
End Function
Function ThrowScriptError(Byval sMessage, ByVal oErr)
'
' ThrowScriptError :: Creates an event and sends it back to the mom server
'
'
On Error Resume Next
ThrowScriptErrorNoAbort sMessage, oErr
Quit()
End Function
Function WMIGetObject(ByVal sNamespace)
'
' WMIGetObject :: Returns the WMI object requested.
'
'
Dim oWMI
Dim e
Set e = New Error
On Error Resume Next
Set oWMI = GetObject(sNamespace)
e.Save
On Error Goto 0
If IsEmpty(oWMI) Then
ThrowScriptError "Unable to open WMI Namespace '" & sNamespace & "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", e
End If
Set WMIGetObject = oWMI
End Function
Function WMIGetInstance(ByVal sNamespace, ByVal sInstance)
'
' WMIGetInstance :: Returns WMI Instance requested.
'
'
Dim oWMI, oInstance, nInstanceCount
Dim e
Set e = New Error
On Error Resume Next
Set oWMI = GetObject(sNamespace)
e.Save
On Error Goto 0
If IsEmpty(oWMI) Then
ThrowScriptError "Unable to open WMI Namespace '" & sNamespace & "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", e
End If
On Error Resume Next
Set oInstance = oWMI.InstancesOf(sInstance)
e.Save
On Error Goto 0
If IsEmpty(oInstance) Or e.Number <> 0 Then
ThrowScriptError "The class name '" & sInstance & "' returned no instances. Please check to see if this is a valid WMI class name.", e
End If
'Determine if we queried a valid WMI class - Count will return 0 or empty
On Error Resume Next
nInstanceCount = oInstance.Count
e.Save
On Error Goto 0
If e.Number <> 0 Then
ThrowScriptError "The class name '" & sInstance & "' did not return any valid instances. Please check to see if this is a valid WMI class name.", e
End If
Set WMIGetInstance = oInstance
End Function
Class Error
Private m_lNumber
Private m_sSource
Private m_sDescription
Private m_sHelpContext
Private m_sHelpFile
Public Sub Save()
m_lNumber = Err.number
m_sSource = Err.Source
m_sDescription = Err.Description
m_sHelpContext = Err.HelpContext
m_sHelpFile = Err.helpfile
End Sub
Public Sub Raise()
Err.Raise m_lNumber, m_sSource, m_sDescription, m_sHelpFile, m_sHelpContext
End Sub
Public Sub Clear()
m_lNumber = 0
m_sSource = ""
m_sDescription = ""
m_sHelpContext = ""
m_sHelpFile = ""
End Sub
Public Default Property Get Number()
Number = m_lNumber
End Property
Public Property Get Source()
Source = m_sSource
End Property
Public Property Get Description()
Description = m_sDescription
End Property
Public Property Get HelpContext()
HelpContext = m_sHelpContext
End Property
Public Property Get HelpFile()
HelpFile = m_sHelpFile
End Property
End Class
Function MomCreateObject(ByVal sProgramId)
Dim oError
Set oError = New Error
On Error Resume Next
Set MomCreateObject = CreateObject(sProgramId)
oError.Save
On Error Goto 0
If oError.Number <> 0 Then ThrowScriptError "Unable to create automation object '" & sProgramId & "'", oError
End Function
Function IsValidObject(ByVal oObject)
IsValidObject = False
If IsObject(oObject) Then
If Not oObject Is Nothing Then
IsValidObject = True
End If
End If
End Function
Function WMIExecQuery(ByVal sNamespace, ByVal sQuery)
'
' WMIExecQuery :: Executes the WMI query and returns the result set.
'
'
Dim oWMI, oQuery, nInstanceCount
Dim e
Set e = New Error
On Error Resume Next
Set oWMI = GetObject(sNamespace)
e.Save
On Error Goto 0
If IsEmpty(oWMI) Then
ThrowScriptError "Unable to open WMI Namespace '" & sNamespace & "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", e
End If
On Error Resume Next
Set oQuery = oWMI.ExecQuery(sQuery)
e.Save
On Error Goto 0
If IsEmpty(oQuery) Or e.Number <> 0 Then
ThrowScriptError "The Query '" & sQuery & "' returned an invalid result set. Please check to see if this is a valid WMI Query.", e
End If
'Determine if we queried a valid WMI class - Count will return 0 or empty
On Error Resume Next
nInstanceCount = oQuery.Count
e.Save
On Error Goto 0
If e.Number <> 0 Then
ThrowScriptError "The Query '" & sQuery & "' did not return any valid instances. Please check to see if this is a valid WMI Query.", e
End If
Set WMIExecQuery = oQuery
End Function
Function Is_Win32_Volume_Supported()
Dim objWMISet, objWMIOS, blnRet
blnRet = False
Set objWMISet = WMIGetInstance("winmgmts:\\" & TargetComputer & "\root\cimv2", "Win32_OperatingSystem")
For each objWMIOS in objWMISet
If CLng(objWMIOS.BuildNumber) >= 3624 Then blnRet = True
Next
Is_Win32_Volume_Supported = blnRet
End Function
Function Is_System_Drive(sDriveLetter)
Dim objWMISet, objWMIOS
Is_System_Drive = False
Set objWMISet = WMIGetInstance("winmgmts:\\" & TargetComputer & "\root\cimv2", "Win32_OperatingSystem")
For each objWMIOS in objWMISet
Dim sSystemDrive
sSystemDrive = Left(objWMIOS.SystemDirectory, 2)
If sSystemDrive = sDriveLetter Then
Is_System_Drive = True
End If
Next
End Function</Script></Contents>
<Unicode>1</Unicode>
</File>
</Files>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
</DataSourceModuleType>