Wait for Finish Zip

Gonzalo Becerra
Gonzalo Becerra used Ask the Experts™
on
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,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Out-Zip isn't a standard PowerShell commandlet, you would need to add an Out-Zip function first and then call it in the same way you state above. Below is an Out-Zip function from /\/\o\/\/ "The Powershell Guy". His stuff is well worth a read - http://ThePowerShellGuy.com

# Out-Zip.msh
# creates out-Zip function
# /\/\o\/\/ 2006   
# http://mow001.blogspot.com

function out-zip {
  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)}
} 

Open in new window

Gonzalo BecerraSharePoint - Engineering Team - Assistant Director

Author

Commented:
Thanks :)

How can I exclude one file from this zip?.

Because I'm including all except exportwsp.wsp
Gonzalo BecerraSharePoint - Engineering Team - Assistant Director

Author

Commented:
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

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Gonzalo BecerraSharePoint - Engineering Team - Assistant Director

Author

Commented:
In the foreach we need add some Sleep?
Sorry, what is not waiting to finish before what?
Gonzalo BecerraSharePoint - Engineering Team - Assistant Director

Author

Commented:
For example in my script, I would like to see the Write-Output "Adding Export-Solutions" After finish compress all with OutZip.
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

Gonzalo BecerraSharePoint - Engineering Team - Assistant Director

Author

Commented:
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

What about '$input | foreach {$zipfile.CopyHere($_.fullname)} | out-null'?
Gonzalo BecerraSharePoint - Engineering Team - Assistant Director

Author

Commented:
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

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 BecerraSharePoint - Engineering Team - Assistant Director

Author

Commented:
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
Gonzalo BecerraSharePoint - Engineering Team - Assistant Director
Commented:
I found the fuction, with this fuction you will compress into zip file and when finish will continue with the rest.

 
function Compress-ToZip
{
    param([string]$zipfilename)

    if(-not (test-path($zipfilename)))
    {
        set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
        (Get-ChildItem $zipfilename).IsReadOnly = $false   
    }

    $shellApplication = new-object -com shell.application
    $zipPackage = $shellApplication.NameSpace($zipfilename)

foreach($file in $input)
{
    $zipPackage.CopyHere($file.FullName)    
    $size = $zipPackage.Items().Item($file.Name).Size
    while($zipPackage.Items().Item($file.Name) -Eq $null)
    {
        start-sleep -milliseconds 5
        #write-host "." -nonewline
    }
}

}

Open in new window

Gonzalo BecerraSharePoint - Engineering Team - Assistant Director

Author

Commented:
Thanks for your help!!.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial