Removing a file from an FTP server using PowerShell

I wrote a PowerShell script to upload files using FTPto on my local computer.

After downloading the file, I want to delete it from the FTP server. I also wrote this code. But, unfortunately, it does not work.

Can someone help me point out what is wrong with my code? Any hints would be helpful ...

Here is my code

function Delete-File($Source,$Target,$UserName,$Password) { $ftprequest = [System.Net.FtpWebRequest]::create($Source) $ftprequest.Credentials = New-Object System.Net.NetworkCredential($UserName,$Password) if(Test-Path $Source) { "ABCDEF File exists on ftp server." $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile $ftprequest.GetResponse() "ABCDEF File deleted." } } function Get-FTPFile ($Source,$Target,$UserName,$Password) { # Create a FTPWebRequest object to handle the connection to the ftp server $ftprequest = [System.Net.FtpWebRequest]::create($Source) # set the request network credentials for an authenticated connection $ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password) if(Test-Path $targetpath) { "ABCDEF File exists" } else { "ABCDEF File downloaded" $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile $ftprequest.UseBinary = $true $ftprequest.KeepAlive = $false Delete-File $sourceuri $targetpath $user $pass } # send the ftp request to the server $ftpresponse = $ftprequest.GetResponse() # get a download stream from the server response $responsestream = $ftpresponse.GetResponseStream() # create the target file on the local system and the download buffer $targetfile = New-Object IO.FileStream ($Target,[IO.FileMode]::Create) [byte[]]$readbuffer = New-Object byte[] 1024 # loop through the download stream and send the data to the target file do{ $readlength = $responsestream.Read($readbuffer,0,1024) $targetfile.Write($readbuffer,0,$readlength) } while ($readlength -ne 0) $targetfile.close() } $sourceuri = "ftp://ftpxyz.com/vit/ABCDEF.XML" $targetpath = "C:\Temp\M\NEWFOLDER\ABCDEF.XML" $user = "*******" $pass = "*******" Get-FTPFile $sourceuri $targetpath $user $pass Delete-File $sourceuri $targetpath $user $pass 

Every time I execute this script, the only statement I get

ABCDEF file uploaded

or

ABCDEF file exists

I think Delete-File not executed at all ... any type of hint will be helpful.

+5
source share
2 answers

You cannot use Test-Path with an FTP URL. Thus, your code to delete the file will never be executed.

Just remove the Test-Path condition and try to delete the file unconditionally. Then check for the error and handle the "file does not exist" error as you like.

 $ftprequest = [System.Net.FtpWebRequest]::create($Source) $ftprequest.Credentials = New-Object System.Net.NetworkCredential($UserName, $Password) try { $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile $ftprequest.GetResponse() | Out-Null Write-Host ("File {0} deleted." -f $Source) } catch { if ($_.Exception.InnerException.Response.StatusCode -eq 550) { Write-Host ("File {0} does not exist." -f $Source) } else { Write-Host $_.Exception.Message } } 

Although when you try to delete a file only after it has been successfully downloaded, it is actually unlikely that the file will not exist.

Thus, you can refuse any specific error handling.

+3
source

I checked your script locally to try and found some problems. I also reorganized several things to make it more readable (at least in my opinion :)).

Questions

  • Line 13. $Source indicates the path ftp://... Test-Path will always return $false here, and the delete request will never be executed.
  • In Get-FTPFile you did not refer to the input parameter of the function, but to variables defined outside it. I do not know if it was just a copy and paste of an error or for the purpose. In my opinion, you should use the parameters that you sent to the function. Lines 38, 39 and 50, at least in my code below.

Code

 function Delete-File { param( [string]$Source, [string]$Target, [string]$UserName, [string]$Password ) $ftprequest = [System.Net.FtpWebRequest]::create($Source) $ftprequest.Credentials = New-Object System.Net.NetworkCredential($UserName,$Password) if(Test-Path $Source) { "ABCDEF File exists on ftp server." $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile $ftprequest.GetResponse() "ABCDEF File deleted." } } function Get-FTPFile { param( [string]$Source, [string]$Target, [string]$UserName, [string]$Password ) # Create a FTPWebRequest object to handle the connection to the ftp server $ftprequest = [System.Net.FtpWebRequest]::create($Source) # set the request network credentials for an authenticated connection $ftprequest.Credentials = New-Object System.Net.NetworkCredential($UserName,$Password) if(Test-Path $Target) { "ABCDEF File exists" } else { "ABCDEF File downloaded" $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile $ftprequest.UseBinary = $true $ftprequest.KeepAlive = $false Delete-File $Source $Target $UserName $Password } # send the ftp request to the server $ftpresponse = $ftprequest.GetResponse() # get a download stream from the server response $responsestream = $ftpresponse.GetResponseStream() # create the target file on the local system and the download buffer $targetfile = New-Object IO.FileStream ($Target,[IO.FileMode]::Create) [byte[]]$readbuffer = New-Object byte[] 1024 # loop through the download stream and send the data to the target file do{ $readlength = $responsestream.Read($readbuffer,0,1024) $targetfile.Write($readbuffer,0,$readlength) } while ($readlength -ne 0) $targetfile.close() } $sourceuri = "ftp://ftpxyz.com/vit/ABCDEF.XML" $targetpath = "C:\Temp\M\NEWFOLDER\ABCDEF.XML" $user = "*******" $pass = "*******" Get-FTPFile $sourceuri $targetpath $user $pass #Delete-File $sourceuri $targetpath $user $pass 

There are also ready-made PowerShell cmdlets for communicating with FTP / SFTP, you do not need to create everything from scratch, unless you need it.

In any case, for reference, check for example.

+3
source

Source: https://habr.com/ru/post/1276267/


All Articles