Download all files in FTP folder with Powershell [duplicate] - powershell

This question already has answers here:
PowerShell FTP download files and subfolders
(2 answers)
Closed 4 years ago.
The vendor I'm working with uploads zip files to an FTP. I need to download whatever is uploaded there and process, as needed.
Using Powershell, how do I download *.* from an FTP folder?
(In reference to https://social.technet.microsoft.com/Forums/office/en-US/744ee28a-9340-446a-b698-4b96e081b501/download-files-from-ftp-server?forum=winserverpowershell)
# Config
$Username = "user"
$Password = "password"
$LocalFile = "C:\tools\file.zip"
$RemoteFile = "ftp://myftpserver:22/Folder1/Folder/file.csv"
# Create a FTPWebRequest
$FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile)
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UseBinary = $true
$FTPRequest.KeepAlive = $false
$ftpRequest.EnableSsl = $true
# Send the ftp request
$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
$LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create)
[byte[]]$ReadBuffer = New-Object byte[] 1024
# Loop through the download
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$LocalFileFile.Write($ReadBuffer,0,$ReadLength)
}
while ($ReadLength -ne 0)
Is there any way to make $RemoteFile something like ftp://myftpserver:22/Folder1/Folder/*.zip or ftp://myftpserver:22/Folder1/Folder/*.*
My apologies if there is a post the same. I saw some similar, but not close enough to answer the question.

I have created PowerShell SFTP, FTP, and FTPS script. You will have to download it from my github. I do not use any third party applications, becuase I do not trust them. What I use is REBEX.dll for the SFTP section and .Net WebRequest for FTP and FTPS.
https://github.com/FallenHoot/ZOTECH-PS/blob/master/SFTP.zip
If you have issues understanding the code please let me know. If you are not going to use the Get-SFTP function just comment it out.

Related

Upload file using SFTP in PowerShell

I am trying the below command but didn't get any output:
echo "put C:\Users\abhishek.chawla\Desktop\AbhishekC_bills.zip deployment/AbhishekC_bills.zip" | sftp TESTSHELTER#shelter-ftp.outlinesys.com
Do I need to add the password too? If yes, then where to add the password?
One of the workaround you can follow ,
To provide the password into the command for upload files to SFTP using PowerShell through Winscp below is the example of code.
cmdltes:-
Add-Type -Path "WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions -Property #{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "example.com"
UserName = "user"
Password = "mypassword"
SshHostKeyFingerprint = "ssh-rsa 2048 xx...="
}
$session = New-Object WinSCP.Session
try
{
$session.Open($sessionOptions)
$session.PutFiles("C:\users\export.txt","/Outbox/").Check()
}
finally
{
$session.Dispose()
}
For complete information please refer this BLOG|Use WinSCP to Upload Files to SFTP Site With PowerShell .
For more information regarding similar issue please refer the below links:-
SO THREAD| Power Shell File Upload to SFTP with File Existence Check & Upload file to SFTP using PowerShell .

CSOM - Download file in chunks

I'm working on a CSOM-based PowerShell-Script to manage data in SharePoint-Online. The current challenge is to give the user some feedback regarding the download status for large files.
When uploading files, this can be done by splitting the file into segments and upload them using $File.StartUpload(), $File.ContinueUpload() and $File.FinishUpload().
For downloading on the other hand I only have the $File.OpenBinaryStream() method which will cause the full file to get downloaded at once.
Sample code:
$Cred = Get-Credential
$CTX = [Microsoft.SharePoint.Client.ClientContext]::new("https://domain.sharepoint.com/sites/collection1/site1/")
$CTX.Credentials = [Microsoft.SharePoint.Client.SharePointOnlineCredentials]::new($Cred.UserName, $Cred.Password)
$CTX.Load($CTX.Web)
$CTX.ExecuteQuery()
$File = $CTX.Web.GetFileByServerRelativeUrl("/sites/collection1/site1/Ordner/Datei.zip")
$CTX.Load($File)
$CTX.ExecuteQuery()
$Stream = $File.OpenBinaryStream()
$CTX.ExecuteQuery()
#...
#Further steps to copy the $Stream.Value to a local System.IO.Stream on my harddrive
My script will simply freeze at the last CTX.ExecuteQuery() until the file download is done in the background. Are there any alternatives?

Powershell script to download file from FTPS server

I have seen mixed posts but nothing definitive and recent. I am running Filezilla Server FTPS Explicit TLS. I need a Powershell script that when run will securely connect to my server across the internet and download a specific file. Does the below code looks good minus the TLS, and can someone help me finish it? I think all of the commands I would need would be from here https://winscp.net/eng/docs/library_sessionoptions#ftpsecure
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property #{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "ftp.example.com"
UserName = "user"
Password = "mypassword"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Download files
$session.GetFiles("/directory/to/download/*", "C:\target\directory\*").Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

Powershell Sharepoint snapin when not on the sharepoint server

I am new to both powershell and sharepoint, and I need to make script to automate the removal and uploading of attachments from outlook to sharepoint. I have easily completed the first part of extracting the attachment, however the uploading to sharepoint has become difficult do to my company's rules. As I understand to use sharepoint cmdlets you need to add the sharepoint snap-in but I am unable to do so because I dont have access to the sharepoint server. Is there anyway to the snapin without being on the server and if not can I upload it another way?
You can't add the SP snap in unless the server is a SP server. Instead, use a webservice/webclient approach to upload the file. Something like this should work depending on your SP version:
http://blog.sharepoint-voodoo.net/?p=205
Accepted answer link is broken.
This script uses PowerShell to upload a file to a document library in SharePoint using purely web service calls so it could be done remotely, also meaning it should work with O365 though I have not tried.
These variables are used throughout the script for source file, destination file and authentication. If your workstation is on the same domain as SharePoint, and your logged on user has permissions to the SharePoint site, you can omit $username, $password, and $domain
$LocalPath = "C:\filename.docx"
$spDocLibPath = "http://site.contoso.com/sites/spteam/Shared Documents/"
$username = "someone"
$password = "somepassword"
$domain = "contoso"
$UploadFullPath = $spDocLibPath + $(split-path -leaf $LocalPath)
$WebClient = new-object System.Net.WebClient
if($username -eq "" -or $password -eq "" -or $password -eq "")
{
# Use Local Logged on User Credentials
$WebClient.Credentials = [System.Net.CredentialCache]::DefaultCredentials
}
else
{
# Alternate Login for specifying credentials
$WebClient.Credentials = new-object System.Net.NetworkCredential($username, $password, $domain)
}
$WebClient.UploadFile($UploadFullPath, "PUT", $LocalPath)
https://web.archive.org/web/20160404174527/http://blog.sharepoint-voodoo.net/?p=205

Powershell FTPWebRequest and EnableSsl = True

I searched this site and found an FTPWebRequest example via Powershell. I put it to use and it works fine. However, when I enable SSL via EnableSsl=$True, all I get is timeouts or a delayed "227 Entering Passive Mode", which breaks the process. As soon as I disable EnableSsl, I can fly right through. Can someone point me in the right direction? SSL is supported on the FTP host.
I'd eventually like change the method to DownloadFile and loop the code to download files, after I get the list and find matches. I'd like to do it securely, though.
# Create an FTPWebRequest object to handle the connection to the FTP server
$ftprequest = [System.Net.FtpWebRequest]::Create($sourceuri)
# Set the request's network credentials for an authenticated connection
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)
# Set FTPWebRequest method to ListDirectory
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
$ftprequest.EnableSsl = $True
$ftprequest.UseBinary = $False
$ftprequest.UsePassive = $True
$ftprequest.KeepAlive = $False
$ftpresponse = $ftprequest.GetResponse()
Write-Out $ftpresponse.StatusCode
Write-Out $ftpresponse.StatusDescription
Come to find out, spontaneous issue was server-side.