Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catch error from calling another program

Status
Not open for further replies.

perlnewbie9292

Programmer
Jul 15, 2009
25
US
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/[URL unfurl="true"]www/uploads"[/URL]
$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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top