title: File system watcher Event when run using a script
body: <p>Hi I have installed the  PowerShellPack and I am using the FileSystem watcher module,but the problem when I safe the file as a script and execute it.</p>
  
  <p>The problem is that if you execute the script it runs and the monitors the folder for changes but once the script stops (gets to the end of execution) the folder is no longer monitored.
  I have tried to place everything in a do while loop but that does not seem to work.</p>
  
  <p><a href="http://archive.msdn.microsoft.com/PowerShellPack" rel="nofollow">PowerShellPack Install</a></p>
  
  <pre><code>Import-Module -Name FileSystem
  
  
  $TempCopyFolder = "c:\test"
  $PatchStorage = "c:\testpatch"
  
  
  Start-FileSystemWatcher -File $TempCopyFolder  -Do {   
      $SearchPath = $File
      $PatchesPath = $PatchStorage
      $NewFolderFullPath = "$($eventArgs.FullPath)"
      $NewFolderName = "$($eventArgs.Name)"
      $PathToCheck = "$PatchesPath\$NewFolderName"
  
      #Check if it is a filde or folder 
      switch ($ObjectType) 
             {{((Test-Path $NewFolderFullPath -PathType Container) -eq $true)}{$ObjectType = 1;break}
             {((Test-Path  $NewFolderFullPath -PathType Leaf) -eq $true)}{$ObjectType = 2;break}} 
  
      # Its a folder so lets check if we have a folder in the $PatchesPath already
      IF($ObjectType -eq 1){
         IF(!(Test-Path -LiteralPath $PathToCheck -EA 0))
             {
              sleep -Seconds 3
  
              #Make a new directory where we store the patches
                New-item -Path $PatchesPath -Name $NewFolderName -ItemType directory
  
              #Make a folde in the folder for TC1
              $TcFolder=$NewFolderName + '_1'
              $NewPatchesPath = "$PatchesPath\$NewFolderName"
  
              New-item -path $NewPatchesPath -Name $TcFolder -ItemType directory
  
              $CopySrc = $NewFolderFullPath
              $CopyDes = "$NewPatchesPath\$TcFolder"
  
             }
  
         # There is a folder there so lets get the next number
         Else{
  
              $HighNumber = Get-ChildItem -Path $PathToCheck | select -Last 1
  
              #Core_SpanishLoginAttemptsConfiguration_Patch_03                                       
  
              $NewNumber = [int](Select-String -InputObject $HighNumber.Name -Pattern "(\d\d|\d)" | % { $_.Matches } | % { $_.Value } )+1
              $TcFolder= $NewFolderName + '_' + $NewNumber
  
              $NewPatchesPath = "$PatchesPath\$NewFolderName"
  
              $CopySrc = $NewFolderFullPath
              $CopyDes = "$NewPatchesPath\$TcFolder"
             }
  
           #Lets copy the files to their new home now that we know where every thing goes 
  
           $robocopy = "robocopy.exe"
           $arguments = '''' + $CopySrc + '''' +' '+ ''''+ $CopyDes + '''' + '/E'
  
           Invoke-Expression  -Command "$robocopy $arguments"
  
           Do {sleep -Seconds 1;$p = Get-Process "robo*" -ErrorAction SilentlyContinue}
               While($p -ne $null)
  
          #Now lets check every thing copyed 
  
          $RefObj = Get-ChildItem -LiteralPath $NewFolderFullPath -Recurse
          $DifObj = Get-ChildItem -LiteralPath $CopyDes -Recurse
  
          IF(Compare-Object -ReferenceObject $RefObj -DifferenceObject $DifObj)
             {write-host "Fail"}
          Else{# Now lets delete the source
  
               Remove-Item -LiteralPath $CopySrc -Force -Recurse
               }     
  }} 
  </code></pre>