Hello,
I want to know what is the best way to automate the secure FTP download for a database backup file? In powershell or even in .net framework?
Hello,
I want to know what is the best way to automate the secure FTP download for a database backup file? In powershell or even in .net framework?
I have read this blog where i saw some cool tips for the question you are asking!
http://scriptolog.blogspot.com/2007/...utomation.html
Hope this helps you!
Using Windows PowerShell Server and NetCmdlets, System Administrators can easily script common network management tasks including router configuration, switch port monitoring, directory access, VLAN administration, remote execution, etc.
After some experimentation I came up with this way to automate a secure FTP download in PowerShell. This script runs off the public test FTP server administered by Chilkat Software. So you can copy and paste this code and it will run without modification.
Helpful webpage!Code:$sourceuri = "ftp://ftp.secureftp-test.com/hamlet.zip" $targetpath = "C:\hamlet.zip" $username = "test" $password = "test" # Create a 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) $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile $ftprequest.UseBinary = $true $ftprequest.KeepAlive = $false # 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 ($targetpath,[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()
http://www.codeproject.com/KB/IP/SimpleFTPDemo.aspx
I knew there was a .Net class that implemented an FTP client so I googled and bit and found it. After reading the examples shown and a little try-and-error, I came up with this extremely simple FTP client in PowerShell below. Note that by "extremely simple" I mean "with very few features and no error detection code". This only gets one file from the FTP server. I probably wouldn't use this in production without a few changes. But it's still a nice example on how to use the .Net framework with PowerShell and this class in particular
Code:$localfile = "c:\file.txt" $remotefile = "/folder/file.txt" $ftphost = "ftp://ftpserver" $URI = $ftphost + $remotefile $username="username" $password="password" function Get-FTPFile ($URI,$localfile,$username,$password){ $credentials=New-Object System.Net.NetworkCredential($username,$password) $ftp=[System.Net.FtpWebRequest]::Create($URI) $ftp.Credentials=$credentials $ftp.UseBinary=1 $ftp.KeepAlive=0 $response=$ftp.GetResponse() $responseStream = $response.GetResponseStream() $file = New-Object IO.FileStream ($localfile,[IO.FileMode]::Create) [byte[]]$buffer = New-Object byte[] 1024 $read = 0 do{ $read=$responseStream.Read($buffer,0,1024) $file.Write($buffer,0,$read) } while ($read -ne 0) $file.close() }
You could at least link back to the blog where you found that information (including the introduction).
Bookmarks