通过电子邮件检查尺寸/如果已完成



运行以下脚本:

$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 将电子邮件发送给用户以通知文件已完成。

  1. $Folder变量需要在"$FileToCheck = Get-Item..."行,因为它使用该变量。

  2. 发送邮件 cmdlet 中没有此类参数"生成"。我认为你在追求身体,因为你试图获得内容...?https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-6

  3. 另一个注意事项是,Get-Content 将无法读取 zip 文件的内容。您需要解压缩文件,然后读取文件或将文件添加为附件。这是在仅包含文本文件的 zip 文件上使用 Get-Content 的示例:PK ï ̧ ÌN_S³ test.txtsdafasdfPK ï ̧ ÌN_S³ test.txtPK 6 .

在脚本顶部添加 $ErrorActionPreference = "Stop",以便显示错误。

使用附件参数添加文件,building不是发送邮件的有效参数

不需要获取内容,只需将路径添加到附件中:

$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
}

相关内容

最新更新