gpt4 book ai didi

由于 Filesystemwatcher,Powershell 挂起

转载 作者:行者123 更新时间:2023-12-02 22:35:58 25 4
gpt4 key购买 nike

我们有一些复杂的解决方案来解决一些打印问题(由 citrix 和远程服务器引起)。基本上从主服务器,我们强制将 pdf 文件推送到远程 pc,然后有一个 powershell 脚本不断在远程 pc 上运行以“捕获”文件并将其推送到本地打印机

这工作“很好”

然而,我们得到随机辍学。 powershell 脚本似乎没有崩溃,因为它仍在 windows 中运行,但实际操作似乎不是在处理新文件

我今天读了很多书,其中提到在完成后必须命名和取消注册事件,否则它会导致缓冲区溢出问题并使 powershell 停止处理操作。但我不确定它应该在代码中的实际位置。这个想法是这个脚本将永久运行,所以我们是在操作本身还是其他地方取消注册或删除事件?

我之前在操作中进行了很多虚拟日志记录以试图找到它失败的地方,但它似乎在没有任何正当理由的情况下停止在不同的点(即,它会在命令查找文件时失败,其他命令移动等的时间等)

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "l:\files\cut"
$watcher.Filter = "*.pdf"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { $path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$scandir="l:\files\cut"
$scanbackdir="l:\files\cut\back"
$scanlogdir="l:\files\cut\log"
$sumatra="l:\SumatraPDF.exe"
$pdftoprint=""
$printername= "MainLBL"

### Get the List of files in the Directory, print file, wait and then move file
Get-ChildItem -Path $scandir -filter "*.pdf" -Name | % {
$pdftoprint=$_
& $sumatra -silent $scandir\$pdftoprint -print-to $printername

sleep 3

Move-Item -force $scandir\$pdftoprint $scanbackdir
}
}

### Define what happens when script fails
$erroraction = {echo $(get-date) the process crashed | Out-File -Append l:\files\cut\log\errorlog.txt}

### DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Error" -Action $erroraction
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 5}

最佳答案

如果您希望脚本在后台运行,请查看 PowerShell 后台作业。

如果你想让一个脚本永久运行,那么你想让它成为一个服务......

查看这些:

How to Create a User-Defined Service

How to run a PowerShell script as a Windows service

...或将其附加到计划任务,这将在重新启动时重新启动它。

有两种方法可以实现 FileSystemWatcher。

  • 同步
  • 异步

同步 FileSystemWatcher,就其本质而言,当检测到更改时,控制权将返回给您的脚本,以便它可以处理更改。如果在您的脚本不再等待事件时发生另一个文件更改,它就会丢失。因此导致意想不到的结果。

异步使用 FileSystemWatcher,它将继续记录新的文件系统更改,并在 PowerShell 处理完之前的更改后处理它们。

* 示例 - 示例异步 FileSystemWatcher*

### New-FileSystemWatcherAsynchronous

# Set the folder target
$PathToMonitor = Read-Host -Prompt 'Enter a folder path'

$FileSystemWatcher = New-Object System.IO.FileSystemWatcher
$FileSystemWatcher.Path = $PathToMonitor
$FileSystemWatcher.IncludeSubdirectories = $true

# Set emits events
$FileSystemWatcher.EnableRaisingEvents = $true

# Define change actions
$Action = {
$details = $event.SourceEventArgs
$Name = $details.Name
$FullPath = $details.FullPath
$OldFullPath = $details.OldFullPath
$OldName = $details.OldName
$ChangeType = $details.ChangeType
$Timestamp = $event.TimeGenerated
$text = "{0} was {1} at {2}" -f $FullPath, $ChangeType, $Timestamp

Write-Host $text -ForegroundColor Green

# Define change types
switch ($ChangeType)
{
'Changed' { "CHANGE" }
'Created' { "CREATED"}
'Deleted' { "DELETED"
# Set time intensive handler
Write-Host "Deletion Started" -ForegroundColor Gray
Start-Sleep -Seconds 3
Write-Warning -Message 'Deletion complete'
}
'Renamed' {
$text = "File {0} was renamed to {1}" -f $OldName, $Name
Write-Host $text -ForegroundColor Yellow
}
default { Write-Host $_ -ForegroundColor Red -BackgroundColor White }
}
}

# Set event handlers
$handlers = . {
Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Changed -Action $Action -SourceIdentifier FSChange
Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Created -Action $Action -SourceIdentifier FSCreate
Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Deleted -Action $Action -SourceIdentifier FSDelete
Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Renamed -Action $Action -SourceIdentifier FSRename
}

Write-Host "Watching for changes to $PathToMonitor" -ForegroundColor Cyan

try
{
do
{
Wait-Event -Timeout 1
Write-Host '.' -NoNewline

} while ($true)
}
finally
{
# End script actions + CTRL+C executes the remove event handlers
Unregister-Event -SourceIdentifier FSChange
Unregister-Event -SourceIdentifier FSCreate
Unregister-Event -SourceIdentifier FSDelete
Unregister-Event -SourceIdentifier FSRename

# Remaining cleanup
$handlers |
Remove-Job

$FileSystemWatcher.EnableRaisingEvents = $false
$FileSystemWatcher.Dispose()

Write-Warning -Message 'Event Handler completed and disabled.'
}

关于由于 Filesystemwatcher,Powershell 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56452971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com