Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http://samplesite.com/sample1/image1.jpg
- try
- {
- # Load WinSCP .NET assembly
- Add-Type -Path "WinSCPnet.dll"
- # Setup session options
- $sessionOptions = New-Object WinSCP.SessionOptions
- $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
- $sessionOptions.HostName = "example.com"
- $sessionOptions.UserName = "user"
- $sessionOptions.Password = "mypassword"
- $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
- $session = New-Object WinSCP.Session
- $remoteRoot = "/home/user"
- try
- {
- # Connect
- $session.Open($sessionOptions)
- foreach ($line in [System.IO.File]::ReadLines("list.txt"))
- {
- if ($line -Match "http://[a-z.]+(/(.*)/[a-z0-9.]+)$")
- {
- $remotePath = $matches[1]
- $remoteDir = $matches[2]
- $localDir = $remoteDir -Replace "/", ""
- if (!(Test-Path $localDir))
- {
- Write-Host "Creating directory $localDir"
- New-Item $localDir -Type directory | Out-Null
- }
- Write-Host "Downloading $remotePath"
- $session.GetFiles(($remoteRoot + $remotePath), ($localDir + "")).Check()
- }
- else
- {
- Write-Host "$line does not have expected URL format"
- }
- }
- }
- finally
- {
- # Disconnect, clean up
- $session.Dispose()
- }
- exit 0
- }
- catch [Exception]
- {
- Write-Host $_.Exception.Message
- exit 1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement