WMI Event Creation-RoboCopy

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of H Man H Man 2 years, 5 months ago.

  • Author
    Posts
  • #16736
    Profile photo of H Man
    H Man
    Participant

    Im trying simulate a windows 8 feature File History, buy creating a WMI event and Triggering a robocopy heres what i have so far any help would be great. The wmi event is being registered just not executing

    $WMI = @{
        Query = 'SELECT * FROM __InstanceCreationEvent WITHIN 10
                 WHERE TargetInstance ISA "CIM_DirectoryContainsFile"
                 AND TargetInstance.GroupComponent = "Win32_Directory.Name=\"c:\\\\FileHistory\""'
        Action = {
            $WatchFolder = "C:\FileHistory"
        $destination = "\\fileshare\users$\$env:USERNAME\FileHistory"
       
       
        $Udrive = "\\fileshare\users$\$env:USERNAME"  
        $UTest  = test-path $Udrive
         if ($UTest -eq $false ) {
        net use U: "\\fileshare\users$\$env:USERNAME"   
           }
        
        
        $Files = (Get-ChildItem $WatchFolder -Recurse).FullName 
        if ($Files) {
    
        if(!(Test-Path -Path $destination)){mkdir $destination}
        
        Robocopy  "$WatchFolder" "$destination" /mir /r:2 /w:3 
        }
    
    
        }
     SourceIdentifier = "RoboDetectnCopy"
    }
    $Null = Register-WMIEvent @WMI
    
    
    

    I also tried the Powerevents module

    $MyFilter = New-WmiEventFilter –Name WatchFolderRoboCopy –Query 'SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE TargetInstance ISA "CIM_DirectoryContainsFile" AND TargetInstance.GroupComponent = "Win32_Directory.Name=\"c:\\\\FileHistory\""' –EventNamespace "root\cimv2"
    $MyConsumer = New-WmiEventConsumer –ConsumerType Script –Name WatchFolderRoboCopy –ScriptFile "C:\jdoe\user\AppData\Roaming\FileHistory\FileHistory.ps1"
    New-WmiFilterToConsumerBinding –Filter $MyFilter –Consumer $MyConsumer
    
    
    
    
  • #16835
    Profile photo of Don Jones
    Don Jones
    Keymaster

    I think you'll ultimately find that WMI's eventing doesn't perform especially well with this class. If the goal is to watch for new file creation, the .NET Framework's FileSystemWatcher performs much better and tends to be more reliable. Even so, running that from a continuously-running PowerShell script might not be the most production-friendly thing to do, either. Good luck.

  • #16838
    Profile photo of H Man
    H Man
    Participant

    thanks Don

You must be logged in to reply to this topic.