Expand Archive Wont Run In ObjectEvent

%3CLINGO-SUB%20id%3D%22lingo-sub-1789273%22%20slang%3D%22en-US%22%3EExpand%20Archive%20Wont%20Run%20In%20ObjectEvent%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1789273%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22s-prose%20js-post-body%22%3E%3CP%3EI%20am%20using%20a%20IO.FileSystemWatcher%20FileCreated%20object%20event%20to%20detect%20when%20a%20zip%20file%20is%20placed%20in%20a%20folder%2C%20once%20the%20zip%20is%20detected%20the%20script%20needs%20to%20extract%20the%20contents%20and%20save%20the%20files%20in%20another%20directory.%20If%20I%20simply%20call%20Expand-Archive%20C%3A%5Csource.zip%20-DestinationPath%20C%3A%5Cdest%5C%20-Verbose%20-Force%20inside%20the%20IO.FileSystemWatcher%20FileCreated%20object%20event%2C%20the%20output%20window%20says%20VERBOSE%3A%20Performing%20the%20operation%20%22Expand-Archive%22%20on%20target%20%22C%3A%5Csource.zip%22.%20and%20will%20hang%20here%20during%20execution%20forever.%3C%2FP%3E%3CP%3EIf%20I%20place%20the%20cmdlet%20Expand-Archive%20outside%20of%20the%20object%20event%2C%20everything%20works%2C%20all%20contents%20in%20the%20zip%20file%20are%20extracted.%20This%20seems%20to%20me%20that%20there%20is%20a%20different%20thread%20the%20object%20event%20is%20located%20on.%20How%20would%20i%20get%20Expand-Archive%20cmdlet%20to%20work%20inside%20a%20object%20event%3F%20Below%20is%20the%20script%20that%20wont%20expand%20a%20zip%20file.%3C%2FP%3E%3CHR%20%2F%3E%3CPRE%3ERegister-ObjectEvent%20%24fileWatcher%20Created%20-SourceIdentifier%20FileCreated%20-Action%20%7B%0AExpand-Archive%20C%3A%5Csource.zip%20-DestinationPath%20C%3A%5Cdest%5C%20-Verbose%20-Force%0A%7D%3C%2FPRE%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1789273%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1791644%22%20slang%3D%22en-US%22%3ERe%3A%20Expand%20Archive%20Wont%20Run%20In%20ObjectEvent%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1791644%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F835363%22%20target%3D%22_blank%22%3E%40ghz99%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENot%20sure%2C%20and%20will%20be%20good%20to%20hear%20from%20others%2C%20but%20it%20seems%20that%20the%20problem%20is%20with%20the%20extraction%20process%20and%20how%20the%20zip%20file%20is%20being%20processed.%20it%20seems%20that%20the%20process%20is%20going%20through%20an%20infinite%20loop%2C%20maybe%20while%20it's%20extracting%20the%20file%2C%20another%20zip%20file%20is%20created%20and%20it%20loop%20for%20good.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Visitor

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
}
1 Reply
Highlighted

@ghz99 

Not sure, and will be good to hear from others, but it seems that the problem is with the extraction process and how the zip file is being processed. it seems that the process is going through an infinite loop, maybe while it's extracting the file, another zip file is created and it loop for good.