none
Expand Archive Wont Execute In ObjectEvent RRS feed

  • Question

  • Hello,

    I am using a IO.FileSystemWatcher FileCreated object event to detect when a zip file is placed in a folder, once the zip is detected the script needs to extract the contents and save the files in another directory. If I simply call Expand-Archive C:\source.zip -DestinationPath C:\dest\ -Verbose -Force inside the IO.FileSystemWatcher FileCreated object event, the output window says VERBOSE: Performing the operation "Expand-Archive" on target "C:\source.zip". and will hang here during execution forever.

    If I place the cmdlet Expand-Archive outside of the object event, everything works, all contents in the zip file are extracted. This seems to me that there is a different thread the object event is located on. How would i get Expand-Archive cmdlet to work inside a object event? Below is the script that wont expand a zip file.


    Register-ObjectEvent $fileWatcher Created -SourceIdentifier FileCreated -Action {
    Expand-Archive C:\source.zip -DestinationPath C:\dest\ -Verbose -Force
    }
    
    Friday, October 16, 2020 5:39 PM

All replies

  • You can almost never execute a long running process in an event as it can block and hang.  I suggest just moving the archive or queuing it for processing.  Provide a scheduled task to dequeue the archives and expand them one at a time.


    \_(ツ)_/

    Saturday, October 17, 2020 7:00 AM
  • If he doesn't mind some complexity, MSMQ might be the way to have the event notify the other process to expand the archive. The queue can be a durable one so nothing can be lost.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Saturday, October 17, 2020 6:51 PM
  • If he doesn't mind some complexity, MSMQ might be the way to have the event notify the other process to expand the archive. The queue can be a durable one so nothing can be lost.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    I agree.   I have used MSMQ often and it is like magic.  Unfortunately most companies never use it and it needs to be explicitly installed.  The code is trivial to feed the queue and the queue can have a scripted reaction to enqueued objects which makes it smooth and, as pointed out, the queues are persisted.

    Moving a large file on the same volume is near instantaneous and is persistent.  You cannot lose a file.  Processing with a scheduled task is trivial and more flexible when MSMQ is not available.


    \_(ツ)_/

    Sunday, October 18, 2020 2:01 AM
  • Hi,

    I will definitely use MSMQ if it saves me from anymore headaches with trying to get this to work,iam still pretty new to PS iam typically a c# dev this is just a personal project iam developing.Reading about MSMQ seems simple for what it does if iam understanding both of you correctly i would have two scripts one for watching the folder for any new files being placed in it plus sending a "trigger" MSMQ message and the other which only expands the archive and listens for a MSMQ message.

    Iam not sure if i can leverage my setup for dealing with this problem iam running a 16core AMD Thread Ripper. Essentially my whole process is i download about 100 different zip files which gets copied into a specific folder each day which powershell will expand the contents and determine what type of files are in it and create a folder structure typically each zip file is around 100-200mb.Would it be worth it to try parallel programming in this case?Any input is appreciated thanks

    Sunday, October 18, 2020 6:12 PM
  • Yes, the two scripts (or two processes, or two machines) concept is correct, only you're not limited to just two. If you've worked with named pipes it's almost the same thing, except the writer and reader don't have to running at the same time, and you won't lose whatever is in the pipe if they both aren't.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Sunday, October 18, 2020 7:33 PM
  • https://techcommunity.microsoft.com/t5/windows-powershell/expand-archive-wont-run-in-objectevent/m-p/1789273

    hassansayedissa

    Sunday, October 18, 2020 8:22 PM
  • https://techcommunity.microsoft.com/t5/windows-powershell/expand-archive-wont-run-in-objectevent/m-p/1789273

    hassansayedissa

    Sunday, October 18, 2020 8:23 PM
  • MSMQ is a parallel threaded process.  It will receive each message and execute then as they arrive.  Almost all examples of MSMQ are in C# so you might want to write this code in C# except foro the file detection piece.  The account run under needs to have appropriate access to the file system and to MSMQ.


    \_(ツ)_/

    Sunday, October 18, 2020 8:45 PM
  • Ok perfect i will give it a try then thanks
    Monday, October 19, 2020 7:15 PM