1

I have a powershell script were I need to execute first to invoke .cmd file to complete a network download and then I need to process on that downloaded data. Below is my command

Runas /savecred /profile /user:myuser "cmd /c C:\Users\myuser\myfile.cmd"
ECHO $Hello

From the above command, myfile.cmd downloads a file and then I need to process it. Here powershell does not waits for the file to download and starts executing below commands. How can I make it wait for the file to download?

2 Answers 2

1

Runas.exe doesn't wait for its child process to exit, so there is no straight-forward way to do this.

There are workarounds using SysInternals' pslist or something similar in a loop, but those solutions will derail if there's more than one process with the same name.

Something along this line:

runas /savecred /profile /user:user "X:\Path\MyProgram.exe"
do {
    Start-Sleep -Seconds 1
    pslist MyProgram >$null 2>&1
} while ($LASTEXITCODE -eq 0)
Sign up to request clarification or add additional context in comments.

Comments

0

You need to wait for runas:

$cmd  = 'runas'
$args = '/savecred','/profile','/user:myuser','cmd /c C:\Users\myuser\myfile.cmd'
Start-Process $cmd $args -Wait

2 Comments

This command does not invoke Runas Command and directly jumps to next powershell commands.
Does you CMD by chance return before the download is complete?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.