简体   繁体   English

脚本监听文件夹事件Powershell

[英]Script listening to folder event Powershell

I am having problem with a Powershell script. 我在Powershell脚本中遇到问题。 What I am doing in the following lines of code is hopefully clear by STEPS (1)-(4): 希望通过以下步骤(1)-(4)清楚我在以下代码行中所做的工作:

# STEP (1) Create listener on folder where outputfile.pdf file will be created 
#         (folder here is `$folder`).

# STEP (2) Wait for new .pdf file to be created.

# STEP (3) When file is created, send mail and print.

# STEP (4) Unsubscribe listener.


$folder = 'X:\foldername'
$filter = '*.*'                             # <-- set this according to your requirements

# STEP (1)

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
 IncludeSubdirectories = $false             # <-- set this according to your requirements
 NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}

# Run python script, removing existing .pdf-files.
Write-Host "Running PS-script..."
start-process runrisk.bat -workingdirectory "H:\FX\"

# STEP (2)

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
 $path = $Event.SourceEventArgs.FullPath
 $name = $Event.SourceEventArgs.Name
 $changeType = $Event.SourceEventArgs.ChangeType
 $timeStamp = $Event.TimeGenerated

 # STEP (3)    

 # Call sendMail.ps1 to send mail confirmation
 invoke-expression -Command .\sendMail.ps1
 Write-Host "The file '$name' was $changeType at $timeStamp `r"

 # Print file
 start-process outputfile.pdf -workingdirectory "X:\folder1" -verb Print

 # STEP (4)
 # unregister current subscription
  Unregister-Event -SourceIdentifier FileCreated

}

I have built an .exe-file around this script. 我围绕此脚本构建了一个.exe文件。 If I run this .ps1 file directly from PowerShell, everything works as one would expect: (1) Create listener onto folder, (2) delete old .pdf-file and wait for new .pdf-file to be created, (3) print and send mail on creation-event, (4) unsubscribe listener. 如果我直接从PowerShell运行此.ps1文件,那么一切都会按预期进行:(1)在文件夹上创建侦听器,(2)删除旧的.pdf文件,并等待创建新的.pdf文件,(3)在创建事件上打印并发送邮件,(4)取消订阅侦听器。

When trying to run the .exe file, or when eg running the ps1-file through Windows Task Scheduler, it seems to only run until STEP (2), ie it seems as though no listener is created, and hence nothing happens when the bat-file creating .pdf-file is finished. 尝试运行.exe文件时,或例如通过Windows Task Scheduler运行ps1-文件时,它似乎只能运行到STEP(2),即好像没有创建任何侦听器,因此当蝙蝠时什么也没发生文件创建.pdf文件完成。 It just flashes some cmd-window and that's it. 它只是闪烁一些cmd窗口,仅此而已。

Any thoughts would be very much grateful! 任何想法将不胜感激!

Thanks, Niklas 谢谢,尼克拉斯

Your script executes Register-objectEvent and then immediately stops. 您的脚本执行Register-objectEvent,然后立即停止。 So if there will be any event triggered you don't allow the script to wait long enough to see any action associated with that trigger. 因此,如果有任何事件触发,则您不允许脚本等待足够长的时间才能看到与该触发器关联的任何操作。 One way to allow for enough time is to add a line like this to the end of the script: 允许有足够时间的一种方法是在脚本末尾添加以下行:

while ($true) {sleep 5}

The above line lets your script wait for any triggers to occur and the action to take place. 上面的代码行让您的脚本等待任何触发器发生和操作发生。 See the example on this article . 请参阅本文中的示例。

When you execute your script in a interactive PowerShell window, the register event listener persists after the script terminates, but only as long as you keep the window open. 在交互式PowerShell窗口中执行脚本时,脚本终止后,注册事件侦听器将继续存在,但前提是您保持窗口打开状态。 Probably this will let you see the action take place. 可能会让您看到正在执行的操作。 However as soon as you close the window, the eventlistener will get unregistered automatically and no action will be taken. 但是,一旦关闭窗口,事件侦听器将自动注销,并且不会执行任何操作。

I don't know exactly how creating an executable changes this behaviour, but certainly as I understand it, the exe that you create also terminates immediately and doesn't wait for the event to be triggered, which is not the right behaviour to get any results from your script. 我不完全知道创建可执行文件如何更改此行为,但是据我了解,您创建的exe也会立即终止并且不等待事件被触发,这不是获取任何内容的正确行为。您脚本的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM