Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1437
  • Last Modified:

Using Powershell to monitor file changes then execute a command

So I am trying to make a script that monitors a file for changes and then executes Unblock-File cmdlet from Powershell. I have gotten to this point and PowerShell ISE is throwing a lot of errors at me. This is my first run time working with Powershell so I am wondering if I am on the right track or way off base.
 

strComputer = “.”
 
Set objWMIService = GetObject(“winmgmts:” _
 
    & “{impersonationLevel=impersonate}!\\” & _
 
        strComputer & “\root\cimv2”)
 
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
 
    (“SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE ” _
 
        & “Targetinstance ISA ‘CIM_DirectoryContainsFile’ and ” _
 
            & “TargetInstance.GroupComponent= ” _
 
                & “‘Win32_Directory.Name=””c:\\\\users\garretth\desktop\unblockfolder\””‘”)
 
Do
 
    Set objLatestEvent = colMonitoredEvents.NextEvent
 
    gci C:\users\garretth\desktop\unblockfolder | Unblock-File
 
Loop
 

Open in new window

0
Garrett
Asked:
Garrett
  • 3
  • 2
1 Solution
 
Dustin SaundersDirector of OperationsCommented:
Create a file system watcher to do the unblock.

$folder = 'c:\test\' 
$filter = '*.*'  

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'} 

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
$name = $folder + $Event.SourceEventArgs.Name 
Unblock-File $name
} 

Open in new window


Change $folder with your folder and the $filter if it's only a specific file type.
1
 
Dustin SaundersDirector of OperationsCommented:
You can unregister the watcher (turn it off) with:
Unregister-Event FileCreated 

Open in new window

0
 
GarrettAuthor Commented:
That you for your response! Super helpful. Is it possible to make it loop or does the $fsw call a Windows service that is always monitoring? My network admin was wondering the system resource use on this as well. It shouldn't be a lot but I was wondering if you had any insight.
0
 
Dustin SaundersDirector of OperationsCommented:
You're creating a watcher object, so it will always wait for a file event to occur in the location specified and then trigger (doesn't run like a service or a looping script).  The resource usage is negligible, unless for some reason you are saving and processing thousands of files every few seconds; but I'm guessing based off the unblock command you're just downloading office files you want unblocked.
1
 
GarrettAuthor Commented:
You're correct about the office files!
Thank you so much. I appreciate your help and explanation!
0

Featured Post

Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now