Power Shell脚本以监视文件夹中的文件更改并在控制台上打印信息 [英] Power Shell Script to Monitor Folder for File change and print the information on console

查看:148
本文介绍了Power Shell脚本以监视文件夹中的文件更改并在控制台上打印信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻求帮助

我正在尝试编写一种实用程序,该实用程序监视Folder中的任何文件更改并在Power Shell控制台上打印信息

在下面的问题中找到了很多帮助,这要归功于OP和线程中的答案.

当我执行上面的脚本时,我遇到了异常

Register-ObjectEvent : Cannot subscribe to event. A subscriber with source identifier 'FileCreated' already exists.
At C:\Users\sysadmin\Desktop\FileWatcher.ps1:6 char:21
+ Register-ObjectEvent <<<<  $fsw Created -SourceIdentifier FileCreated -Action {
    + CategoryInfo          : InvalidArgument: (System.IO.FileSystemWatcher:FileSystemWatcher) [Register-ObjectEvent],
    ArgumentException
    + FullyQualifiedErrorId : SUBSCRIBER_EXISTS,Microsoft.PowerShell.Commands.RegisterObjectEventCommand

我刚开始使用Powershell,因为我需要一个监视文件夹中文件更改的实用程序,请让我知道以上代码中是否有任何错误,我只是想在控制台上打印更改后的文件信息./p>

我们非常感谢您的帮助

谢谢.

解决方案

如果要重新运行相同的脚本,请在Register-ObjectEvent条目之前添加以下行:

Unregister-Event FileCreated -ErrorAction:SilentlyContinue

Looking for help

I am trying to write a utility that monitors any file change in Folder and print the info on the power shell console

Found lot of help from below question, Thanks to OP and answers in the thread.

Powershell script to run a .bat file when a file is added to a folder

I have a script something like this now

$folder = '\\{Networkname}\Partner1\' # Enter the root path you want to monitor. 
$filter = '*'  # You can enter a wildcard filter here. 

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
write-host "test"

}

When I execute the above script, I am getting below exception

Register-ObjectEvent : Cannot subscribe to event. A subscriber with source identifier 'FileCreated' already exists.
At C:\Users\sysadmin\Desktop\FileWatcher.ps1:6 char:21
+ Register-ObjectEvent <<<<  $fsw Created -SourceIdentifier FileCreated -Action {
    + CategoryInfo          : InvalidArgument: (System.IO.FileSystemWatcher:FileSystemWatcher) [Register-ObjectEvent],
    ArgumentException
    + FullyQualifiedErrorId : SUBSCRIBER_EXISTS,Microsoft.PowerShell.Commands.RegisterObjectEventCommand

I just started with Powershell as I need a utility that monitors File change in Folder, please let me know if there is anything that's wrong in the above code, I am just trying to print the changed file information on the console.

Any help is highly appreciated

Thanks.

解决方案

If you want to re-run the same script, add the following line before the Register-ObjectEvent entry:

Unregister-Event FileCreated -ErrorAction:SilentlyContinue

这篇关于Power Shell脚本以监视文件夹中的文件更改并在控制台上打印信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆