Create weblink on upload with Powershell

You can share your Lua Scripts with everybody here.

Create weblink on upload with Powershell

Postby dthompson » Thu Oct 13, 2011 2:40 am

OK, so this isn't LUA, but it is scripting and sometime LUA just don't cut it...

Howdy all,

I recently had a request from one of our WingFTP users to do some fancy scripting triggered by an upload event, specifically, to create a weblink as soon as a file was uploaded and then email an external party with the link. LUA wasn't enough to do this, so here I will cover the two elements of getting this going:
1. How to make a weblink through the CLI
2. How to launch Powershell on upload of a file

Firstly, in order for this to work off the bat, you should go and create an initial web link of any file. This will make the necessary folder in the install directory of WingFTP. It will create a directory along the lines of:
<InstallDir>\Data\_WEBLINK\ (eg. C:\Program Files (x86)\Wing FTP Server\Data\_WEBLINK\)

You'll notice that WingFTP create an extension-less file here. The name of the file is the 32 character string that is part of the WebLink URL. If you open the file in notepad, you can see that it has 3 lines:
Full path to file
Downlod limit
Expiration date
If you do not set limits or expirations, the values for these are -1, which is what I am working on from here on out. Someone else can extend on how to set these values late if need be.

so the process of creating your own weblink is very simple, just make a file here with a 32 character long name (would probably work with any length, but I kept the 32 char length), enter the actual file location and then -1, -1. Save as ANSI file (note Unicode as I found out) and you are ready to send out a weblink.

The weblink format is then along the lines of:<weblinkfilename>&

OK, so that was simple enough. I know had what I needed to manually create a weblink. All that is left is to set WingFTP to launch Powershell on upload and run my script. Well, this proved harder than I thought. I think this was mainly due to how much we locked down the OS we were running WingFTP on.

I built up a dev system and I still couldn't get Powershell to launch correctly. In the end, the only way I could find around it was to tell WingFTP to launch PSExec.exe and us it to run Powershell under the system account. Here is what I ended up with for the settings of OnFileUpload -> Enable Execute Program
Program Path: C:\Scripts\Psexec.exe
Parameters: -d -h -s C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -NoProfile -NoLogo -File F:\Scripts\OnUpload.ps1 %PathName %FileName %YYYY%MM%DD %Name

This tells PSExec to launch in non-interactive mode, with elvated rights and as the system account. The options for Powershell are pretty self explanitory. Lastly, I pass the WingFTP variables for the file uploaded, short file name, a date stamp or sorts and the username.

Inside the Powershell script, I came up with a way to generate a random 32 characted string. It's not very elegant, but it works. It uses a .NET function to generate a random 8.3 filename. It joins 4 of these together, removes the "."s and truncates down to the last 32 characters. With that in place, it makes the weblink file, send out an email to the required users and that is that. This whole process is filtered based on a username check, so this only occurs when a service account we have does an upload.

Lastly, regardless of the logged on user, I force SophosAV to do a scan on the uploaded file. this is ontop of on-access scanning. Sophos outputs a log of all the scans as well.

The Powershell itself could use a tidy up, but it's in and working for now. If I find the time I will clean it up. Or perhaps someone on the forums will do it for me :-D

Powershell Code - Save as OnUpload.ps1 and edit the details, like email server, file locations, etc.
param ( [string] $FullFileName, [string] $FileName, [string] $DateStamp, [string] $UserName )

if ($UserName -eq "uploadservice"){
$rfn1 = []::getrandomfilename()
$rfn2 = []::getrandomfilename()
$rfn3 = []::getrandomfilename()
$rfn4 = []::getrandomfilename()
$rfnfull = $rfn1 + $rfn2 + $rfn3 + $rfn4
$rfnfull = $rfnfull.Replace(".","")
$rfnfull = $rfnfull.Substring(12)
$TextOutput = @"

$TextOutput | Out-File -Encoding Ascii "C:\Program Files (x86)\Wing FTP Server\Data\_WEBLINK\$rfnfull"

$emailFrom = “”
$emailTo = “”
$subject = “New Data Available”
$body = “The upload service has delivered a file available at$rfnfull&realfilename=$FileName
$smtpServer = “”
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)

$FullFileName = $FullFileName.Replace("://",":\")
$FullFileName = $FullFileName.Replace("/","\")
& "C:\Program Files (x86)\Sophos\Sophos Anti-Virus\sav32cli.exe" -nc $FullFileName -p=F:\AVLogs\$DateStamp-$FileName.log
Tue Sep 20, 2011 4:53 am

Return to Lua scripts

Who is online

Users browsing this forum: No registered users and 1 guest