【发布时间】:2017-03-29 16:21:12
【问题描述】:
我是 powershell 和一般开发的新手。我正在尝试编写一个脚本,一旦文件超过一定大小,它将向联系人发送电子邮件。我有两个单独的功能,它们都单独工作(一个用于检查文件大小,一个用于生成文件以供 sendmail 使用),但我无法让它们交互。
我想执行函数 CheckSize 并且如果变量 $ExceedsSize 设置为 1 则调用函数 SendMail 否则脚本应该在没有其他操作的情况下完成。
我搜索了论坛,但找不到任何适用于我正在做的事情的内容。
##Check file to see if it is over a particular size and then send email once threshold is reached.
param(
[string]$SiteName = "TestSite", #Name of Site (No Spaces)
[string]$Path = "\\NetworkPath\Directory", #Path of directory to check
[int]$FileSizeThreshold = 10, #Size in MB that will trigger a notification email
[string]$Contacts = "MyEmail@email.com"
)
CLS
##Creates variable $ExceedsSize based on newest file in folder.
Function CheckSize {
IF ((GCI $Path -Filter *.txt | Sort LastWriteTime -Descending | Select-Object -first 1 | Measure-Object -property Length -sum).sum / 1000000 -gt $FileSizeThreshold) {$ExceedsSize = 1}
ELSE {$ExceedsSize = 0}
Write-Host $ExceedsSize
}
Function SendMail {
Param([string]$Template, [string]$Contacts, [string]$WarnTime)
$EmailLocation = "\\NetworkPath\Scripts\File_$SiteName.txt"
#Will Generate email from params
New-Item $EmailLocation -type file -force -value "From: JMSIssue@emails.com`r
To: $Contacts`r
Subject: $SiteName file has exceeded the maximum file size threshold of $FileSizeThreshold MB`r`n"
#Send Email
#CMD /C "$SendMail\sendmail.exe -t < $EmailLocation"
}
【问题讨论】:
标签: powershell