Friday, November 21, 2014

Monitoring Secure FTP file and Download it via Power-shell

Hi Readers,

Recently we have written as script to monitor if the particular file has been uploaded to secure FTP or not.

If its not uploaded that script will send us alert that Today's file has not been uploaded, if its uploaded than it will download & move it to FileV3Files folder with date stamp.

We have achieved this by using Powershell along with Curl utility

http://curl.haxx.se/

Extract the Zip file from below Link

https://gallery.technet.microsoft.com/scriptcenter/Secure-FTP-file-Uploads-f33bbda7



 

Edit FileV3Mon.ps1 Shell script

Change the Variables:(I reference here FileV3.csv, you might want to monitor different file, change the script accordingly)

----------------------------------------------------------------------------------------------

$smtpServer = "smtp.labtest.com"

$fromadd = "DoNotReply@labtest.com"

$email1 = "Sukhija@labtest.com"

 

.\cURL\curl.exe -u User:Password -R -O ftps://XXX.XXX.XXX.XXX/FileV3.csv -k

Note: Change the syntax for SFTP - https://curl.haxx.se/docs/manual.html

----------------------------------------------------------------------------------------------

After variables are changed, you can schedule the script to run daily at time you expect file to be present on SFTP.


###################################################################### 
###################################################################### 
#               Author: Vikas Sukhija 
#               Date:- 11/05/2014 
#               Description:- This will download the FileV3 csv 
#               file & monitor it daily 
###################################################################### 
######Download file from sftp########################################## 
 
$date = get-date -format d 
$date = $date.ToString().Replace(“/”, “-”) 
$time = get-date -format t 
$time = $time.ToString().Replace(":""-"
$time = $time.ToString().Replace(" """
 
$logs = ".\Logs" + "\" + "Powershell" + $date + "_" + $time + "_.txt" 
 
$smtpServer = "smtp.labtest.com" 
$fromadd = "DoNotReply@labtest.com" 
$email1 = "Sukhija@labtest.com" 
 
 
$date1=get-date -format d 
 
Start-Transcript -Path $logs  
 
####Enter th Secure FTP details ################################################### 
 
.\cURL\curl.exe -u User:Password -R -O ftps://XXX.XXX.XXX.XXX/FileV3.csv -
 
$lwrt=Get-ChildItem .\FileV3.csv 
 
if($lwrt.LastWriteTime -like "$date1*"

Write-host "File for Today found on SFTP Site" -ForegroundColor green 
Move-Item .\FileV3.csv .\FileV3Files\FileV3-$date.csv 

else 

write-host "FileV3-check file no uploaded Today, Pls Check" -ForegroundColor yellow 
$msg = new-object Net.Mail.MailMessage 
$smtp = new-object Net.Mail.SmtpClient($smtpServer
#Mail sender 
$msg.From = $fromadd 
#mail recipient 
$msg.To.Add($email1
$msg.Subject = "FileV3-check file not uploaded Today, Pls Check" 
$msg.Body = "FileV3-check file not uploaded Today, Pls Check" 
$smtp.Send($msg
 

 
######################################################################## 
if ($error -ne $null
      { 
#SMTP Relay address 
$msg = new-object Net.Mail.MailMessage 
$smtp = new-object Net.Mail.SmtpClient($smtpServer
#Mail sender 
$msg.From = $fromadd 
#mail recipient 
$msg.To.Add($email1
$msg.Subject = "FileV3 Monitoring script error" 
$msg.Body = $error 
$smtp.Send($msg
       } 
 
Stop-Transcript 
#############################################################################




Regards

Sukhija Vikas

http://msexchange.me

1 comment: