1

I'm trying to copy files into a zip file. I used start-job and wait-job to wait for the copy process finish but it didnt work at all. This is the error when I call receive-job:

Method invocation failed because 
[Deserialized.System.__ComObject#{a7ae5f64-c4d7-4d7f-9307-4d24ee54b841}] does not contain a method 
named 'copyhere'.
    + CategoryInfo          : InvalidOperation: (copyhere:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
    + PSComputerName        : local

This is my code

$Shell = New-Object -com Shell.Application
$b = $shell.namespace($zippath.ToString())

$files = Get-ChildItem $fileDest -recurse -include *.*
foreach ($file in $files) {
    $job = Start-Job -scriptblock {$args[0].copyhere($args[1].fullname)} -Name copyfiles -argumentlist @($b, $file)
    Wait-Job -name copyfiles
    #Remove-Item -Path $file
}

Any idea?

Update

@PetSerAI, I also try to put that COM object into the job but it doesnt work too. Receive-job doesn't show anything. Below is the new code:

$func = {
    $zippath = "d:\nhl\test.zip"
    $Shell = New-Object -com Shell.Application
    $b = $shell.namespace($zippath.ToString())
    $b.CopyHere($args[0].tostring())
    #Remove-Item -Path $filedest
}

$file = "D:\nhl\abc\test.rar"
start-job -ScriptBlock $func -ArgumentList $file
Bum
  • 89
  • 1
  • 6
  • 1
    Why start a job for each file ? See the second answer here, maybe this method will work better for you : http://stackoverflow.com/questions/1153126/how-to-create-a-zip-archive-with-powershell/1153144#1153144 – sodawillow Dec 12 '15 at 09:34
  • 2
    `Start-Job` start new PowerShell process. Note the type name `Deserialized.System.__ComObject#{a7ae5f64-c4d7-4d7f-9307-4d24ee54b841}`: **`Deserialized`**. You simply can not pass live COM object thru process boundary with PowerShell, you have to create new one inside the job. – user4003407 Dec 12 '15 at 12:50
  • @sodawillow I tried that but `::CreateFromDirectory` will only zip a whole directory into a new zipfile while I need to add a file in to an existed zipfile – Bum Dec 13 '15 at 04:16
  • See : http://stackoverflow.com/questions/14827716/adding-a-complete-directory-to-an-existing-zip-file-with-system-io-compression-f you can add files to an existing archive with this method (last snippet in the question). – sodawillow Dec 13 '15 at 09:53

0 Answers0