Author Posts

July 2, 2014 at 1:15 pm

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



July 7, 2014 at 7:24 am

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.