WMI Event Creation-RoboCopy

Welcome Forums General PowerShell Q&A WMI Event Creation-RoboCopy

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
4 years, 4 months ago.

  • Author
    Posts
  • #16736

    Participant
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 1,625
    Helping HandTeam Member
    Rank: Community Hero

    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

    Participant
    Points: 0
    Rank: Member

    thanks Don

The topic ‘WMI Event Creation-RoboCopy’ is closed to new replies.