INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Jobs from Indeed

Catch error from calling another program

Catch error from calling another program

(OP)
Hello,

I'm a complete newbie to powershell.  I'm trying to catch the output of the command that I'm calling, which is pscp binary.  I would like to check for errors returned when the binary is executed. Depending on the error I would like to trigger an event, like attempt to re-transfer the file(s) that returned the error or just run through all the files again. Right now if I print $status it appears to be blank/null.

CODE

$windowsLogPath = "C:\files\uploads"
$unixLogPath = "/var/www/uploads"
$privKey = "c:\keys\key"
$pscp = "C:\pscp.exe -r -scp -i " + $privKey    
$user = "uploader"
$remoteServer = "xx.xx.xx.xx"

function run-SCP
{    
    try
    {
        $runCommandConstr = $pscp + " " + $windowsLogPath+ "/*" + " " + $user+"@"+ $remoteServer + ":" + $unixLogPath    
        Invoke-Expression $runCommandConstr    
    }
    catch
    {
          [system.exception]
          "caught a system exception"
    }
}


$status = run-SCP


if ($status -eq "True")
{
    "Copy completed successfully"
}
else
{
    "Copy failed, will attempt to run the command again"    
    $retry = run-SCP
    if ( $retry -eq "True")
    {
        "Second attempt successful
    }
    else
    {
        "Failed, aborting"
    }
}

I also tried just capturing the exit status like below, then checking for that.  But it's not an accurate result b/c it will return true even if the transfer wasn't successful.  

CODE

Invoke-Expression $runCommandConstr
$status = $?

My question is how do I actually capture if any of the transfers failed or if there was a problem either due to permissions or access denied or timeout etc... All of which are reported when the command "$runCommandConstr" is executed. I just need to be able to capture that output and add some error logic to what is returned.

Thanks for the help in advanced.

RE: Catch error from calling another program

What does $error contain?

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close