-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsend-files.ps1
106 lines (87 loc) · 2.88 KB
/
send-files.ps1
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<#
.SYNOPSIS
Small Power Shell script to send files from a directory via mail.
.DESCRIPTION
Small Power Shell script to send files from a directory via mail and move them into an archive. Each mail contain only a single file. There is a filter for the file type.
This script is designed to be executed as scheduled task.
.EXAMPLE
send-files.ps1 -sourcepath "C:\User\Fabian\Scaned-PDF" -archivepath "$sourcepath\Archive" -filetype "*.pdf" -SmtpServer mail.infrastrukturhelden.de -From [email protected] -To [email protected]
.PARAMETER sourcepath
Path where to look for files. Subfolders will not be used!
.PARAMETER archivepath
Path where file will moved after sending
.PARAMETER filetype
Filter for file types
.PARAMETER SmtpServer
SmtpServer witch is uses to send the mail
.PARAMETER From
Mail From
.PARAMETER To
Mail To
.PARAMETER Subject
Subject of the Mail
.PARAMETER SmtpAuth
Switch if SMTP needs authentication
.PARAMETER smtppw
Password for SMTP User. Only need with SmtpAuth.
.PARAMETER smtpuser
SMTP Username. Only need with SmtpAuth.
.PARAMETER SmtpPort
Portnumber for SMTP if non Standard
.PARAMETER Body
Mail body, no HTML.
.NOTES
Author : Fabian Niesen
Filename : send-files.ps1
Requires : PowerShell Version 3.0
Version : 1.0
History : 1.1 FN 27.08.2022 Add SMTP Port (for #4)
1.0 FN initial version
.LINK
https://www.infrastrukturhelden.de/?p=13527
#>
[cmdletbinding()]
Param(
[Parameter(Position=1)]
[string]$sourcepath,
[Parameter(Position=2)]
[string]$archivepath,
[Parameter(Position=3)]
[string]$filetype="*.pdf",
[Parameter(Position=10)]
[string]$SmtpServer,
[Parameter(Position=17)]
[string]$From,
[Parameter(Position=18)]
[string]$To,
[Parameter(Position=19)]
[string]$Subject = "PDF Report: ",
[Parameter(Position=22)]
[switch]$SmtpAuth,
[Parameter(Position=23)]
[string]$smtppw,
[Parameter(Position=24)]
[string]$smtpuser,
[Parameter(Position=25)]
[int]$SmtpPort,
[Parameter(Position=28)]
[string]$Body = "Please see attachment."
)
IF ($SmtpAuth) {
Write-Debug "Using SMTP Auth"
$password = ConvertTo-SecureString $smtppw -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($smtpuser, $password)
}
IF ($SmtpPort) { $SmtpClient.Port = $SmtpPort }
$sources = Get-ChildItem $sourcepath -Filter $filetype -Depth 0
ForEach ( $source in $sources)
{
$file = $source.FullName
$SubjectM = $Subject + $source.Name
Write-Debug $file
Write-Debug "Send-MailMessage"
IF ($SmtpAuth) { Send-MailMessage -To $To -From $From -Subject $SubjectM -SmtpServer $SmtpServer -Attachments $file -Credential $cred -UseSsl }
ELSE { Send-MailMessage -To $To -From $From -Subject $SubjectM -SmtpServer $SmtpServer -Attachments $file -UseSsl }
Write-Debug "Move File"
Move-Item -Path $file -Destination $archivepath
}