国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 操作系統 > 正文

PowerShell之SFTP下載文件

2024-06-28 16:04:45
字體:
來源:轉載
供稿:網友

SFTP是一種加密的安全的文件傳輸協議。通過PowerShell腳本進行SFTP傳輸相當方便,還可用于定時任務。我們將一些配置項寫入到一個xml文件中,通過PowerShell讀取xml內容。

下面是download.xml的代碼:

<?xml version="1.0" ?><sftpConfig> <PRotocol>Sftp</Protocol> <HostName>127.0.0.1</HostName> <UserName>admin</UserName> <PassWord>Password</Password> <SshHostKeyFingerprint>ssh-rsa 2048 32:af:45:6b:a0:e5:0d:f3:1e:b0:44:b7:de:ec:77:b3</SshHostKeyFingerprint> <RemotePath>/E/test/path/data/</RemotePath> <LocalPath>e:/test/path/data/</LocalPath></sftpConfig>

下面是download.ps1腳本的代碼:

param ( $xmlData=[xml](Get-Content "E:/test/path/scripts/download.xml"), $localPath = $xmlData.sftpConfig.LocalPath, $remotePath = $xmlData.sftpConfig.RemotePath, $dataFile = (Get-Date).toString("yyyy-MM-dd")+".txt", $flagFile = "flag_"+(Get-Date).toString("yyyyMMdd")+".txt")function wait10sec{ $step=10 $add=0 $t=(get-date) $step-(($t.Hour*3600+$t.Minute*60+$t.Second)%$step)+$add}try{ # Load WinSCP .NET assembly Add-Type -Path "E:/test/path/scripts/WinSCPnet.dll" # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions $sessionOptions.Protocol = [WinSCP.Protocol]$xmlData.sftpConfig.Protocol $sessionOptions.HostName = $xmlData.sftpConfig.HostName $sessionOptions.UserName = $xmlData.sftpConfig.UserName $sessionOptions.Password = $xmlData.sftpConfig.Password $sessionOptions.SshHostKeyFingerprint = $xmlData.sftpConfig.SshHostKeyFingerprint $session = New-Object WinSCP.Session try { # Connect $session.Open($sessionOptions) # Detect the flag file while(! $session.FileExists($remoteRawPath+$flagFile)) { Get-Date Start-Sleep -s (wait10sec) } write-host "Start Downloading..." #if the data file or flag file exists,delete file first if((Test-Path ($localPath+$dataFile)) -or (Test-Path ($localPath+$flagFile))) { { write-host $dataFile" exists and will be removed!" Remove-Item ($localPath+$dataFile) } if(Test-Path ($localPath+$flagFile)) { write-host $finishFile" exists and will be removed!" Remove-Item ($localPath+$flagFile) } } $session.GetFiles($session.EscapeFileMask($remotePath + $dataFile), $localPath).Check() New-Item ($localPath+$flagFile) -ItemType file write-host "End Download" } finally { # Disconnect, clean up $session.Dispose() } exit 0}catch [Exception]{ Write-Host ("Error: {0}" -f $_.Exception.Message) exit 1}

該腳本是通過WinSCP進行SFTP連接的,因此需要添加一個WinSCPnet.dll動態文件。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 冀州市| 慈利县| 蓝田县| 鱼台县| 集贤县| 吉林省| 定西市| 泸州市| 汉沽区| 金坛市| 探索| 镇宁| 大渡口区| 手游| 罗田县| 河南省| 和平区| 东乡| 渭南市| 临澧县| 甘德县| 乌拉特中旗| 高台县| 西乡县| 堆龙德庆县| 都匀市| 平顶山市| 仁化县| 类乌齐县| 东乡族自治县| 南靖县| 和平区| 山阴县| 三明市| 宁波市| 和平县| 吉木乃县| 大渡口区| 淳化县| 伊金霍洛旗| 敦化市|