我写了一个powershell脚本,当文件保存在某个文件夹中时获得电子邮件通知。第一次有人保存时,它的效果很好,但是之后有人保存时,您会收到2封电子邮件。您能否查看下面发布的代码,看看我到底做错了什么或为什么会发生这种情况?
$folder = "Folder Path"
$mailserver = "mail server"
$recipient = "Alerts@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 '~') -And ([IO.Path]::GetExtension($item) -ne '.tmp')) {
Send-MailMessage -To $recipient `
-From "Email@Email.com" `
-Subject “File Creation Event” `
-Body "A file was created: $($eventArgs.FullPath)" `
-SmtpServer $mailserver `
-Credential $anon
}
}