sftp/Simple.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | ######################################################### # # Summary: # This script demonstrates the following features: # - Connect # - Download of file # - Read drectory (enumeration) # - Disconnect # # Notes: # If you get the following error: # "cannot be loaded because the execution of scripts is disabled on this system." # Enable the execution of scripts: # Set-ExecutionPolicy RemoteSigned # # Technical support: support@smartftp.com # ######################################################### # abort on first error $ErrorActionPreference = "Stop" # to use the interop assembly, powershell 5.0 (CLR 4) or higher is required if ( [Environment] ::Is64BitProcess) { [void][Reflection.Assembly] ::LoadFrom(${env:ProgramFiles(x86)} + '\SmartFTP FTP Library\x64\Interop.sfFTPLib.dll' ) } else { [void][Reflection.Assembly] ::LoadFrom(${env:ProgramFiles} + '\SmartFTP FTP Library\Interop.sfFTPLib.dll' ) } $global = New-Object sfFTPLib.GlobalClass; # If LoadLicense is not called a trial license is automatically obtained from the activation server. The FTP Library uses WinHTTP to access # the activation server at www.smartftp.com (TLS, port 443). Ensure that your application is not blocked by any firewall. # TODO: insert the provided serial after the purchase of a license # $global.LoadLicense("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"); $ssh = New-Object sfFTPLib.SSHConnectionClass; $ssh .Host = "host" ; $ssh .Port = 22; $ssh .Username = "user" ; $ssh .Password = "password" ; # $fileLogger = $ssh.SetFileLogger(); # $fileLogger.File = "C:\ssh.log"; $ssh .Connect(); Write-Host "Connected" ; $sftp = $ssh .CreateSFTPConnection(); # $fileLogger = $sftp.SetFileLogger(); # $fileLogger.File = "C:\sftp.log"; $sftp .Connect(); Write-Host "SFTP channel opened" ; # gets current directory $home = $sftp .RealPath( "." ); Write-Host ( "RealPath succeeded. Home={0}" -f $home ) foreach ( $item in $sftp .ReadDirectory( $home )) { # if you do not use the interop, the ModifyTimeAsDate must be converted with: # [System.DateTime]::FromOADate($item.ModifyTimeAsDate).ToString("o") Write-Host ( "Name={0}, Type={1}, Size={2}, ModifyTime={3}" -f $item .Name, $item .Type, $item .Size, $item .ModifyTimeAsDate) } # Use the absolute path to the source file $sftp .DownloadFileEx( "/License.txt" , "License.txt" , [sfFTPLib.DataTransferType] ::ftpDataTransferTypeImage, 0, 0, $null ); Write-Host "DownloadFile succeeded" $ssh .Disconnect(); |