Avatar of Gonzalo Becerra
Gonzalo Becerra
Flag for Argentina asked on

Wait for Finish Zip

Hi guys,

I'm finishing one script and I have one issue when I need zip files.

I need wait for finish zip and then continue with the rest.

This is my command line:
echo "Creating Zip File with Solutions"
	Get-Item $backupdestination\*.* -Include *.*  -Exclude exportwsp.wsp | out-zip $backupdestination"\"$backupfilename"-Solutions"$guid".zip"

Open in new window


I tried with Start-Job using wait but I have problems with Out-Zip:
The term 'out-zip' is not recognized as the name of a cmdlet

Could you help me to know how to wait to finish the Zip File before continue the Script?

Thanks in advance,
Powershell

Avatar of undefined
Last Comment
Gonzalo Becerra

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
the_endjinn

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Gonzalo Becerra

ASKER
Thanks :)

How can I exclude one file from this zip?.

Because I'm including all except exportwsp.wsp
Gonzalo Becerra

ASKER
Sorry, The fuction is working but is not waiting for finish.

 NoWait
This is the code:
 
function OutZip {
	  Param([string]$path)

	  if (-not $path.EndsWith('.zip')) {$path += '.zip'}

	  if (-not (test-path $path)) {
	    set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
	  }
	  $ZipFile = (new-object -com shell.application).NameSpace($path)
	  $input | foreach {$zipfile.CopyHere($_.fullname)}
	} 
	
	echo "Creating Zip File with Solutions"
	Get-Item $backupdestination\*.* -Include *.*  -Exclude exportwsp.wsp | OutZip $backupdestination"\"$backupfilename"-Solutions"$guid".zip"
	


	Write-Output "Adding Export-Solutions"

Open in new window

Gonzalo Becerra

ASKER
In the foreach we need add some Sleep?
Your help has saved me hundreds of hours of internet surfing.
fblack61
the_endjinn

Sorry, what is not waiting to finish before what?
Gonzalo Becerra

ASKER
For example in my script, I would like to see the Write-Output "Adding Export-Solutions" After finish compress all with OutZip.
the_endjinn

Try to pipe the output of Out-Zip to null, this should mean that the write-output doesn't execute until the Shell.Application.CopyHere method finishes as it's actually waiting for out-null to get everything first.
Get-Item $backupdestination\*.* -Include *.*  -Exclude exportwsp.wsp | OutZip $backupdestination"\"$backupfilename"-Solutions"$guid".zip" | out-null
Write-Output "Adding Export-Solutions"

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Gonzalo Becerra

ASKER
With out-null does not work does not wait to finish the zip after show me the next command.

I used this:
Write-Output "Creating Zip File with Solutions"
	Get-Item $backupdestination\*.* -Include *.*  -Exclude exportwsp.wsp | out-zip $backupdestination"\"$backupfilename"-Solutions"$guid".zip" | Out-Null

	Write-Output "Adding Export-Solutions"

Open in new window

the_endjinn

What about '$input | foreach {$zipfile.CopyHere($_.fullname)} | out-null'?
Gonzalo Becerra

ASKER
No, still continue with the next command before finish the zip.

 
function out-zip { 
 $path = $args[0] 
 $files = $input 

 if (-not $path.EndsWith('.zip')) {$path += '.zip'} 

 if (-not (test-path $path)) 
 { 
  set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
 } 

 $zipfile = (new-object -com shell.application).NameSpace($path) 
 $files | foreach {$zipfile.CopyHere($_.fullname) | Out-Null } 
}

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
the_endjinn

I'm afraid the copyhere must return immediately and I'm not sure of a way to interrogate it for a finished property.

You could try a .net library like sharpziplib instead and you would have more control. http://sharpdevelop.net/OpenSource/SharpZipLib/
Gonzalo Becerra

ASKER
I found this I think is very similar that I need, but how can I adapt to pass as parameter?


http://mysticdotnet.blogspot.com/2010/04/compression-with-powershell.html
SOLUTION
Gonzalo Becerra

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Gonzalo Becerra

ASKER
Thanks for your help!!.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.