Wednesday 27 July 2011

Uploading a file to FTP from PowerShell Script

Issue: 
    To Upload a file to FTP with proper credentials.

Solution:
    Giving the Address and proper credentials, and code will be,
         
      $File=<File to Upload>
      #ftp server
      $ftp = <FTP Path + File Name>
      $user = <UName>
      $pass = <Passwd>
      $webclient = New-Object System.Net.WebClient
      $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
      "Uploading $item..."
      $uri = New-Object System.Uri($ftp)
      $webclient.UploadFile($uri, $File)

[Note:]
Files will get uploaded properly if and only if the FTP allows You with Access Permissions and Credentials (if having).

No comments:

Post a Comment