I'm not sure if I'm approaching this the best way and thought I would see how 
others handle similar requests. We have a request to monitor some folders where 
files that fail to process arrive. We can normally resubmit them without issue 
and they'll process on a 2nd or 3rd try. My thought is to have SCOM 
automatically perform the file moves, but if the same file is seen 3+ times, to 
raise an alert. I do have this working via the below test script. However, in 
its current form there is an external dependency on the XML file on the 
filesystem that gets brought in at runtime. I know performance data has the 
ability to show the historical trends, so you can do the 'number of samples' 
before generating an alert. Is that a better idea? Perhaps there are some 
gotchas to consider, other than the XML file going away-which I have a separate 
monitor to warn me if/when an XML file gets deleted.

I appreciate any constructive criticism as I'm happy to learn from others' 
experience. Below is the script.

param($Directory,
      $LogFile = "C:\Windows\Temp\SCOM_TestScript_$(Get-Date -Format 
yyyyMMdd).log",
      $XMLPath,
      $SuccessPath,
      $RetryPath
     )

# Create SCOM Property Bag #
$api = new-object -comObject 'MOM.ScriptAPI'
$bag = $api.CreatePropertyBag()

# Define variables #
$XMLFile = Split-Path $XMLPath -Leaf
$ErrorMatch = 0


# Import XML to provide historical view #
$Filehash = @{}
$FileHash = Import-Clixml $XMLPath

# Begin Script #
$FolderItems = Get-ChildItem $Directory
$FileCount = ($FolderItems | Measure-Object).Count
If($FileCount -gt 0)
{
    $FileNames = $FolderItems | Foreach {$_.Name}
    Foreach($File in $Filenames)
    {
        $SourceFile = "$Directory\$($File)"
        Start-Log -Entry "Processing Started: $File"
        If(-Not ($FileHash.ContainsKey($File)))
        {
            $FileHash.Add($File,[int]0)
            Move-Item -Path $SourceFile -Destination $RetryPath
        }
        Else
        {
            $FileHash[$($File)] += 1
            If($FileHash[$File] -lt 5)
            {
                Move-Item -Path $SourceFile -Destination $RetryPath
            }
            Else
            {
                $RepeatList += $File.ToString() + "`r`n"
                $ErrorMatch += 1
            }
        }
    }
}

If($Filehash.Count -gt 0)
{
    $HashClone = $FileHash.Clone()
    $HashClone.GetEnumerator() | Foreach {
        If(Test-Path "$SuccessPath\$($_.Key)")
        {
            $FileHash.Remove("$($_.Key)")
        }
    }
}

$FileHash | Export-Clixml $XMLPath

# Add data to Property Bag for SCOM Processing #
$bag.AddValue('Directory',$Directory)
$bag.AddValue('FileCount',$FileCount)
$bag.AddValue('FileList',$FileList)
$bag.AddValue('ErrorMatches',$ErrorMatch)
$bag.AddValue('RepeatFiles',$RepeatList)

# $api.return($bag)
$bag


Confidentiality Notice: This is a transmission from Community Hospital of the 
Monterey Peninsula. This message and any attached documents may be confidential 
and contain information protected by state and federal medical privacy 
statutes. They are intended only for the use of the addressee. If you are not 
the intended recipient, any disclosure, copying, or distribution of this 
information is strictly prohibited. If you received this transmission in error, 
please accept our apologies and notify the sender. Thank you.



Reply via email to