Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

Join Tek-Tips
*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 from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Catch error from calling another program

perlnewbie9292 (Programmer) (OP)
8 Feb 12 21:03
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.
58sniper (MIS)
20 Feb 12 11:44
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.

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!

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