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
Post a Comment