none
How to force PS to wait for .exe process to finish before continuing through script? RRS feed

  • General discussion

  •  

    Hello!

    I am quite new to PowerShell.

    I have a script that does the following:
     1. Loops through files in a folder to compress them and move them to a "temp" folder
     2. Copies all files in the "temp" folder to an AWS S3 bucket
     3. Moves all files from the "temp" folder to a separate different folder on the LAN

    My issue is with the part of the script that uploads copies of the file to S3 (step #2).  When I call the application normally in the script, it runs fine, but the script doesn't wait for that part of the process (#2 above) to finish before moving onto the next part of the script (#3 above).  This results in my files not being uploaded to S3.  So, (I'll just include the problematic part of the script). . . 

    ### 1. Zip/encrypt file; move to $TempDir ###
    		#RESULT: (the associated code completes as expected)
    
    ### 2. Transfer to the AWS S3 location specified ###
    & "C:\TestOut\S3Sync\S3Sync.exe" -AWSAccessKeyId ABCXYZABCXYZABCXYZAB -AWSSecretAccessKey 9Ium89Ium89Ium89Ium89Ium89Ium89Ium89Ium8 -SyncDirection upload -LocalFolderPath "C:\TestOut\Temp" -BucketName MyDatabaseBackups -S3FolderKeyName "TestBackup/" 
    		#RESULT: The file is *NOT* transferred to S3
    		
    ### 3. Moves the compressed/encrypted file to specified directory on the LAN ###
    		#RESULT: (the associated code completes as expected)


    What's happening here is the script is moving so quickly, it's getting to step #3 before the .exe in step #2 can get initiated and upload the file to S3.  (I know this because if there are multiple files included in the operation, some or all of them are uploaded as expected.)

    My search for a solution has led me to try piping the command to "out-host" or to "out-null".  However, when I try either of these, the error I get is:

    Program 'S3Sync.exe' failed to run: The requested operation requires elevationAt C:\TestOut\MyScripts\ZipEncryptMove2.ps1:65 char:5
    +     & "C:\TestOut\S3Sync\S3Sync.exe" -AWSAccessKeyId AKIAJ7MSN3LQB6E3 ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
    At C:\TestOut\MyScripts\ZipEncryptMove2.ps1:65 char:5
    +     & "C:\TestOut\S3Sync\S3Sync.exe" -AWSAccessKeyId AKIAJ7MSN3LQB6E3 ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
        + FullyQualifiedErrorId : NativeCommandFailed


    Also, I've tried the following:

    $S3Sync = "C:\TestOut\S3Sync\S3Sync.exe"
    $S3SyncArgs = "-AWSAccessKeyId ABCXYZABCXYZABCXYZAB -AWSSecretAccessKey 9Ium89Ium89Ium89Ium89Ium89Ium89Ium89Ium8 -SyncDirection upload -LocalFolderPath "C:\TestOut\Temp" -BucketName MyDatabaseBackups -S3FolderKeyName "TestBackup/"
    &$S3Sync $S3SyncArgs | echo "Waiting"

    . . . with the resulting error:

    Program 'S3Sync.exe' failed to run: The requested operation requires elevationAt C:\TestOut\MyScripts\ZipEncryptMove2.ps1:64 char:5
    +     &$S3Sync $S3SyncArgs | echo ;">+     ~~~~~~~~~~~~~~~~~~~~.
    At C:\TestOut\MyScripts\ZipEncryptMove2.ps1:64 char:5
    +     &$S3Sync $S3SyncArgs | echo "Waiting"
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
        + FullyQualifiedErrorId : NativeCommandFailed

    Finally, I tried the "Start-Process" command with the "-Wait" flag.  But, that didn't work either.

    I have already set the properties of the S3Sync.exe file to "Run as administrator".  I'm not sure why I'm being told it requires elevation.  Just not sure what my options are.  A point in the right direction would be greatly appreciated!

    Thanks!

    • Changed type Bill_Stewart Friday, July 27, 2018 8:18 PM
    • Moved by Bill_Stewart Friday, July 27, 2018 8:19 PM This is not third-party support forum
    Tuesday, May 15, 2018 8:22 PM

All replies

  • Apparently s3sync.exe runs asynchronously from your script.

    You could try executing it with Start-Process (using the -Wait parameter) and see if that blocks script execution before proceeding to step 3.

    Alternatively, you will need to have a way to determine if step 2 is completed and wait for that to happen before proceeding to step 3.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, May 15, 2018 8:27 PM
  • A folder sync is asynchronous and cannot be waited on.

    Post in vendor forum for help on using this utility function.

    https://sprightlysoft.com/S3Sync/


    \_(ツ)_/

    Tuesday, May 15, 2018 9:51 PM
  • ...which is what I was saying. If the program runs asynchronously, you need a way to detect if it's finished.

    As jrv has noted - this is not a support forum for that program.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, May 15, 2018 9:54 PM