Powershell通知电子邮件文件文件夹

时间:2018-08-14 15:58:34

标签: powershell email temp

每当在某个文件夹中创建文件时,我都试图接收电子邮件,虽然我可以正常使用,但是我一直在获取.tmp文件和〜文件。如果发生以上任何一种情况,我都不希望发送电子邮件。如果没有发生任何情况,请发送电子邮件。但是,我现在无法获得向我发送电子邮件的强大功能,而且我不确定自己做错了什么或者是否有更简单的方法来做到这一点。我已经在下面发布了我的代码,如果您知道为什么它不起作用,请帮助我。

$folder = "Folder Path"
$mailserver = "mail server"
$recipient = "MyEmail@Email.com"

$fsw = New-Object System.IO.FileSystemWatcher $folder -Property @{
   IncludeSubdirectories = $true
   EnableRaisingEvents=$true
}


$created = Register-ObjectEvent -InputObject $fsw -EventName Created -SourceIdentifier CreatedEvent -Action {
   $item = Get-Item $eventArgs.FullPath
   $s = New-Object System.Security.SecureString
   $anon = New-Object System.Management.Automation.PSCredential ("NT AUTHORITY\ANONYMOUS LOGON", $s)
   If ($item -notlike '~') {
    foreach($line in $lines) {
        $extn = [IO.Path]::GetExtension($line)
        if ($extn -ne ".tmp") {
          Send-MailMessage -To $recipient `
                -From "Alerts@Email.com" `
                -Subject “File Creation Event” `
                -Body "A file was created: $($eventArgs.FullPath)" `
                -SmtpServer $mailserver `
                -Credential $anon
}
}
}
}

0 个答案:

没有答案