

' Declare Variables ' File-related variables Dim fso, folder, file, FilePath ' Intended for use with OpsMgr two state script monitor. ' COMMENT: Verifies a target file (including path) exists. ' AUTHOR: Pete Zerger, MVP (Cloud and Datacenter Admin) ' DATE : ' ' VBScript Source File - Created with SAPIEN Technologies PrimalScript 2009 '

Here is my customized version of Pete’s script: This monitor will run a VBscript, which we adapted from Pete Zerger’s script at We will start with a simple monitor example, which will use the. The AD management pack will automatically deploy this MSI if and when it is needed, but the MP expects the OOMADS.MSI to be in that specific directly above. This might leave several active directory domain controllers without these necessary scripting objects deployed. However, if the agent is manually installed, we do not copy this file, it is copied only if the agent is pushed from the SCOM console. The context of this example is for a scenario, where we expect the OOMADS.MSI file (Active Directory Helper Objects) to be placed in a specific directoy on the agent: C:\Program Files\Microsoft Monitoring Agent\Agent\HelperObjects If the file does not exist, we can run a recovery to copy the file there from a network location. PS> Get-EventSubscriber | Unregister-EventĪt this point, the subscriber has been removed and we're back to where we started.This is going to be an example of making a two state monitor to check for the existence of a file on an agent managed server. Then, to remove them, use the Unregister-Event cmdlet. We can view all existing subscribed events by using the Get-EventSubscriber command. This will continue to monitor this folder until the PowerShell session ends. This message came from the watcher we created. Our New-Item command didn't return anything since the output was sent to $null, but we did get a message saying the file was created. PS C:\> $null = New-Item -path 'C:\FolderWhereStuffChanges\file.txt' -ItemType FileĬ:\FolderWhereStuffChanges\file.txt was Created at 15:42:35 Let's now drop a file into the C:\FolderWhereStuffChanges folder and see what happens. Id Name PSJobTypeName State HasMoreData Location Command PS> Register-ObjectEvent $watcher 'Created' -Action $action To do that, I'll use the Register-ObjectEvent cmdlet and provide it the watcher object we created, as well as the type of action to monitor. Now that I have the watcher object and the action I'd like to take when a file is created, I then need to register this event. Write-Host "$path was $changetype at $(get-date)" This is a variable that will be present every time an event fires and contains information such as the file path and the type of event that fired. As you can see below, I'm using the built-in variable. We define this action by creating a PowerShell scriptblock. There are different types of events you can "watch," such as new files or modified files, but in this article we're just going to focus on new files. For simplicity, I'll write output to the console with the name of the path of the file that gets created and the type of event. I now need to define some action to take when the event fires. $watcher.Path = 'C:\FolderWhereStuffChanges' I do that with the Path property, and since I want the watcher to raise events when one happens, I'll also set the EnableRaisingEvents property to $true. I also need to specify which folder I'll be monitoring. To do that, I'll assign the IncludeSubdirectories property. For example, I'll be monitoring a folder for new files and perhaps I'd like to monitor all subfolders, as well. Once you've instantiated the object, you can then provide various "parameters" to the watcher by assigning values to different object properties. $watcher = New-Object System.IO.FileSystemWatcher
