【问题标题】:Powershell IO.FileSystemWatcher - folder not updated for last 30 minsPowershell IO.FileSystemWatcher - 文件夹在过去 30 分钟内未更新
【发布时间】:2021-01-14 02:48:09
【问题描述】:

我正在我的 Powershell 脚本中寻求帮助。

如果目录日志位置 C:\Users\10146187\Downloads\Syslogd 在过去 30 分钟内没有创建或更改任何新文件,我想运行函数“RunMyStuff”。我也没有取消注册功能。不确定,我把它放在哪里。谢谢。

Function RunMyStuff {
        # this is the bit I want to happen when there is no file created for 30 mins under C:\Users\10146187\Downloads\Syslogd
     Start-Process 'C:\windows\system32\calc.exe'
     Write-Host $global:FileCreated
    }
    
    Function Watch {    
        $global:FileCreated = $false 
        $folder = "C:\Users\10146187\Downloads\Syslogd"
        $filter = "*.*"
        $watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ 
            IncludeSubdirectories = $false 
            EnableRaisingEvents = $true
        }
    
        Register-ObjectEvent $Watcher "Created" -Action {$global:FileCreated = $true} > $null
         
        Start-Sleep -Seconds 120
        
        while($true){
            while ($global:FileCreated -eq $true){
                 Start-Sleep -Seconds 600
                 Write-Host $global:FileCreated
            }
           
            RunMyStuff
            
            $global:FileCreated = $false
        }
    }
    
    Watch

【问题讨论】:

  • 那个东西是当一个动作发生时。您为什么要尝试将其用于 _when 并且动作不会在给定的时间范围内发生? ///// 更明智的技术似乎是使用任务调度程序每 10 或 15 分钟运行一次以检查“在过去 xx 分钟内有什么变化吗?”。
  • 如果您要创建两个事件处理程序 - 一个用于 FileSystemWatcher,另一个用于 System.Timers.Timer 对象 (System.Timers.ElapsedEventHandler)。启动计时器,启动观察者。如果计时器在观察者事件之前结束,那么您将知道没有任何变化。如果观察者在计时器结束之前触发,则重置计时器事件。

标签: powershell powershell-4.0 system.io.file


【解决方案1】:

这可能是您正在寻找的示例。它使用 Timer 事件和 FileWatcher 事件。

function RunMyStuff()
{
    Write-Host "$(get-date) RunMyStuff (no files added within last 10 seconds)"
    # Start-Process "calc.exe"
}

function Watch ($folder) {
    Write-Host "Watching $((Get-Item $folder).Fullname)"
    $global:FileCreated = $true

    $filter = "*.*"
    $watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ 
        IncludeSubdirectories = $false
        EnableRaisingEvents = $true
    }
    Register-ObjectEvent -InputObject $watcher -EventName "Created" -SourceIdentifier "myFileWatcher" -Action { 
        $global:timer.Stop()
        $global:FileCreated = $true
        $global:timer.Start()
    } > $null

    $global:timer = New-Object Timers.Timer
    $global:timer.Interval = 10000
    Register-ObjectEvent -InputObject $global:timer -EventName "Elapsed" -SourceIdentifier "myTimer" -Action {
        $global:FileCreated = $false
    } > $null

    $global:timer.Start()

    try {
        while ($true) {
            Start-Sleep -Seconds 1
            if (!$global:FileCreated) {
                RunMyStuff
                $global:FileCreated = $true
            }
        }
    }
    finally {
        Unregister-Event -SourceIdentifier "myFileWatcher"
        Unregister-Event -SourceIdentifier "myTimer"
    }
}

Watch "C:\"

它正在运行:

【讨论】:

  • 谢谢,但它每秒都会为我触发。你对上面的代码做了什么修改吗?
  • @Powershel 唯一的区别是RunMyStuff 中的消息。我将脚本更新为相同。适用于 PowerShell Core 7.1 和 PowerShell 5.1
  • 谢谢。你知道如何让它在服务器重启期间持续存在吗?
  • 如果我理解正确,可以使用任务计划程序通过创建在系统启动时启动的任务来启动脚本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-24
相关资源
最近更新 更多