Solved

Using Powershell to monitor file changes then execute a command

Posted on 2016-08-03
5
607 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
Dustin Saunders earned 500 total points
ID: 41741207
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 13

Expert Comment

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

Open in new window

0
 
LVL 1

Author Comment

by:Garrett
ID: 41741622
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 13

Expert Comment

by:Dustin Saunders
ID: 41741774
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
ID: 41742367
You're correct about the office files!
Thank you so much. I appreciate your help and explanation!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

734 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