Need help on error handler - Issue ErrorAction Stop


all,

with script want upload file on ftp , delete them.

the thing want stop , send alert email, if failed upload whatever reason

here code

$ftp = "ftp://aa.bb.cc.dd/"   $user = "test"   $pass = "pass"  $settype = "bin"    $files = get-childitem 'c:\test\' -recurse| where-object {(!($_.psiscontainer))}  $webclient = new-object system.net.webclient   foreach($item in $files){       $uri = new-object system.uri($ftp+$item.name)         	try  	{   	$erroractionpreference = "stop"  	$webclient.uploadfile($uri,$item.fullname)  	}  	catch  	{    	  send-mailmessage -from emailfrom -to emailto -subject 		  "error uploading files ftp" -smtpserver xxx.xx.com  	break  	}  }   remove-item "c:\test\*" -recurse -force

in try tried add -erroraction stop failed, , error action preference = stop, continue , try send email when connection ok

if can me gratefull...

thanks lot in advance.

best regards,

m.

i dont think need send-mailmessage in catch, since once issue happens going run whatever in catch put after catch


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Group Policy Event ID 1058 Error Code 1326 (The user name or password is incorrect)

Suspicious event log Event ID: 4905

DCOM received error "2147746132" from...