Powershell 检查文件夹并发送电子邮件
Powershell Check folders and send e-mail
我有一个程序检查文件的两个路径,如果有任何文件,它会为每个人发送一封邮件。
他们现在需要检查四个路径,并且多个人需要一封邮件作为路径。
例如
- 路径 1 - 邮寄给 x、y 和 z
- 路径 2 - 邮寄给 a 和 b
- 路径 3 - 邮寄给 x 和 a
- 路径 4 - 邮寄给 s、r 和 w
我怎样才能使它变得简单和最有效?
$folders = @()
$folders += "\server\files\Info\test"
$folders += "\server\files\Info\test2"
$receiver = @()
$receiver += "person1@test.com"
$receiver += "person2@test.com"
$i = 0
$folders | ForEach-Object {
$checkforfiles = $_
$directoryInfo = Get-ChildItem $checkforfiles | Measure-Object
$directoryInfo.count #Returns the count of all of the objects in the directory
$Numberone = $directoryInfo.Count
if ($directoryInfo.count -ge 1){
send-mailmessage -subject "Subject" -Body "There are $checkforfiles files " -from foldercheck@test.com -to $receiver[$i] `
-smtpserver smtp.ser.com
$i = $i+1
}
else{
write-host "nothing to process"
}
}
您可以简单地扩展文件夹数组来测试并使用开关来确定哪些用户应该收到电子邮件。
此外,我建议在带有大量参数的 cmdlet 上使用 splatting 以使代码更清晰。
# an array of folder paths to test
$folders = "\server\files\Info\test", "\server\files\Info\test2", "\server\files\Info\test3", "\server\files\Info\test4"
for ($i = 0; $i -lt $folders.Count; $i++) {
# get the file (and folder) count inside
$count = @(Get-ChildItem -Path $folders[$i]).Count
if ($count -gt 0) {
# determine who to email
$to = switch ($i) {
0 { 'personX@test.com', 'personY@test.com', 'personZ@test.com' ; break }
1 { 'personA@test.com', 'personB@test.com' ; break }
2 { 'personX@test.com', 'personA@test.com' ; break }
3 { 'personS@test.com', 'personR@test.com', 'personW@test.com' }
}
# set up a hashtable with parameters for splattting to Send-MailMessage
$mailParams = @{
To = $to
From = 'foldercheck@test.com'
Subject = "Subject"
Body = "There are $count files in folder '$($folders[$i])'"
SmtpServer = 'smtp.ser.com'
# add more parameters here if needed
}
# send the email
Send-MailMessage @mailParams
}
else {
Write-Host "Empty folder '$folders[$i]'; Nothing to process"
}
}
我有一个程序检查文件的两个路径,如果有任何文件,它会为每个人发送一封邮件。 他们现在需要检查四个路径,并且多个人需要一封邮件作为路径。 例如
- 路径 1 - 邮寄给 x、y 和 z
- 路径 2 - 邮寄给 a 和 b
- 路径 3 - 邮寄给 x 和 a
- 路径 4 - 邮寄给 s、r 和 w
我怎样才能使它变得简单和最有效?
$folders = @()
$folders += "\server\files\Info\test"
$folders += "\server\files\Info\test2"
$receiver = @()
$receiver += "person1@test.com"
$receiver += "person2@test.com"
$i = 0
$folders | ForEach-Object {
$checkforfiles = $_
$directoryInfo = Get-ChildItem $checkforfiles | Measure-Object
$directoryInfo.count #Returns the count of all of the objects in the directory
$Numberone = $directoryInfo.Count
if ($directoryInfo.count -ge 1){
send-mailmessage -subject "Subject" -Body "There are $checkforfiles files " -from foldercheck@test.com -to $receiver[$i] `
-smtpserver smtp.ser.com
$i = $i+1
}
else{
write-host "nothing to process"
}
}
您可以简单地扩展文件夹数组来测试并使用开关来确定哪些用户应该收到电子邮件。
此外,我建议在带有大量参数的 cmdlet 上使用 splatting 以使代码更清晰。
# an array of folder paths to test
$folders = "\server\files\Info\test", "\server\files\Info\test2", "\server\files\Info\test3", "\server\files\Info\test4"
for ($i = 0; $i -lt $folders.Count; $i++) {
# get the file (and folder) count inside
$count = @(Get-ChildItem -Path $folders[$i]).Count
if ($count -gt 0) {
# determine who to email
$to = switch ($i) {
0 { 'personX@test.com', 'personY@test.com', 'personZ@test.com' ; break }
1 { 'personA@test.com', 'personB@test.com' ; break }
2 { 'personX@test.com', 'personA@test.com' ; break }
3 { 'personS@test.com', 'personR@test.com', 'personW@test.com' }
}
# set up a hashtable with parameters for splattting to Send-MailMessage
$mailParams = @{
To = $to
From = 'foldercheck@test.com'
Subject = "Subject"
Body = "There are $count files in folder '$($folders[$i])'"
SmtpServer = 'smtp.ser.com'
# add more parameters here if needed
}
# send the email
Send-MailMessage @mailParams
}
else {
Write-Host "Empty folder '$folders[$i]'; Nothing to process"
}
}