4 Replies Latest reply on Feb 3, 2020 9:22 AM by jvm2016

    ftp_powershell

    jvm2016 Expert

      Hi Luc ,

       

      this is in continuation of the last thread we discussed about vsphere best practices .

       

       

      i need to put backup directory under ftp server using powershell

      i want to put that piece of code in the main script .

      however i  am not able to connect ftp server using powershell

       

        • 1. Re: ftp_powershell
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          So we are on the same page, which OS are you doing this on?
          And how did you set up the FTP server?
          Is that with this script attached in The specified item was not found. 

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: ftp_powershell
            jvm2016 Expert

            yes that he same script i attached .we discussed in last project thread about setting up the ftp server .

            windows 2012 r2  is the os

            i am giving backup location as

            ftp://192.168.110.10:21/backup

             

            though its in location path but do i need to give logic in script to create .

             

            for some reasons working on ftp server works on comaand prompt not on powershell console .

            • 3. Re: ftp_powershell
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              With the following script I can upload a file to an FTP server.
              The script will create the required folders (in $ftpPath) if they do not exist yet.

              The name of the uploaded file can be changed with $ftpFile.

               

              $ftpServer = 'localhost'

              $ftpPath = 'Test/SubFolder'

              $ftpFile = 'MyTest.txt'

              $srcFile = 'C:\Test.txt'


              $Username = "FTPUSER"

              $Password = "VMware1!"


              $webclient = New-Object System.Net.WebClient

              $webclient.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)


              $pwd = ''

              $ftpPath.Split('/') | ForEach-Object -Process {

                  try{

                      $pwd = "$pwd/$_"

                      $tPath = "ftp://$($ftpServer)$($pwd)"

                      $tDir = [System.Net.WebRequest]::Create($tPath)

                      $tDir.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)

                      $tDir.Method = [System.Net.WebRequestMethods+FTP]::MakeDirectory

                      $tDir.GetResponse() | Out-File

                  }

                  catch [Net.WebException] {

                      try{

                              $cDir = [System.Net.WebRequest]::Create($tPath)

                              $cDir.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)

                              $cDir.Method = [System.Net.WebRequestMethods+FTP]::PrintWorkingDirectory

                              $cDir.GetResponse() | Out-File

                      }

                      catch [Net.WebException] {

                          throw "Unknown error"

                      }  

                  }

              }


              $uri = New-Object System.Uri("ftp://$ftpServer/$ftpPath/$ftpFile")

              $webclient.UploadFile($uri,$srcFile)

              $webclient.Dispose()

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: ftp_powershell
                jvm2016 Expert

                thnaks Luc .iam going to check this .