Expand Archive Wont Run In ObjectEvent

Copper Contributor

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

@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.