Forwarding large files. Automatically receive the download links in the mail
Users often have to deal with sending big files inside a local network and beyond. For example, Pierrot, Malvina need to send documentation with a volume of 10 gigabytes. Here is a not coming corporate e-mail, because users usually don't want to bother with copying links etc. If they are in the same local network, it will help shared folder, and if they are at different ends of the country... usually have to use an FTP server that often is not very convenient, a web-services sharing big files.
So the idea came to create a comfortable and controlled services for sending large files in local network.
Implementation of PowerShell on the Windows platform.
Requirements:
the
How it works:
the
Description script:
the
Note: if a user is in the Administrators group on the server with the shared folder, that Owner = Admins, this script specifies the administrative mailbox.
In the script you need to change the following parameters required:
the
function Send_URL to change parameters:
the
Correct definition of the owner of a file is only possible in the domain. Without a domain you have to enter user accounts on the computer with the shared folder and associate them with your users.
Code:
the
Article based on information from habrahabr.ru
So the idea came to create a comfortable and controlled services for sending large files in local network.
Implementation of PowerShell on the Windows platform.
Requirements:
the
-
the
- the Web server published to the Internet. the
- Shared folder on my Windows PC (to determine the owner of the file). the
- setup on the server. run the script on a schedule, once per minute.
How it works:
the
-
the
- the User puts a ZIP file in a shared folder on the server.
- User receives an email with a link to download the file through your web server. the
- User sends an email with a download link to the recipient.
the Script searches for ZIP files in the shared folder and moves them to a folder on the web server, then sends the letter Owner file. the
Description script:
the
-
the
- Deletion in the target folder of everything that has the extension ZIP. the
- get a list of ZIP files in the target folder. the
- Bust ZIP files processing each file function Send_URL. the
- Function Send_URL: identifies the Owner of the file, processes the file name, moves the file under a new name to a folder on the web server, sends the owner and the Administrator (for control) e-mail a link to download the file, it deletes the files in the folder on the web server older than a certain number of days. More detailed comments, see the script. the
- create a file in the shared folder with the name-hint "only ZIP files, the rest удаляется.txt".
Note: if a user is in the Administrators group on the server with the shared folder, that Owner = Admins, this script specifies the administrative mailbox.
In the script you need to change the following parameters required:
the
$path = "C:\UPLOAD\" #Bassarena folder on the server
$path1 = "C:\inetpub\wwwroot\UPLOAD\" #Folder on the web server where download
function Send_URL to change parameters:
the
$day = -30 #You can download the file for the "+(-1*$day)+" days. Then the file is deleted from the server.
$mail_domain = "@mydomain.com"
$owner_domain = "mydomain\\"
$mail_admin = "admin@mydomain.com"
$mail_server = "mail.mydomain.com"
$http = "http://web.mydomain.com/UPLOAD/"
Correct definition of the owner of a file is only possible in the domain. Without a domain you have to enter user accounts on the computer with the shared folder and associate them with your users.
Code:
the
#File Func.ps1 ###################################
# Smirnov Alexander hars@bk.ru December 2015.
Function Send_URL($File,$path,$path1){
### The parameters that need to improve their
##################################################
$day = -30 #You can download the file for the "+(-1*$day)+" days. Then the file is deleted from the server.
$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() # remove the spaces at the beginning and at the end of the file name
$File = $File -replace '\[|\]',"$0' # replace the [] with the correct symbols
$owner = ( Get-Acl ($path + $File) ).The Owner of # the file Owner
############## The formation of the 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"
############# The size limit of a file name
if($File_nm.length > 100){
$File_url = $File_nm.Substring(0,100)
}else{
$File_url = $File_nm
}
#############################################
############# Character replacement @!{} %$#"&`
$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 names to publish to web
$File_url = $mail + "_" + $File_url
# The path of the moved file
$File_new = $path1 + $File_url
# Move the file to a web folder
Move-Item -path ( $path + $File ) -destination $File_new -force -EA SilentlyContinue #-whatif
# Check the file is moved in case if it is copied by the user to the folder
if( Get-ChildItem $File_new -EA SilentlyContinue){
write-host "OK" #the File exists
#setting new timestamp file creation,
#to the script responsible for clearing folders of old files,
#removed created a long time ago but sent now or from time to time archive
$F = Get-ChildItem $File_new
$F. LastWriteTime = Get-date
}else{
write-host "NO" #File are not from the existence
break # interrupt functions
}
# File size
$colItems = (Get-ChildItem $File_new | Measure-Object-property length-sum)
$colSize = "{0:N2}" -f ($colItems.sum / 1MB) + "Mb"
######################################
#Message to the user
$Body = "<b>Download: </b><a href='" + $http + $File_url +"'>" + $File_nm + " (" + $colSize + ")</a><br/>
You can download the file for the "+(-1*$day)+" days. Then the file is deleted from the server."
#Send letters
Send-MailMessage -to $mail-from $mail_admin -subject "file transfer" -BodyAsHtml $Body -Encoding UTF8-SmtpServer $mail_server
#Sending a letter to the administrator, to control
if($mail-inotlike $mail_admin){
Send-MailMessage -to $mail_admin -from $mail -subject "file transfer" -BodyAsHtml $Body -Encoding UTF8-SmtpServer $mail_server
}
Write-Host $owner $mail $Body # Check in the console that someone sent
######################################
#Check obsolete files in the web folder and deleting them as obsolete
get-childitem $path1 *.zip | where {$_.lastwritetime -le (get-date).adddays($day) } | del -Recurse -Force
}
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
##########################################################################################
#File Send_URL.ps1 ###################################
# Smirnov Alexander hars@bk.ru December 2015.
#In the schedule to add the execution of the script once a minute
# powershell.exe "C:\Scripts\Send_URL.ps1" -NoLogo -NonInteractive -WindowStyle Hidden
<#
."C:\scripts\Func.ps1" # Loadable file functions. It is better to load function(procedure) that is so beautiful.
#>
### The parameters that need to improve their
##################################################
$path = "C:\UPLOAD\" #Bassarena folder on the server
$path1 = "C:\inetpub\wwwroot\UPLOAD\" #Folder on the web server where download
##################################################
# Delete everything except ZIP files
Get-ChildItem $path -Recurse -Exclude *.zip | %{ Remove-Item $_ -Force -Recurse }
# Get names of files in a folder
$flist = get-childitem $path"*.zip" #-recurse
# Iterate ZIP files, moving, and sending the owner of the file letter with reference
$flist | ForEach-Object{ Send_URL $_.PSChildName $path $path1 }
# Re-copy the html redirect to the site
#In the file index.html line redirection, for "failing" view folder in the browser
#<html><head><meta http-equiv="refresh" content="0;url=http://www.yandex.EN" /></head></html>
#copy C:\inetpub\wwwroot\index.html C:\inetpub\wwwroot\UPLOAD\index.html -Force
# Create a file with a name hint for the gifted
New-Item $path"only ZIP files, the rest удаляется.txt" -type file -Force
###################################
###################################
###################################
#File DeleteOldFiles.ps1 ###################################
#Deleting old files. Delete files older than XX days.
# Smirnov Alexander hars@bk.ru December 2015.
#In the schedule to add the execution of the script once a day
# powershell.exe "C:\Scripts\DeleteOldFiles.ps1" -NoLogo -NonInteractive -WindowStyle Hidden
### The parameters that need to improve their
##################################################
$ClearFolder = "C:\inetpub\wwwroot\UPLOAD\" #the Folder which you clean
$day = -14 #the expiration Time of the file in days with a minus sign
##################################################
#Function oldDel iterates through all files in a folder and checks the time they were last modified
#if the modification time is less than or equal to current date minus 14 days, the file is deleted
function oldDel($folder,$day){
}
oldDel $ClearFolder $day
Комментарии
Отправить комментарий