PowerShell を使用して、データベース バックアップ ファイルの FTP ダウンロードを自動化したいと考えています。ファイル名には日付が含まれているため、毎日同じ FTP スクリプトを実行することはできません。PowerShell に組み込まれている、または .NET フレームワークを使用してこれを行うクリーンな方法はありますか?
安全な FTP セッションを使用したい。
PowerShell を使用して、データベース バックアップ ファイルの FTP ダウンロードを自動化したいと考えています。ファイル名には日付が含まれているため、毎日同じ FTP スクリプトを実行することはできません。PowerShell に組み込まれている、または .NET フレームワークを使用してこれを行うクリーンな方法はありますか?
安全な FTP セッションを使用したい。
いくつかの実験の後、PowerShell で安全な FTP ダウンロードを自動化するこの方法を思いつきました。このスクリプトは、Chilkat Software が管理する公開テスト FTP サーバーから実行されます。したがって、このコードをコピーして貼り付けると、変更なしで実行できます。
$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()
これらのリンクで多くの役立つ情報を見つけました
SSL 接続を使用する場合は、次の行を追加する必要があります。
$ftprequest.EnableSsl = $true
GetResponse() を呼び出す前にスクリプトに追加します。場合によっては、期限切れのサーバー セキュリティ証明書を処理する必要がある場合があります (残念ながら私もそうです)。そのためのコード スニペットを含むページがPowerShell コード リポジトリにあります。最初の 28 行は、ファイルをダウンロードする目的に最も関連しています。
ここから撮影:
$source = "ftp://ftp.microsoft.com/ResKit/win2000/dureg.zip"
$target = "c:\temp\dureg.zip"
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($source, $target)
わたしにはできる。
PowerShellに関する限り、/ n Software NetCmdletsパッケージには、これに非常に簡単に使用できるFTPコマンドレット(両方の安全なFTPタイプのサポートを含む)が含まれています。
JAMS ジョブ スケジューラには、このタスクを簡単にするいくつかのコマンドレットが用意されています。安全なセッションのためのさまざまな FTP コマンドレットと、自然な日付を「月の最終日」などの .NET 日付オブジェクトに変換するための日付コマンドレットがあります。
これは私が望むほど単純ではありません。私が知っている3つのオプションがあります。
.NET - .NET フレームワークを使用して PowerShell でこれを行うことができますが、これには、スクリプトで行いたくない生のソケット操作が含まれます。もし私がこの方法をとっていたら、すべての FTP ジャンクを C# の DLL にラップし、PowerShell からその DLL を使用します。
ファイルの操作 - 毎日取得する必要があるファイル名のパターンがわかっている場合は、PowerShell で FTP スクリプトを開き、スクリプト内のファイル名を変更するだけです。次に、スクリプトを実行します。
テキストを FTP にパイプする - 最後のオプションは、PowerShell を使用して FTP セッションとの間で情報をパイプすることです。ここを参照してください。
このようなものはうまくいくかもしれません:
$bkdir = "E:\BackupsPWS" #backups location directory
$7Zip = 'C:\"Program Files"\7-Zip\7z.exe' #compression utility
$files_to_transfer = New-Object System.Collections.ArrayList #list of zipped files to be transferred over FTP
$ftp_uri="myftpserver"
$user="myftpusername"
$pass="myftppassword"
# Find .bak files not zipped yet, zip them, add them to the list to be transferrd
get-childitem -path $bkdir | Sort-Object length |
where { $_.extension -match ".(bak)" -and
-not (test-path ($_.fullname -replace "(bak)", "7z")) } |
foreach {
$zipfilename = ($_.fullname -replace "bak", "7z")
Invoke-Expression "$7Zip a $zipfilename $($_.FullName)"
$files_to_transfer.Add($zipfilename)
}
# Find .bak files, if they've been zipped, delete the .bak file
get-childitem -path $bkdir |
where { $_.extension -match ".(bak)" -and
(test-path ($_.fullname -replace "(bak)", "7z")) } |
foreach { del $_.fullname }
# Transfer each zipped file over FTP
foreach ($file in $files_to_transfer)
{
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) # FTP credentials
$ftp_urix = $ftp_uri + "/" + $file.Substring($bkdir.Length + 1) # ftp address where to transfer the file
$uri=[system.URI] $ftp_urix
$webclient.UploadFile($uri, $file) #transfer the file
}
これを確認してください:Powershell:バックアップとFTP転送を圧縮します
$realdate = (Get-Date).ToString("yyyyMMdd")
$path = "D:\samplefolderstructure\filename" + $realdate + ".msi"
ftps -f $path -s:D:\FTP.txt
ftp.txt は、FTP サーバーへのすべての接続情報を保持する方法です。ftps
は明らかに使用するクライアントであるため、いくつかの変更が必要になる場合があります。しかし、これはアイデアを得るのに役立つはずです。
Indy Project .NET ライブラリを使用して FTP を実行することに成功しました。そして... うーん、ホストされた .NET ビルドは利用できなくなったようです。