-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileCopyToSftp.ps1
More file actions
54 lines (42 loc) · 2.05 KB
/
FileCopyToSftp.ps1
File metadata and controls
54 lines (42 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
## Defining variables used for this program
$source_dir = "<Source Dir where file will be genrated>"
$ArchivePath ="<Arhive folder Dir>"
$PSEmailServer = '<smtp mail server>'
## Load WinSCP .NET assembly
Add-Type -Path "<path>\WinSCPnet.dll"
## Set up SFTP session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "<sftp host name>"
UserName = "<user>"
Password = "<password>"
SshHostKeyFingerprint = "<host key to connect to sftp server>"
}
## Checking if file exist for todays date
$MailBody = "Time in UTC = "+$(Get-Date -format 'u') + "`n`nChecking if RMA File exists for today's date.";
$File = Get-ChildItem -Path $source_dir -Filter '<file name>*' | Where-Object {$_.LastWriteTime -gt (Get-Date).Date}
$FullFilePath = $source_dir + $File.Name
##If file found for todays date copy to sftp server, else inform file does not exist.
if(Test-Path -Path $FullFilePath -PathType Leaf) {
$MailBody += "`nFile found with name = " + $FullFilePath +"`n`nNow i am copying it to SFTP server"
################### Copy file to SFTP Folder ####################
$session = New-Object WinSCP.Session
try{
$session.Open($sessionOptions)
$session.PutFiles($FullFilePath, "/<path of sftp server>/").Check()
}
finally{
$session.Dispose()
}
$MailBody += "`n**File has been copied to SFTP server fine**"
#################### Move file to Archive ######################
$MailBody += "`n`nNow i am moving file to Archive folder."
Get-ChildItem –Path $FullFilePath | Move-Item -Destination $ArchivePath
$MailBody += "`n**RMA File has been moved to Archive folder successfully**"
#################### Send Mail notification #####################
Send-MailMessage -From '<sender mail id>' -To '<reciever mail id>' -Subject 'SFTP Script Run Summary' -Body $MailBody
}
else {
$MailBody +="`n`n**File does not exists for todays date, Program is exiting**"
Send-MailMessage -From '<sender mail id>' -To '<reciever mail id>' -Subject 'SFTP Script Run Summary' -Body $MailBody
}