title: Powershell script to monitor XML files and run stored procedure if a file is added or updated
body: <p>I have a folder with sub folders that contain XML files.</p>
  
  <p>When an XML file is added, deleted or an existing XML file is updated I need to run a stored procedure in SQL Server and pass the path and filename as a parameter to the stored procedure so it can upload the changes in my database.</p>
  
  <p>I understand (from previous posts) there is a Watcher </p>
  
  <pre><code># Set Folder to Watch 
      $watcher = New-Object System.IO.FileSystemWatcher
      $watcher.Path = "C:\Projects"
      $watcher.Filter = "*.XML"
      $watcher.IncludeSubdirectories = $true
      $watcher.EnableRaisingEvents = $true  
  </code></pre>
  
  <p>and I've found this PowerShell code to connect to my database</p>
  
  <pre><code># Connect to SQL Server
  $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
  $SqlConnection.ConnectionString = "Server=MYPC\SQLEXPRESS;Database=MYDB;Integrated Security=True"
  $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
  $SqlCmd.CommandText = # Call stored procedure and pass my parameter
  $SqlCmd.Connection = $SqlConnection
  $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
  $SqlAdapter.SelectCommand = $SqlCmd
  $DataSet = New-Object System.Data.DataSet
  $SqlAdapter.Fill($DataSet)
  $SqlConnection.Close
  </code></pre>
  
  <p>However I'm not sure of rest of the code needed to so that the stored procedure will run once for each new file added, deleted or modified.</p>