#author("2021-11-15T02:33:11+00:00","default:admin","admin") #author("2021-11-15T02:38:05+00:00","default:admin","admin") -[[PowerShellを使用してファイルをSFTPにアップロードします:https://www.fixes.pub/program/445748.html]] -[[Powershellを使ってFTPでデータ取得:https://qiita.com/arachan@github/items/896921ac232c1de97021]] *実装 [#r42acf4c] **WinSCP使用 [#n5bbf356] -[[Using WinSCP .NET Assembly from PowerShell:https://winscp.net/eng/docs/library_powershell]] ***[[Session Class - WinSCP:https://winscp.net/eng/docs/library_session]] [#qe02872b] -[[Session.Open Method:https://winscp.net/eng/docs/library_session_open]] -[[Session.Close Method:https://winscp.net/eng/docs/library_session_close]] -[[Session.GetFiles Method:https://winscp.net/eng/docs/library_session_getfiles]] -[[Session.GetFilesToDirectory Method:https://winscp.net/eng/docs/library_session_getfilestodirectory]] -[[Session.PutFiles Method:https://winscp.net/eng/docs/library_session_putfiles]] -[[Session.PutFilesToDirectory Method:https://winscp.net/eng/docs/library_session_putfilestodirectory]] ***実装例 [#dc615217] -[[WinSCPを自動化しても別にかまわんのだろ:https://qiita.com/mima_ita/items/35261ec39c3c587210d8]] -[[【さくらVPS】PowerShellでデータをバックアップする【WinSCP】:https://papamamameyou.com/%E3%80%90%E3%81%95%E3%81%8F%E3%82%89vps%E3%80%91powershell%E3%81%8B%E3%82%89%E3%83%87%E3%83%BC%E3%82%BF%E3%82%92%E3%83%90%E3%83%83%E3%82%AF%E3%82%A2%E3%83%83%E3%83%97%E3%81%99%E3%82%8B%E3%80%90winscp/]] ***テストスクリプト [#u3d052c3] # # SFTP TestScript with WinSCP # 1) upload files. # 2) download 1) files. # # reference) # https://qiita.com/mima_ita/items/35261ec39c3c587210d8 # https://winscp.net/eng/docs/library_session # # Set TestPath $testDirectory = Get-Date -Format "yyyyMMdd_HHmm"; $uploadLocalPath = "C:\var\test\upload"; $downloadLocalPath = "C:\var\test\download\${testDirectory}"; $testDirectory = Get-Date -Format "yyyyMMdd_HHmm" $uploadLocalPath = "C:\var\test\upload" $downloadLocalPath = "C:\var\test\download\${testDirectory}" try { # Load WinSCP .NET assembly Add-Type -Path 'C:\Program Files (x86)\WinSCP\WinSCPnet.dll'; Add-Type -Path 'C:\Program Files (x86)\WinSCP\WinSCPnet.dll' # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp; HostName = "localhost"; UserName = "test"; Password = "XXXXXXXX"; GiveUpSecurityAndAcceptAnySshHostKey = $True; Protocol = [WinSCP.Protocol]::Sftp HostName = "localhost" UserName = "test" Password = "XXXXXXXX" GiveUpSecurityAndAcceptAnySshHostKey = $True } $session = New-Object WinSCP.Session; $session = New-Object WinSCP.Session try { # Connect $session.Open($sessionOptions); $session.Open($sessionOptions) # Force binary mode transfer $transferOptions = New-Object WinSCP.TransferOptions; $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary; $transferOptions = New-Object WinSCP.TransferOptions $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary # Create Remote TestDirectory if (!$session.FileExists($testDirectory)) { $session.CreateDirectory($testDirectory); $session.CreateDirectory($testDirectory) } # Upload files to TestDirectory $transferResult = $session.PutFiles($uploadLocalPath, $testDirectory, $False, $transferOptions); $transferResult.Check(); $session.PutFiles($uploadLocalPath, $testDirectory, $False, $transferOptions) $transferResult.Check() # Download files from TestDirectory if (!(Test-Path $downloadLocalPath)) { New-Item $downloadLocalPath -ItemType Directory; New-Item $downloadLocalPath -ItemType Directory } $transferResult = $session.GetFiles($testDirectory, $downloadLocalPath, $False, $transferOptions); $session.GetFiles($testDirectory, $downloadLocalPath, $False, $transferOptions) # Throw on any error to emulate the default "option batch abort" $transferResult.Check(); $transferResult.Check() } finally { # Disconnect, clean up $session.Dispose(); $session.Dispose() } exit 0; exit 0 } catch { Write-Host "Error: $($_.Exception.Message)"; exit 1; Write-Host "Error: $($_.Exception.Message)" exit 1 }