Announcement

Collapse
No announcement yet.

Need Powershell script to wait on previous statement

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need Powershell script to wait on previous statement

    Hello Folks:

    The part of the script that concerns my question does the following:
    1. maps a UNC path to a network drive
    2. adds contents of a folder in network drive to a zip file (stored in the network drive)
    3. disconnects mapped drive.

    The problem I am running into is that the folder that needs to be zipped is fairly large and it seems that the script opens the connection and the starts to zip the files but then it continues to disconnect the mapped drive before the zipping is done.

    The bit of the script in question looks like:

    Code:
    #Map network drive (used only so that we get access using the UNC paths while the drive is mapped
    net use X: $DestRoot /user:username password
    $bakDir = Join-Path "X:" $bakDirName
    
    #Prepare the Zip Archive
    $ZipFilePath = Join-Path $DestRoot "$bakDirName.zip"
    set-content $ZipFilePath ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
    
    $ZipFile = (new-object -com shell.application).NameSpace($ZipFilePath) 
    
    $r = "gi $bakDir | .\out-zip.ps1 $ZipFilePath $_"
    
    gi $bakDir  | foreach {$zipfile.CopyHere($_.fullname)}
    
    #close connection
    net use X: /DELETE
    If I leave the line "net use X: /DELETE" out everything works fine. Otherwise I get an error about the drive not being available.

    This is what I have tried to do to make the last statement wait for all the files to be zipped:

    Code:
    gi $bakDir  | foreach {$zipfile.CopyHere($_.fullname)} | Out-Null
    I have also tried
    Code:
    $job = gi $bakDir  | foreach {$zipfile.CopyHere($_.fullname)}
    $job | wait-job
    And still nothing works, the connection to the mapped drive is closed way too quickly.

    Is there anything else I could do?
    Thanks,

    JD

  • #2
    Re: Need Powershell script to wait on previous statement

    Originally posted by JonasDavis View Post
    still nothing works, the connection to the mapped drive is closed way too quickly.
    The problem is that you zip up stuff via tricking Explorer (Windows' shell). The control from zipping returns immediately, and actual packing is done in background.

    The most straight-forward solution would be using a real compression utility. Buy PKZip or use open source like 7-zip.

    -vP

    Comment

    Working...
    X