📜 ⬆️ ⬇️

Forwarding large files. Automatically receive download links to email

Users often have to deal with sending large files both within the local network and outside it. For example, Pierrot needs to send Malvina documentation of 10 gigabytes. Here corporate mail will not come to the rescue, as users usually do not want to bother with copying links, etc. If they are in the same local network, then a shared folder will help, and if they are at different ends of the country ... As a rule, you have to use an FTP server, which is often not very convenient, or Internet services for sharing large files.

So the idea came to create a convenient and controlled service for sending large files on the local network.

Implementation on PowerShell, Windows platform.
')
Requirements:


How it works:



Description of the script:


Note: if the user is a member of the Administrators group on the server with a shared folder, then Owner = Administrators group, for this the administrative mailbox is specified in the script.

In the script, you need to change the following parameters to the ones you need:

$path = "C:\UPLOAD\" #    $path1 = "C:\inetpub\wwwroot\UPLOAD\" #  -,   

In the Send_URL function, change the parameters:

 $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/" 

The correct definition of the owner of the file is possible only in the domain. Without a domain, you will have to create user accounts on a computer with a shared folder and associate them with your users.

Code:

 # 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 

Source: https://habr.com/ru/post/276575/


All Articles