File Count Probe

Microsoft.Amalga.Library.FileCountProbe (ProbeActionModuleType)

finds the count of files for a folder, must supply the path as an absolute path or as an HKLM key that looks up the path

Element properties:

TypeProbeActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
PASSTHROUGH ProbeAction System.PassThroughProbe Default
ScriptPropBag ProbeAction Microsoft.Windows.ScriptPropertyBagProbe Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
ThresholdValueint$Config/ThresholdValue$Threshold Value
StateWhenFolderMissingstring$Config/StateWhenFolderMissing$State when folder is missing
DebugEnabledbool$Config/DebugEnabled$Log EnabledFor debugging management pack script issues. Not recommended for general use.

Source Code:

<ProbeActionModuleType ID="Microsoft.Amalga.Library.FileCountProbe" Accessibility="Public" Batching="false" PassThrough="false">
<Configuration>
<xsd:element minOccurs="1" name="FilePath" type="xsd:string"/>
<xsd:element minOccurs="1" name="PathIsARegKey" type="xsd:boolean"/>
<xsd:element minOccurs="1" name="ThresholdValue" type="xsd:integer"/>
<xsd:element minOccurs="1" name="StateWhenFolderMissing" type="xsd:string"/>
<xsd:element minOccurs="1" name="DebugEnabled" type="xsd:boolean"/>
<xsd:element minOccurs="1" name="SubFolder" type="xsd:string"/>
<xsd:element minOccurs="1" name="DebugEventSource" type="xsd:string"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="ThresholdValue" Selector="$Config/ThresholdValue$" ParameterType="int"/>
<OverrideableParameter ID="StateWhenFolderMissing" Selector="$Config/StateWhenFolderMissing$" ParameterType="string"/>
<OverrideableParameter ID="DebugEnabled" Selector="$Config/DebugEnabled$" ParameterType="bool"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<ProbeAction ID="PASSTHROUGH" TypeID="System!System.PassThroughProbe"/>
<ProbeAction ID="ScriptPropBag" TypeID="Windows!Microsoft.Windows.ScriptPropertyBagProbe">
<ScriptName>AmalgaGetFileCount.vbs</ScriptName>
<Arguments>"$Target/Id$"</Arguments>
<ScriptBody><Script>
''' AmalgaGetFileCount.vbs
Option Explicit
SetLocale("en-us")
const SCRIPT_VERSION = "1.0"
dim WRITELINEHEADER
WRITELINEHEADER = "AmalgaGetFileCount.vbs" &amp; vbcrlf &amp; "Microsoft.Amalga.Library.FileCountProbe" &amp; vbcrlf &amp; "script version: " &amp; SCRIPT_VERSION &amp; vbcrlf &amp; "script start: " &amp; now()

const HKEY_LOCAL_MACHINE = &amp;H80000002

const EXPECTED_PARAM_COUNT = 1

const LOGSEVERITY_INFO = 4
const LOGSEVERITY_WARN = 2
const LOGSEVERITY_ERROR = 1

dim LogEnabled
LogEnabled = false
dim ManagedEntityId
ManagedEntityId = ""

Dim oAPI, oBag
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()

dim DebugEventSource
dim LogEventSource
dim LogSeverity
LogSeverity = LOGSEVERITY_INFO

DebugEventSource = "$Config/DebugEventSource$"
if instr(DebugEventSource,"$") then
LogEventSource = "Amalga"
LogEnabled = true
LogSeverity = LOGSEVERITY_ERROR
WriteLine "Error in DebugEventSource - $ not translated - " &amp; DebugEventSource
cdate("unexpected error - macro subsitution did not happen") ''' intentionally crash
else
LogEventSource = trim(DebugEventSource &amp; "")
if (LogEventSource = "") then
LogEventSource = "Amalga"
LogEnabled = true
LogSeverity = LOGSEVERITY_WARN
WriteLine "missing DebugEventSource, defaulting to 'Amalga'"
LogEnabled = false
LogSeverity = LOGSEVERITY_INFO
end if
end if

Dim oArgs
Set oArgs = WScript.Arguments
DumpArgs(oArgs)

if (oArgs.Count &lt;&gt; EXPECTED_PARAM_COUNT) then
LogEnabled = true
LogSeverity = LOGSEVERITY_ERROR
WriteLine "invalid arg count: " &amp; oArgs.Count &amp; " expected " &amp; EXPECTED_PARAM_COUNT
oArgs = cdate("invalid arg count") ''' intentionally crash
end if


ManagedEntityId = GetArg(oArgs(0))


dim DebugEnabled
DebugEnabled = "$Config/DebugEnabled$"
if instr(DebugEnabled,"$") then
LogEnabled = true
LogSeverity = LOGSEVERITY_ERROR
WriteLine "Error in DebugEnabled - $ not translated - " &amp; DebugEnabled
cdate("unexpected error - macro subsitution did not happen") ''' intentionally crash
end if
DebugEnabled = trim(ucase(DebugEnabled &amp; " "))
LogEnabled = (DebugEnabled = "TRUE")


dim FolderPath, UseReg, ThresholdValue, SubFolder

FolderPath = "$Config/FilePath$"
if instr(FolderPath,"$") then
cdate("unexpected error - macro subsitution did not happen")
end if
WriteLine "FolderPath: " &amp; FolderPath
FolderPath = lcase(FolderPath &amp; "")

UseReg = "$Config/PathIsARegKey$"
if instr(UseReg,"$") then
LogEnabled = true
LogSeverity = LOGSEVERITY_ERROR
WriteLine "Error in UseReg - $ not translated - " &amp; UseReg
cdate("unexpected error - macro subsitution did not happen") ''' intentionally crash
end if
WriteLine "UseReg: " &amp; UseReg
UseReg = lcase(trim(UseReg &amp; " "))
UseReg = (UseReg = "true")
WriteLine "UseReg: " &amp; UseReg

ThresholdValue = "$Config/ThresholdValue$"
if instr(ThresholdValue,"$") then
LogEnabled = true
LogSeverity = LOGSEVERITY_ERROR
WriteLine "Error in ThresholdValue - $ not translated - " &amp; ThresholdValue
cdate("unexpected error - macro subsitution did not happen") ''' intentionally crash
end if
WriteLine "ThresholdValue: " &amp; ThresholdValue
ThresholdValue = cint(ThresholdValue &amp; "")

SubFolder = "$Config/SubFolder$"
if instr(ThresholdValue,"$") then
LogEnabled = true
LogSeverity = LOGSEVERITY_ERROR
WriteLine "Error in SubFolder - $ not translated - " &amp; SubFolder
cdate("unexpected error - macro subsitution did not happen") ''' intentionally crash
end if
WriteLine "SubFolder: " &amp; SubFolder
SubFolder = lcase(trim(SubFolder &amp; " "))

if (UseReg) then

dim temp, pos, strKeyPath, strValueName

temp = FolderPath
pos = len(temp)
do while (pos &gt; 0)
if (mid(temp,pos,1) = "\") then exit do
pos = pos - 1
loop
if pos = 0 then pos = len(temp) +1

strKeyPath = left(temp,pos-1) ''' no trailing backslash here
strValueName = mid(temp,pos+1)

dim oReg, strComputer, arrSubKeys, i, keyfound, arrValueNames, strValue


strComputer = "."

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &amp; _
strComputer &amp; "\root\default:StdRegProv")

If oReg.EnumKey (HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames) = 0 Then
WriteLine("The registry path exists " &amp; strKeyPath)
keyfound = true
Else
WriteLine("The HKLM registry path does not exist for " &amp; strKeyPath &amp; " assuming this is a file folder and continuing..." )
keyfound = false
end if


If (keyfound) Then

oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
FolderPath = strValue &amp; ""
else
FolderPath = ""
end if

end if

WriteLine "FolderPath: " &amp; FolderPath
if (SubFolder &lt;&gt; "" and FolderPath &lt;&gt; "") then
if (right(FolderPath,1) &lt;&gt; "\") then
FolderPath = FolderPath &amp; "\" &amp; SubFolder
else
FolderPath = FolderPath &amp; SubFolder
end if
end if
WriteLine "FolderPath: " &amp; FolderPath

dim fso, Files, Count
Count = 0
Set fso = CreateObject("Scripting.FileSystemObject")
on error resume next
Set Files = Fso.GetFolder(FolderPath).Files
if err.number &lt;&gt; 0 then Count = -1
Count = Files.Count
if err.number &lt;&gt; 0 then Count = -1
on error goto 0
wscript.echo "File count: " &amp; Count


dim tempResult

if (Count = -1) then
tempResult = "$Config/StateWhenFolderMissing$"
if instr(tempResult,"$") then cdate("unexpected error - macro subsitution did not happen")
Call oBag.AddValue("result", tempResult)
Call oBag.AddValue("details","MISSING_FOLDER")
Writeline "MISSING_FOLDER - returning: " &amp; tempResult
elseif (Count &gt;= ThresholdValue) then
Call oBag.AddValue("result", "GE_THRESHOLD")
Writeline "GE_THRESHOLD"
else
Call oBag.AddValue("result", "LT_THRESHOLD")
Writeline "LT_THRESHOLD"
end if

Call oBag.AddValue("ThresholdValue", ThresholdValue)
Call oBag.AddValue("Count", Count)
Call oBag.AddValue("PathIsARegKey",UseReg)
if (UseReg) then
tempResult = "$Config/FilePath$"
if instr(tempResult,"$") then cdate("unexpected error - macro subsitution did not happen")
Call oBag.AddValue("RegLookup", tempResult)
end if
if (SubFolder &lt;&gt; "") then
Call oBag.AddValue("Appended SubFolder",SubFolder)
end if
Call oBag.AddValue("FolderPath",FolderPath)

WriteLine "returning property bag"
WriteLine "end script."

Call oAPI.Return(oBag)



''------------------------------------------------------------------------------------------
'////////////////////////////

sub DumpArgs(Args)
dim i, status
WriteLine("Dumping args as passed in.")
WriteLine("Note: If you see quotes, they are actually part of the argument literal.")
dim untranslated
untranslated = false
status = "Args: "
for i = 0 to Args.Count - 1
status = status &amp; Args(i) &amp; " "
WriteLine(i &amp; ": " &amp; Args(i))
if (instr(Args(i),"$") &gt; 0) then
untranslated = true
LogEnabled = true
LogSeverity = LOGSEVERITY_ERROR
WriteLine("ERROR: untranslated $ encountered at arg " &amp; i &amp; ": " &amp; Args(i))
end if
next
if (untranslated) then
LogEnabled = true
LogSeverity = LOGSEVERITY_ERROR
WriteLine("ERROR: one or more untranslated $ args encountered, quitting.")
status = cdate("ERROR: one or more untranslated $ args encountered, quitting.") ''' intentionally crash here
end if

end sub

'////////////////////////////

function GetArg(quotedarg)
dim s
s = quotedarg
if s &lt;&gt; "" then
if mid(s,1,1) = """" then
s = mid(s,2, len(s)-1)
end if
if mid(s,len(s),1) = """" then
s = mid(s, 1, len(s)-1)
end if
end if
GetArg = s
end function

'////////////////////////////

sub WriteLine(in_line)

dim s

s = vbcrlf &amp; "ID: " &amp; ManagedEntityId &amp; vbcrlf &amp; WRITELINEHEADER &amp; vbcrlf &amp; vbcrlf &amp; in_line

if (LogEnabled) then
wscript.echo in_line
call oAPI.LogScriptEvent(LogEventSource, 100, LogSeverity, s)
end if

end sub

'////////////////////////////

function NormalizeDate(x)
dim retval, i, L, c

L = len(x)
for i = 1 to L
c = ucase(mid(x,i,1))
if (instr("/:AMP ",c)=0) then
retval = retval &amp; c
end if
next

NormalizeDate = retval
end function


'////////////////////////////


</Script></ScriptBody>
<TimeoutSeconds>300</TimeoutSeconds>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="ScriptPropBag">
<Node ID="PASSTHROUGH"/>
</Node>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
<TriggerOnly>true</TriggerOnly>
</ProbeActionModuleType>