如何从$ fsw Powershell File Watcher中排除临时文件

时间:2019-07-10 20:21:49

标签: powershell

我想监视添加到文件夹的新文件,并收到一封电子邮件通知我新文件。每当打开word文档时,它都会创建一个临时文件,并将此临时文件通知我。我想从通知中排除这些临时文件。

此外,我想知道是否有一种方法可以在每次发送电子邮件时不打开Outlook的新版本。

$folder = 'S:\FRD\ENDOWMENT\Legacy Commitments\Donor files' # Enter the root path you want to monitor.
$filter = '*.*'  # You can enter a wildcard filter here.

# In the following line, you can change 'IncludeSubdirectories to $true if required.
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubdirectories = $true;
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}

# Here, all three events are registerd.  You need only subscribe to events that you need:

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "The file '$name' was $changeType at $timeStamp" -Fore Green
    Start-Process Outlook
    $o = New-Object -Com Outlook.Application
    $mail = $o.CreateItem(0)
    #2 = high importance email header
    $mail.Importance = 2
    $mail.Subject = "CYJL File Created"
    $mail.Body = "The file '$name' was $changeType at $timeStamp"
    #for multiple email, use semi-colon ; to separate
    $mail.To = "ZMichelson@jfedcin.org"
    $mail.Send()

我希望每次将新文件添加到文件夹时都会收到电子邮件,并且在打开Word文档时不会收到添加临时文件的通知。另外,我不希望每次发送电子邮件时都打开新的Outlook迭代。

0 个答案:

没有答案