• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3583
  • Last Modified:

force PowerShell to wait for cmdlet execution to end

Before I forget, credit where credit is due for as far as I've gotten: the following code is adapted from http://mow001.blogspot.com/2006/01/msh-out-zip-function.html.

Okay, here's my challenge: I would like to create a function that will accept a series of filenames (full paths) as arguments, and zip them ONE AT A TIME to the same folder and filename, but with ".zip" appended.

So [PS> zipfiles "g:\test\file1.txt" "g:\test\file2.txt"] will generate [g:\test\file1.txt.zip] and [g:\test\file2.txt.zip].

Here's something that works:

function zipFiles {
  for( $i = 0; $i -lt $args.length; $i++ ){
    # Define the source and destination file paths.
    $FileName = $args[$i]
    $ZipName = $FileName + ".zip"
   
    # Create code to zip the file.
    $cmds = ' $' + "ZipFile = (new-object -com shell.application).NameSpace('$ZipName');"
    $cmds += ' $' + "ZipFile.CopyHere('$FileName');"

    # Execute the code in a separate process and wait for it to exit, so we won't overload the machine.
    write-host "Zipping '$FileName...'"
    set-content $ZipName("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    [diagnostics.process]::start("powershell", "-noexit -command & {$cmds}").waitforexit()
    write-host "...done."
  }
}

...BUT the "-noexit" is highly undesirable, since there are a lot of files to be zipped and I would like to be able to start it and walk away, rather than sit there and close the secondary window for each file.

The problem is, if I remove the "-noexit" it doesn't work.  The secondary window doesn't wait for CopyHere() to finish; it just opens and closes in a flash without doing anything.

The ideal solution would be something that tells PowerShell to wait for a given cmdlet to finish executing before going on to the next such that the secondary window wouldn't even be necessary.

Hopefully the solution is obvious to the Shellistas here, but I'm new and confused.
0
Bitley
Asked:
Bitley
  • 3
  • 2
2 Solutions
 
Chris DentPowerShell DeveloperCommented:

If it's doing it one at a time why not just do each inline rather than spawning a separate PowerShell process? What does the separate process give you here?

Chris
0
 
BitleyAuthor Commented:
Thanks Chris, good question - quite possibly the separate process is giving me nothing.

All I'm trying to accomplish is to single-thread (I believe that's the right term here) the execution so that one file at a time is compressed.  Without the separate process and the .waitforexit, as many zip processes as there were files would be spawned simultaneously, doing Bad Things to the machine since some of the files are multi-GB.

In case there's any question about it, the important part is the end result: zipping multiple files one at a time.  Feel free to ignore the code listing - if there's a completely different and better way to accomplish the goal, I'm all ears!
0
 
peter_fieldCommented:
The code is using the shell.application COM object, which simply invokes the shell to perform the action for you, it runs asynchronously and doesn't have an option to run synchronously, so you're out of luck. It will also pop any errors up on screen. I am disappointed at windows lack of decent support for ZIP, I haven't found any way to zip files progmatically reliably (i.e. you can wait and get an exit code or whatever to determine success).

I'd suggest obtaining a 3rd party app that supports being invoked from the command line (like 7zip or winzip) and calling them to do the zipping for it, it will be more reliable.
0
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
BitleyAuthor Commented:
Thanks Peter, even though that was the kind of answer I was afraid of!
I'll leave this thread here for another day or two to see if there are more comments.
Anybody else?  Anybody?
0
 
Chris DentPowerShell DeveloperCommented:

Hey,

Sorry for the late response.

I've found the same problems as Peter with this. It's no fun trying to get it to run in a controlled manner. And lack of proper support for things like this is extremely frustrating.

There are quite a few threads on using 7Zip instead, obviously with greater success since you're executing on the command line.

Chris
0
 
BitleyAuthor Commented:
Thanks guys, even though the answer wasn't what I was hoping for!  Have a great weekend.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now