Solved

Using Powershell to monitor file changes then execute a command

Posted on 2016-08-03
5
88 Views
Last Modified: 2016-08-04
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
Comment
Question by:Garrett
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
Dustin Saunders earned 500 total points
Comment Utility
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
 
LVL 12

Expert Comment

by:Dustin Saunders
Comment Utility
You can unregister the watcher (turn it off) with:
Unregister-Event FileCreated 

Open in new window

0
 
LVL 1

Author Comment

by:Garrett
Comment Utility
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
 
LVL 12

Expert Comment

by:Dustin Saunders
Comment Utility
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
 
LVL 1

Author Comment

by:Garrett
Comment Utility
You're correct about the office files!
Thank you so much. I appreciate your help and explanation!
0

Featured Post

Free book by J.Peter Bruzzese, Microsoft MVP

Are you using Office 365? Trying to set up email signatures but you’re struggling with transport rules and connectors? Let renowned Microsoft MVP J.Peter Bruzzese show you how in this exclusive e-book on Office 365 email signatures. Better yet, it’s free!

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
A procedure for exporting installed hotfix details of remote computers using powershell
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now