FileSystemWatcher不会触发事件上的动作阻止

时间:2017-05-17 13:04:21

标签: git powershell

我有一个文件,我需要在写入后立即推送到Git仓库并且我已经敲了下面的脚本来实现这个目标。事件被创建得很好,但在写入文件时没有执行代码块,我不确定原因。

剧本:

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Path\To\Repo"
$watcher.Filter = "file.txt"
$watcher.EnableRaisingEvents = $true
$watcher.IncludeSubdirectories = $false
$watcher.NotifyFilter = [IO.NotifyFilters]::LastWrite

Register-ObjectEvent $watcher Changed -SourceIdentifier FileChanged -Action {
    Set-Location $watcher.Path

    # random alphanumeric string to populate the commit name
    $rand = -join ((65..90) + (97..122) | Get-Random -Count 16 | % {[char]$_})

    Write-Host "File pwds.kdb has been updated. Committing to git..."

    git add .
    git commit -m "$rand"
    git push

    if ($LASTEXITCODE -eq 0) {
        Write-Host "Pushed to repo successfully"
    }
}

Register-ObjectEvent的输出:

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
18     FileChanged                     NotStarted    False                                ...

$(Get-EventSubscriber -SubscriptionId 18).Action.Command是动作块的内容。

有两点需要注意:

  • " HasMoreData"属性不会更新为" True"在我写入文件后,这可能表明它没有正确地接收更改?

  • "州"属性说" NotStarted"

1 个答案:

答案 0 :(得分:0)

您可以在下面尝试

$watcher.NotifyFilter = [IO.NotifyFilters]'lastaccess,filename'

我不知道它是否有效,请尝试