电子邮件以检查尺寸/是否完整

时间:2019-06-12 12:19:56

标签: powershell

运行以下脚本:

class Liste: UIViewController {
    @IBOutlet weak var maListe: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

目标:检查文件.zip是否完整,一旦完成,它会发送一封电子邮件,让该文件可以通过。我正在运行脚本,没有错误,但也没有警报电子邮件。

建立在:加上可能发送电子邮件的时间。例如,脚本将在每天早上6:00运行,电子邮件将发送给用户以通知文件已完成。

2 个答案:

答案 0 :(得分:1)

在脚本顶部添加$ ErrorActionPreference =“ Stop”,因此将显示错误。

使用Attachments参数添加文件,building不是Send-MailMessage的有效参数

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

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

答案 1 :(得分:1)

  1. $ Folder变量必须使用“ $ FileToCheck = Get-Item ...”行,因为它使用了该变量。

  2. 在Send-MailMessage cmdlet中没有这样的参数“ building”。我认为您是在追求身体,因为您正在尝试获取内容...? https://docs.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。

相关问题