powershell - 通过电子邮件检查尺寸/是否完整
问题描述
运行以下脚本:
$FileToCheck = Get-Item -Path $folder/test.zip -ErrorAction SilentlyContinue
$EmailSplat = @{
To = 'business@email.com'
CC = 'admin@email.com'
#SmtpServer = 'smtp.server.net'
From = 'my@email.com'
Priority = 'High'
}
$folder = "C:\test\"
# first condition: 'If the file does not exist, or was not created today, an e-mail should be sent that states "File not created" or similar.'
if ((-not $FileToCheck) -or ($FileToCheck.CreationTime -le (Get-Date).AddDays(-1))) {
$EmailSplat.Subject = 'File not Found or not created today'
$EmailSplat.building = 'This is the email building'
Send-MailMessage @EmailSplat
# second condition 'If the file exists and was created today, but has no content, no e-mail should be sent.'
} elseif (($FileToCheck) -and ($FileToCheck.Length -le 2)) {
#third condition and the default condition if it does not match the other conditions
} else {
$EmailSplat.Subject = 'Active Directory Accounts To Check'
$EmailSplat.building = Get-Content -Path/test.zip //maybe add the file??
Send-MailMessage @EmailSplat
}
目标:检查文件 .zip 是否完整,一旦完成,它会发送一封电子邮件,让业务该文件很好。我正在运行脚本,没有收到任何错误,但也没有收到警报电子邮件。
建立在:添加可能发送电子邮件的时间。例如,脚本将在每天早上 6:00 运行,电子邮件会发送给用户以通知文件已完成。
解决方案
在脚本顶部添加 $ErrorActionPreference = "Stop",以便显示错误。
使用 Attachments 参数添加文件,building
不是 Send-MailMessage 的有效参数
不需要 Get-Content,只需添加附件的路径即可:
$EmailSplat.Attachments = "Path/test.zip"
所以是这样的:
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$folder = "C:\test"
$fileToCheck = Get-Item -Path (Join-Path $folder test.zip) -ErrorAction SilentlyContinue
$emailOptions = @{
"To" = "business@email.com"
"CC" = "admin@email.com"
"SmtpServer" = "smtp.server.net"
"From" = "my@email.com"
"Priority" = "High"
}
# first condition: If the file does not exist, or was not created today, an e-mail should be sent that states "File not created" or similar.
if ((-not $fileToCheck) -or ($fileToCheck.CreationTime -le (Get-Date).AddDays(-1)))
{
$emailOptions.Subject = "File not Found or not created today"
Send-MailMessage @emailOptions
}
elseif ($fileToCheck -and ($fileToCheck.Length -le 2))
{
# second condition: If the file exists and was created today, but has no content, no e-mail should be sent.
}
else
{
# third condition and the default condition if it does not match the other conditions
$emailOptions.Subject = "Active Directory Accounts To Check"
$emailOptions.Attachments = $fileToCheck.FullName
Send-MailMessage @emailOptions
}
推荐阅读
- python-datetime - 将数据框的列转换为时间戳
- google-apps-script - Google GroupsApp 中的 .getMembers() 和 .getGroups() 列出了意外结果
- laravel - Laraven Cron 表达式
- javascript - 通过 addEventListener 添加的事件监听器被多次调用
- java - 无法使用 SLF4J + Log4J 记录调试记录器
- python - 比较同一数据框中的两列
- c# - GeckoWebbrowser 在 vb.net 中单击类名
- python - 如何在xml文件中使用python插入值
- python - 使用 MNIST 数据集 Pytorch 训练 SqueezeNet 模型
- javascript - 通过键聚合对象数组