そこで、ローカルネットワーク上で大きなファイルを送信するための便利で制御されたサービスを作成するというアイデアが生まれました。
PowerShell実装、Windowsプラットフォーム。
要件:
- インターネットで公開されているWebサーバー。
- Windowsコンピューター上の共有フォルダー(ファイルの所有者を決定するため)。
- 1分に1回、スケジュールに基づいてスクリプトを実行するサーバーでのセットアップ。
仕組み:
- ユーザーは、サーバー上の共有フォルダーにZIPアーカイブを配置します。
- スクリプトは、共有フォルダーでZIPファイルを検索し、それらをWebサーバーフォルダーに移動してから、ファイル所有者にレターを送信します。
- ユーザーは、Webサーバーからファイルをダウンロードするためのリンクが記載された電子メールを受け取ります。
- ユーザーは、ダウンロードリンクを含むレターを宛先に転送します。
スクリプトの説明:
- ZIP拡張子のない宛先フォルダー内のすべてを削除します。
- 宛先フォルダー内のZIPファイルのリストを取得します。
- Send_URL関数による各ファイルの処理でZIPファイルを列挙します。
- Send_URL関数:ファイル所有者を定義し、ファイル名を処理し、新しい名前でファイルをWebサーバーフォルダーに移動し、所有者と管理者(制御用)にメールへのダウンロードリンクをファイルに送信し、特定の日数より古いWebサーバーフォルダー内のファイルを削除します。 より詳細なコメントについては、スクリプトを参照してください。
- 「ZIPアーカイブのみ、残りは.txtが削除されます」というプロンプト名で共有フォルダーにファイルを作成します。
注:ユーザーが共有フォルダーを持つサーバーのAdministratorsグループのメンバーである場合、所有者= Administratorsグループです。このため、管理メールボックスはスクリプトで指定されます。
スクリプトでは、次のパラメーターを必要なものに変更する必要があります。
$path = "C:\UPLOAD\" # $path1 = "C:\inetpub\wwwroot\UPLOAD\" # -,
Send_URL関数で、パラメーターを変更します。
$day = -30 # "+(-1*$day)+" . . $mail_domain = "@mydomain.com" $owner_domain = "mydomain\\" $mail_admin = "admin@mydomain.com" $mail_server = "mail.mydomain.com" $http = "http://web.mydomain.com/UPLOAD/"
ファイルの所有者の正しい決定は、ドメイン内でのみ可能です。 ドメインなしでは、共有フォルダーを持つコンピューター上にユーザーアカウントを作成し、それらをユーザーに関連付ける必要があります。
コード:
# Func.ps1 ################################### # hars@bk.ru 2015. Function Send_URL($File,$path,$path1){ ### ################################################## $day = -30 # "+(-1*$day)+" . . $mail_domain = "@mydomain.com" $owner_domain = "mydomain\\" $mail_admin = "admin@mydomain.com" $mail_server = "mail.mydomain.com" $http = "http://web.mydomain.com/UPLOAD/" ################################################## $File_nm = $File.Trim() # $File = $File -replace '\[|\]','`$0' # [] $owner = ( Get-Acl ($path + $File) ).Owner # ############## email if( $owner -like "*dministrator*" -or !$owner ){ $mail = $mail_admin }else{ $mail = ($owner -ireplace($owner_domain,"")) + $mail_domain } ###################################### #$md5 = Get-FileHash ($path + $File) -Algorithm MD5 #$File_url = $md5.Hash + ".zip" #$File_new = $path1 + $md5.Hash + ".zip" ############# if($File_nm.length > 100){ $File_url = $File_nm.Substring(0,100) }else{ $File_url = $File_nm } ############################################# ############# @!{}%$#''&` $File_url = $File_url -ireplace("#","") $File_url = $File_url -ireplace("%","") $File_url = $File_url -ireplace("$","") $File_url = $File_url -ireplace("&","_") $File_url = $File_url -ireplace("{","(") $File_url = $File_url -ireplace("}",")") $File_url = $File_url -ireplace('`',"") $File_url = $File_url -ireplace('"',"") $File_url = $File_url -ireplace("'","") $File_url = $File_url -ireplace("~","") $File_url = $File_url -ireplace(" ","_") ######################################### # $File_url = $mail + "_" + $File_url # $File_new = $path1 + $File_url # - Move-Item -path ( $path + $File ) -destination $File_new -force -EA SilentlyContinue #-whatif # if( Get-ChildItem $File_new -EA SilentlyContinue){ write-host "OK" # # , # , # , $F = Get-ChildItem $File_new $F.LastWriteTime = Get-date }else{ write-host "NO" # break # } # $colItems = (Get-ChildItem $File_new | Measure-Object -property length -sum) $colSize = "{0:N2}" -f ($colItems.sum / 1MB) + " Mb" ###################################### # $Body = "<b>: </b><a href='" + $http + $File_url +"'>" + $File_nm + " (" + $colSize + ")</a><br/> "+(-1*$day)+" . ." # Send-MailMessage -to $mail -from $mail_admin -subject " " -BodyAsHtml $Body -Encoding UTF8 -SmtpServer $mail_server # , if($mail -inotlike $mail_admin){ Send-MailMessage -to $mail_admin -from $mail -subject " " -BodyAsHtml $Body -Encoding UTF8 -SmtpServer $mail_server } Write-Host $owner $mail $Body # ###################################### # - get-childitem $path1 *.zip | where {$_.lastwritetime -le (get-date).adddays($day) } | del -Recurse -Force } ########################################################################################## ########################################################################################## ########################################################################################## ########################################################################################## ########################################################################################## # Send_URL.ps1 ################################### # hars@bk.ru 2015. # # powershell.exe "C:\Scripts\Send_URL.ps1" -NoLogo -NonInteractive -WindowStyle Hidden <# ."C:\scripts\Func.ps1" # . (), . #> ### ################################################## $path = "C:\UPLOAD\" # $path1 = "C:\inetpub\wwwroot\UPLOAD\" # -, ################################################## # ZIP Get-ChildItem $path -Recurse -Exclude *.zip | %{ Remove-Item $_ -Force -Recurse } # $flist = get-childitem $path"*.zip" #-recurse # ZIP , $flist | ForEach-Object{ Send_URL $_.PSChildName $path $path1 } # html # index.html , "" #<html><head><meta http-equiv="refresh" content="0;url=http://www.yandex.ru" /></head></html> #copy C:\inetpub\wwwroot\index.html C:\inetpub\wwwroot\UPLOAD\index.html -Force # -, New-Item $path" ZIP , .txt" -type file -Force ################################### ################################### ################################### # DeleteOldFiles.ps1 ################################### # . . # hars@bk.ru 2015. # # powershell.exe "C:\Scripts\DeleteOldFiles.ps1" -NoLogo -NonInteractive -WindowStyle Hidden ### ################################################## $ClearFolder = "C:\inetpub\wwwroot\UPLOAD\" # $day = -14 # ################################################## # oldDel # 14 , function oldDel($folder,$day){ get-childitem $folder * | where {$_.lastwritetime -le (get-date).adddays($day) } | del -Recurse -Force #-WhatIf } oldDel $ClearFolder $day