0

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.

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.

PowerShellPack Install

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
             }     
}} 

2 Answers 2

1

You don't need add-on modules or WMI for this. Just set of the FileSystemWatcher yourself and register an event. Sure, it's a bit more code, but at least you know what's going on. :)

$watcher = new-object System.IO.FileSystemWatcher
$watcher.Path = 'c:\logs'
$watcher.Filter = '*.log'  # whatever you need
$watcher.IncludeSubDirectories = $true  # if needed
$watcher.EnableRaisingEvents = $true

Register-ObjectEvent $watcher -EventName Changed -SourceIdentifier 'Watcher' -Action { param($sender, $eventArgs) 
   <process event here>
}

When done:

Unregister-Event -SourceIdentifier 'Watcher'
Sign up to request clarification or add additional context in comments.

Comments

0

This is something you probably need: Monitoring file creation using WMI and PowerEvents module

PowerEvents Module isn't mandatory if you know how to create Permanent Events in WMI. For more information on that, check my eBook on WQL via PowerShell: http://www.ravichaganti.com/blog/?page_id=2134

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.