Powershell FTP script -


i'm using script in loop download number of files server , works pretty good.

$sourceuri = "ftp://ftp.secureftp-test.com/hamlet.zip" $targetpath = "c:\hamlet.zip" $username = "test" $password = "test"  # create ftpwebrequest object handle connection ftp server $ftprequest = [system.net.ftpwebrequest]::create($sourceuri)  # set request's network credentials authenticated connection $ftprequest.credentials = new-object system.net.networkcredential($username,$password)  $ftprequest.method = [system.net.webrequestmethods+ftp]::downloadfile $ftprequest.usebinary = $true $ftprequest.keepalive = $false  # send ftp request server $ftpresponse = $ftprequest.getresponse()  # download stream server response $responsestream = $ftpresponse.getresponsestream()  # create target file on local system , download buffer $targetfile = new-object io.filestream ($targetpath,[io.filemode]::create) [byte[]]$readbuffer = new-object byte[] 1024  # loop through download stream , send data target file do{     $readlength = $responsestream.read($readbuffer,0,1024)     $targetfile.write($readbuffer,0,$readlength) } while ($readlength -ne 0)  $targetfile.close() 

but target file not there , script starts flushing command prompt errors. kinda anoying debug, of files missing. there way, turn of script there missing file , skip to next one?

here error:

exception calling "getresponse" "0" argument(s): "the remote server returned n error: (550) file unavailable (e.g., file not found, no access)." @ d:\powershell\ipo_preveri_cmd_all\ftp.ps1:37 char:5 +     $ftpresponse = $ftprequest.getresponse() +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + categoryinfo          : notspecified: (:) [], methodinvocationexception     + fullyqualifiederrorid : webexception  cannot call method on null-valued expression. @ d:\powershell\ipo_preveri_cmd_all\ftp.ps1:40 char:5 +     $responsestream = $ftpresponse.getresponsestream() +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + categoryinfo          : invalidoperation: (:) [], runtimeexception     + fullyqualifiederrorid : invokemethodonnull 

the last 6 lines repeated...

sounds asking simple try-catch. don't know of error handling [system.net.ftpwebrequest] has offer generic approach place start.

$savederroractionpreference = $erroractionpreference $erroractionpreference = "stop"   try{     # send ftp request server     $ftpresponse = $ftprequest.getresponse()      # download stream server response     $responsestream = $ftpresponse.getresponsestream()      # create target file on local system , download buffer     $targetfile = new-object io.filestream ($targetpath,[io.filemode]::create)     [byte[]]$readbuffer = new-object byte[] 1024      # loop through download stream , send data target file     do{         $readlength = $responsestream.read($readbuffer,0,1024)         $targetfile.write($readbuffer,0,$readlength)     }     while ($readlength -ne 0)      $targetfile.close() } catch {     # notify user error occured.      write-warning "unable download '$sourceuri' because: $($_.exception)" }  $erroractionpreference = $savederroractionpreference  

so if any terminating error occurs in try block processing stopped , catch block executed. looks of errors not terminating need to temporarily change stop ensure try block functions properly.

then in catch block take simplest approach , write out file path failed while providing message associated error. using above example should like:

unable download 'ftp://ftp.secureftp-test.com/hamlet.zip' because: remote server returned error: (550) file unavailable (e.g., file not found, no access).

this should stop on first error. ceasing processing avoid null expression errors.


Comments

Popular posts from this blog

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -