none
Problems with the wait-job RRS feed

  • Question

  • Hi guys, first question in this forum.

    I have created a script for backing up file-stores and databases, then copy the content into a zip-file.

    I really want the powershell window "sleep" while the zip-process is compacting, but I am not getting the results I want. 

    To me it looks like the wait-job is waiting for the the jobs running in the back ground. But the Job ends after sending commands to a different process (compressing) and exits premature.

    This is my code:

    $filepath1 = "F:\Music\"

    function zipIT{
    write-host "Starting ZIP job, might complete after script is finished" -ForegroundColor Green
    Set-Content F:\backup\mytest.zip ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    $File = Get-ChildItem F:\backup\mytest.zip -ErrorAction SilentlyContinue
    $File = (New-Object -COM Shell.Application).Namespace($File.FullName)
    $File.CopyHere($filepath1, 4)}

    $myZipIT = zipIT
    Start-Job -name SnoreIT -ScriptBlock {$myZipIT}
    Get-Job -IncludeChildJob | Wait-Job

    I get the following information in the shell:

    PS F:\Lync scripts> & '.\sleep time.ps1'
    Starting ZIP job, might complete after script is finished

    Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
    --     ----            -------------   -----         -----------     --------             -------                  
    22     SnoreIT         BackgroundJob   Running       True            localhost            $myZipIT                 
    22     SnoreIT         BackgroundJob   Completed     True            localhost            $myZipIT                 
    23     Job23                           Completed     True            localhost            $myZipIT                 
    Finished

    Problem is: job reports completed but the compressing process is not over (I guess PS has handed it over to a different process, and no longer regard the the job as running.

    You guys have any ideas how to handle this? It's the last piece to my puzzle, which I need to use this script in a fully automated way. (When running the script as it is, the scheduled task exits prematurely and the zipfile is corrupted)

    Kind regards,

    Lasse


    Lasse Wedø,
    Blog:Tech@work, Twitter: @lawedo

    Please take a second to hit the green arrow on the left if the post was helpful, or mark it as an answer if it resolved your issue.

    Tuesday, January 7, 2014 6:47 PM

Answers

  • The shell object CopyHere method isn't really designed for automation - for example, there is no error checking and it works asynchronously.

    You are better off using a tool designed for the job. 7-Zip is free and has a command-line interface.

    Bill

    Tuesday, January 7, 2014 7:39 PM
    Moderator
  • Zip with Net Framework:

    [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
    [System.IO.Compression.ZipFile]::CreateFromDirectory('c:\scripts','c:\temp\scripts.zip','Optimal',$false)
    


    ¯\_(ツ)_/¯

    • Marked as answer by Lasse WedøMVP Tuesday, January 21, 2014 9:59 AM
    Wednesday, January 8, 2014 11:29 AM

All replies

  • Use 7ZIP.  It is much more reliable, faster and more flexible.

    ¯\_(ツ)_/¯

    Tuesday, January 7, 2014 6:51 PM
  • The shell object CopyHere method isn't really designed for automation - for example, there is no error checking and it works asynchronously.

    You are better off using a tool designed for the job. 7-Zip is free and has a command-line interface.

    Bill

    Tuesday, January 7, 2014 7:39 PM
    Moderator
  • Thanks, for the feedback.

    But I was looking for something which could run natively, without other downloads.

    I am fine with the lack of error correction :)

    Lasse


    Lasse Wedø,
    Blog:Tech@work, Twitter: @lawedo

    Please take a second to hit the green arrow on the left if the post was helpful, or mark it as an answer if it resolved your issue.

    Tuesday, January 7, 2014 7:44 PM
  • You skipped the other part - CopyHere runs asynchronously, which means you can't wait for it to complete. This is by design. See this thread:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/760ea95b-4c3e-4f48-a0f6-9d728d5580da/

    Bill

    Tuesday, January 7, 2014 7:48 PM
    Moderator
  • You're right.

    I was hoping for a different answer ;)

    Thank you for your replies.

    I'll look into 7zip or other programs to do the job. Guess I was hoping someone would say: Hey, you should use this other way of creating a zip file.

    Cheers!


    Lasse Wedø,
    Blog:Tech@work, Twitter: @lawedo

    Please take a second to hit the green arrow on the left if the post was helpful, or mark it as an answer if it resolved your issue.

    Tuesday, January 7, 2014 9:04 PM
  • Zip with Net Framework:

    [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
    [System.IO.Compression.ZipFile]::CreateFromDirectory('c:\scripts','c:\temp\scripts.zip','Optimal',$false)
    


    ¯\_(ツ)_/¯

    • Marked as answer by Lasse WedøMVP Tuesday, January 21, 2014 9:59 AM
    Wednesday, January 8, 2014 11:29 AM
  • Thanks, I'll try it out :)

    Lasse Wedø,
    Blog:Tech@work, Twitter: @lawedo

    Please take a second to hit the green arrow on the left if the post was helpful, or mark it as an answer if it resolved your issue.

    Wednesday, January 8, 2014 4:51 PM