监视文件的网络文件夹

时间:2016-05-17 20:03:09

标签: powershell powershell-v2.0

我正在尝试在powershell中运行此查询,但当我使用当前$MonitorFolder作为\\\vavm\CINICO\Incoming运行时,我的查询在向文件夹添加文件时不起作用。如果我将$MonitorFolder的变量更改为C:\Users\RickG\Desktop\Test Reports,则在将文件添加到文件夹时,我的代码可以正常工作。那么如何监视文件添加到文件夹的\\vavm\CINICO\Incoming路径。

以下是检查文件夹的第一部分代码片段:

$MonitorFolder = "\\vavm\CINICO\Incoming"
$MonitorStopFile = "monitor.die"
$smtpServer = "mail.test.org"
$smtpFrom = "SYSTEMFUNCTION@test.org" 
$smtpTo = "test@test.org"


$SourceID = "MonitorFiles"

$smtpSubject = "New file arrived in $($MonitorFolder)" 
$Query = @"
SELECT * FROM __InstanceCreationEvent WITHIN 10
WHERE targetInstance ISA 'Cim_DirectoryContainsFile'
AND targetInstance.GroupComponent = 'Win32_Directory.Name="$($MonitorFolder.Replace("\", "\\\\"))"'
"@

解决方案:

$MonitorFolder = "\\vavm\CINICO\incoming"
$MonitorStopFile = "monitor.die"

$smtpServer = "mail.test.org"
$smtpFrom = "SYSTEMFUNCTION@test.org" 
$smtpTo = "test@test.org"
$smtpSubject = "New file arrived in $($MonitorFolder)" 
$SourceID = "MonitorFiles"
$IncomingFiles = Get-ChildItem $MonitorFolder

$smtp = New-Object -TypeName "Net.Mail.SmtpClient" -ArgumentList $smtpServer

$watcher = New-Object System.IO.FileSystemWatcher $MonitorFolder
#Files only. Default is files + directory
$watcher.NotifyFilter = [System.IO.NotifyFilters]'FileName,LastWrite'

$newFileSubscription = Register-ObjectEvent -InputObject $watcher -EventName Created -SourceIdentifier NewFileCreated -Action { 
Write-Host "New file named '$($Event.SourceEventArgs.Name)' arrived in $($MonitorFolder)"

$smtpBody += "`n[$(Get-Date -Format HH:mm:ss)]`tNew file named '$($Event.SourceEventArgs.Name)' arrived in $($MonitorFolder)"

if($Event.SourceEventArgs.Name -eq $MonitorStopFile) {
    Write-Host "Monitoring stopped"
    #Stop monitoring
    Unregister-Event -SubscriptionId $newFileSubscription.Id
    #Dispose FileSystemWatcher
    $watcher.Dispose()
}
$smtp.Send($smtpFrom, $smtpTo, $smtpSubject, $smtpBody)      
}

1 个答案:

答案 0 :(得分:2)

我99%确定__InstanceCreationEvent不支持远程共享。即使您没有订阅任何内容,WMI事件也会被您的计算机触发。您的计算机无法监控网络上的文件共享。

您应该尝试System.IO.FileSystemWatcher来监控远程共享。实施例

$MonitorFolder = "\\vavm\CINICO\Incoming"
$MonitorStopFile = "monitor.die"

$smtpServer = "mail.test.org"
$smtpFrom = "SYSTEMFUNCTION@test.org" 
$smtpTo = "test@test.org"
$smtpSubject = "New file arrived in $($MonitorFolder)" 
$SourceID = "MonitorFiles"

$smtp = New-Object -TypeName "Net.Mail.SmtpClient" -ArgumentList $smtpServer

$watcher = New-Object System.IO.FileSystemWatcher $MonitorFolder
#Files only. Default is files + directory
$watcher.NotifyFilter = [System.IO.NotifyFilters]'FileName,LastWrite'

#Using a thread-safe collection (in global scope so Action-block can reach it) to store the log just to be safe.
$global:newFiles = [System.Collections.ArrayList]::Synchronized((New-Object System.Collections.ArrayList))

$newFileSubscription = Register-ObjectEvent -InputObject $watcher -EventName Created -SourceIdentifier NewFileCreated -Action {   
    Write-Host "New file named '$($Event.SourceEventArgs.Name)' arrived in $($MonitorFolder)"
    $global:newFiles.Add("[$(Get-Date -Format HH:mm:ss)]`tNew file named '$($Event.SourceEventArgs.Name)' arrived in $($MonitorFolder)")

    if($Event.SourceEventArgs.Name -eq $MonitorStopFile) {
        Write-Host "Monitoring stopped"
        #Stop monitoring
        Unregister-Event -SubscriptionId $newFileSubscription.Id
        #Dispose FileSystemWatcher
        $watcher.Dispose()
    }
}

while ($watcher.EnableRaisingEvents -or $global:newFiles.Count -gt 0) {   

    #Sleep
    Start-Sleep -Seconds 60

    if($global:newFiles.Count -gt 0) {
        #Convert list of strings to single string (multiline)
        $smtpbody = $global:newFiles | Out-String

        $smtp.Send($smtpFrom, $smtpTo, $smtpSubject, $smtpBody) 

        #Mail sent, Empty array
        $global:newFiles.Clear()
    }

}

使用线程安全的全局arraylist更新上面的示例,以存储" newfiles" -log in和while循环,以便在日志中有新项目的情况下创建邮件。