【发布时间】:2019-12-09 10:44:05
【问题描述】:
我有一个文件,我需要在写入后立即将其推送到 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”,这可能表明它没有正确获取更改?
“状态”属性显示“未启动”
【问题讨论】:
标签: git powershell