none
Powershell FTP RRS feed

  • Question

  • I am trying to do this simple process

    • List FTP files
    • When Files Count = 4
    • Download all 4 files
    • Upload the for files to new ftp site

    So far I have successfully listed the files and run an IF test with this;

     

    $ftp_uri =
    $user =
    $pass =
    $subfolder =

    $ftp_urix = $ftp_uri + $subfolder $uri=[system.URI] $ftp_urix $ftp=[system.net.ftpwebrequest]::Create($uri) $ftp.Credentials=New-Object System.Net.NetworkCredential($user,$pass) $ftp #Get a list of files in the current directory. $ftp.Method=[system.net.WebRequestMethods+ftp]::ListDirectorydetails $ftp <# $ftp.UseBinary = $true $ftp.KeepAlive = $false $ftp.EnableSsl = $true $ftp.Timeout = 30000 $ftp.UsePassive=$true #> try { $ftpresponse=$ftp.GetResponse() $strm=$ftpresponse.GetResponseStream() $ftpreader=New-Object System.IO.StreamReader($strm,'UTF-8') $list=$ftpreader.ReadToEnd() $lines=$list.Split("`n") #$lines #$lines.Count $ftpReader.Close() $ftpresponse.Close() } catch{ $_|fl * -Force $ftpReader.Close() $ftpresponse.Close() } $count=$lines.count $lines <# Comment out this next line before using in WUG or get the error "A command that prompts the user failed...". write-host commands are used just for testing before using in WUG. #> if ($count -gt 3){ #SetResult to 1 so WUG will send out alerts. Unccomment the next line before using in WUG. #$Context.SetResult(1,$count); #Comment out this next line before using in WUG or get the error "A command that prompts the user failed..." write-host Greater than 3 } Else { #Comment out this next line before using in WUG or get the error "A command that prompts the user failed..." write-host Less than 3 #SetResult to 0 and WUG will assume all is good/green. Unccomment the next line before using in WUG. #$Context.SetResult(0,$count); }

    But I do not know enough about the underling FTP commands to continue the process.  I have found this code snippet

    unction Get-FtpDir ($url,$credentials) {
        $request = [Net.WebRequest]::Create($url)
        $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
        if ($credentials) { $request.Credentials = $credentials }
        $response = $request.GetResponse()
        $reader = New-Object IO.StreamReader $response.GetResponseStream() 
    	$reader.ReadToEnd()
    	$reader.Close()
    	$response.Close()
    }
    $folderPath="ftp://folder/"
    $files=Get-FTPDir $folderPath
    
    $target = "c:\folder"
    Foreach ($file in ($files | where {$_ -like "FTP_*"})){
    	$source=$folderPath+$file 
    	$WebClient.DownloadFile($source, $target)
    }
    

    But I don't know how to mesh these together

    I do understand from reading that I can't use wild cards, and will have to list and then 'foreach' to get them.

    Can someone please help me push these together


    BlankMonkey

    Friday, August 18, 2017 9:02 PM

Answers

  • You can do this.  You got this far.  Remember that this is not a free code re-writing forum.  Try to code this and post a specific question.

    You could also download the FTP module and just use that like you do any other CmdLets.

    find-module psftp|ft -wrap -auto


    \_(ツ)_/

    • Marked as answer by BlankMonkey Tuesday, August 22, 2017 4:35 PM
    Friday, August 18, 2017 11:48 PM
  • Microsoft has no support for SFTP commandline.

    This module does: https://gallery.technet.microsoft.com/Secure-FTP-Powershell-65a2f5c5


    \_(ツ)_/

    • Marked as answer by BlankMonkey Wednesday, September 20, 2017 3:48 PM
    Tuesday, August 22, 2017 4:49 PM

All replies

  • You can do this.  You got this far.  Remember that this is not a free code re-writing forum.  Try to code this and post a specific question.

    You could also download the FTP module and just use that like you do any other CmdLets.

    find-module psftp|ft -wrap -auto


    \_(ツ)_/

    • Marked as answer by BlankMonkey Tuesday, August 22, 2017 4:35 PM
    Friday, August 18, 2017 11:48 PM
  • Thank you for the encouragement, I think I have this worked out, mostly :)  The module was a great help, I usually hate using outside commands/apps/downloads, but in this case it wasn't so bad.

    I do have one last question.  I can't seem to find anywhere where I can issue an FTP command to Move a file already on the FTP server.  My vendor requires I upload it to an 'inprogress" dir, and after, to Move it to the production directory, so automated scripts will not run on partial files.  I did not see the move option in the module.  Am I missing it?  Google has suggested a rename may do the trick, but not sure the syntax.  Link or ideas?


    BlankMonkey

    Monday, August 21, 2017 9:40 PM
  • With FTP and all Unix related systems the "rename" command is used to move files.  Just use the full source and destination file name.


    \_(ツ)_/

    Monday, August 21, 2017 9:56 PM
  • Ok, I have the rename working on the FTP site.  But I am unable to move it :(

    Is it because it is up the DIR tree?

    $FTPA = "SERVER" 
    $subA = "%2ftobfm/InProgress"
    $subB = "%2ftobfm"
    
    PS C:\Users\> Rename-FTPItem ftp://$FTPA/$subA/DELETEME_TEST_FILE0001.csv -newname ftp://$FTPA/$subB/DELETEME_TEST_FILE0005.csv
    Rename-FTPItem : Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
    At line:1 char:1
    + Rename-FTPItem ftp://$FTPA/$subA/DELETEME_TEST_FILE0001.csv -newname  ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
        + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Rename-FTPItem


    BlankMonkey

    Tuesday, August 22, 2017 1:01 PM
  • Your file names are not correct.  What is "%2" doing in the name?

    All FTP servers do not support rename/move.


    \_(ツ)_/

    Tuesday, August 22, 2017 1:15 PM
  • Its actually the %2f, to 'escape' the first backslash as I understand it (not much)  But it is required and will not work without it.

    I think I have it fixed and running.  Don't know why it didn't work the first time, but I have it working now.  here is my command to rename(move) all the files;

    Get-ChildItem $LocalFile |  # This just gets the file names, as there may be other files at the upload location
    % {
    Rename-FTPItem ftp://$FTPA/$subA/$_ ../$_ -verbose
    }


    BlankMonkey


    • Edited by BlankMonkey Tuesday, August 22, 2017 3:09 PM
    Tuesday, August 22, 2017 3:09 PM
  • No need to escape anything in Windows.  Your outcome is not what you expect even if it does appear to work.


    \_(ツ)_/

    Tuesday, August 22, 2017 3:39 PM
  • So JRV had the answer, and the script I have works great, using the PSFTP cmdlet.

    BUT for anyone else walking this road, be advised, this does not work with SFTP.  I thought I had setup my test server correctly, and my script worked like a dream.  But when I went to port it to production, turns out I was using regular FTP, and not SFTP.  The entire script will have to be re written from scratch, and probably have to use a secondary program like a scheduler or WinSCP. 

    A big TY to JRV, the script I did write works great....on FTP.


    BlankMonkey

    Tuesday, August 22, 2017 4:38 PM
  • Microsoft has no support for SFTP commandline.

    This module does: https://gallery.technet.microsoft.com/Secure-FTP-Powershell-65a2f5c5


    \_(ツ)_/

    • Marked as answer by BlankMonkey Wednesday, September 20, 2017 3:48 PM
    Tuesday, August 22, 2017 4:49 PM