Submitted By: Eric Payne

Microsoft Operations Manager response script that deletes a folder.

Visual Basic
Edit|Remove
'#####================================================================================ 
'## Title: DeleteFolder.vbs
'## Author: Eric Payne 
'## Client\Company: xxxx
'## Date: 12/7/2006
'##                          
'## Purpose: 
'##                          1. Response script for MOM. Deletes Folder.
'##                                                                          
'## Requirements:
'##                          1.  Mom Script Parameter - FolderName: Name of Folder to delete.
'##
'##
'## Basic Logic: 
'##                          1. Reads in FolderName parameter
'##                          2. Calls DeleteFolder function
'##                          3. Creates event based on DeleteFolder function responce
'#####================================================================================ 

Option Explicit
On Error Resume Next

'Event Type constants
Const EVENT_TYPE_ERROR = 1  
Const EVENT_TYPE_WARNING = 2
Const EVENT_TYPE_INFORMATION = 4
Const EVENT_NUMBER_1000 = 1000
Const EVENT_NUMBER_1001 = 1001
Const EVENT_NUMBER_1002 = 1002

'Parameters
Dim objParams: Set objParams = ScriptContext.Parameters
Dim strFolderName: strFolderName = objParams.Get("FolderName")

'Call function
if FolderExists(strFolderName) Then
                If DeleteFolder(strFolderName) Then
                      CreateEvent EVENT_NUMBER_1000,EVENT_TYPE_INFORMATION,"DeleteFolder",strFolderName & " has been deleted."
                Else
                      CreateEvent EVENT_NUMBER_1001,EVENT_TYPE_WARNING,"DeleteFolder",strFolderName & " has not been deleted."
                End if
Else
                      CreateEvent EVENT_NUMBER_1001,EVENT_TYPE_WARNING,_
                          "DeleteFolder",strFolderName & " does not exists and can not be deleted."

End if

if err <> 0 Then CreateEvent EVENT_NUMBER_1002,EVENT_TYPE_ERROR,"DeleteFolder","An Error occurred. Error.Number: " _
                                                                                                & err.number & " | Error.Source: " & err.Source & " | Error.Description: " & err.Description 

Function DeleteFolder(strFolderName)
                'DESC: Attemps to delete strFolderName
                Const FORCE = True
                Dim objFSO: Set objFSO = CreateObject("Scripting.FolderSystemObject")
                objFSO.DeleteFolder strFolderName, FORCE 
                if err = 0 Then
                                DeleteFolder = vbTrue
                Else
                                DeleteFolder = vbFalse
                End if     
                Set objFSO = Nothing
End Function

Function FolderExists(strFolderName)
                'DESC: Checks to see if folder Exists
                Dim objFSO: Set objFSO = CreateObject("Scripting.FolderSystemObject")
                if objFSO.FolderExists(strFolderName) Then
                                FolderExists = vbTrue
                Else
                                FolderExists = vbFalse
                End if
                Set objFSO = Nothing
End Function

Sub CreateEvent(intEventNumber,intEventType,strEventSource,strEventMessage)
   'DESC: Creates an internal MOM event
    Set objEvent = ScriptContext.CreateEvent()
    objEvent.EventSource = strEventSource
    objEvent.EventNumber = intEventNumber
    objEvent.EventType = intEventType 
    objEvent.Message = strEventMessage
    ScriptContext.Submit objEvent
End Sub