Powershell监控多个目录

2023-12-06

注意:运行 PowerShell v3

我目前的目录设置是:

\ftproot\001\converted
\ftproot\001\inbound
\ftproot\001\pdf

\ftproot\002\converted
\ftproot\002\inbound
\ftproot\002\pdf

\ftproot\xxx\converted
\ftproot\xxx\inbound
\ftproot\xxx\pdf

每个 FTP 用户都映射到一个入站目录。如果结构可以使解决方案更容易,则可以更改

\ftproot\converted\001
\ftproot\converted\002
\ftproot\converted\xxx

\ftproot\inbound\001
\ftproot\inbound\002
\ftproot\inbound\xxx

\ftproot\pdf\001
\ftproot\pdf\002
\ftproot\pdf\xxx

我需要监视每个入站目录中的 TIFF 文件,并在添加新的 FTP 用户和入站目录时拾取它们(遵循相同的结构),因此我不希望为每个新用户修改脚本。

目前的脚本是这样的:

$fileDirectory = "\ftproot";
$folderMatchString = "^[0-9]{3}$";
$inboundDirectory = "inbound";
$filter = "*.tif";

$directories = dir -Directory $fileDirectory | Where-Object {$_.Name -match $folderMatchString}

foreach ($directory in $directories) { 

  $sourcePath = "$fileDirectory\$directory\$inboundDirectory"

  if(Test-Path -Path $sourcePath -pathType container ) { 

        // Problem is here //
        $fsw = New-Object IO.FileSystemWatcher $sourcePath, $filter -Property @{
            IncludeSubdirectories = $false
            NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
        }

  }

}


$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {

 $path = $Event.SourceEventArgs.FullPath
 $name = $Event.SourceEventArgs.Name
 $changeType = $Event.SourceEventArgs.ChangeType
 $timeStamp = $Event.TimeGenerated

 Write-Host "The file '$name' was $changeType at $timeStamp"

}

我将 New-Object IO.FileSystemWatcher 包装在一个循环中,因此在上面的示例中,只会监视最后一个目录。

是否可以创建一个数组或列表或类似的 IO.FileSystemWatcher?如果是这样,我如何为每个实例编写 Register-ObjectEvent 代码?最终将有一百多个目录需要监控(并且正在缓慢增加),但相同的操作适用于所有入站文件夹。

或者我应该研究更好的解决方案?

每个 FTP 用户的过程都是相同的,文件需要位于可以链接回用户的目录中。将监视入站目录中是否有上传的 TIFF 文件,当检测到文件时,多页 TIFF 将被拆分为单独的文件并移动到转换后的目录中,当在转换中检测到新文件时,将对 TIFF 文件执行另一个操作,然后它被转换为 PDF 并移动到 PDF 文件夹。

谢谢

Solution

$result = @($directoriesOfInterest | ? { Test-Path -Path $_ } | % {

$dir = $_;

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

$oc = Register-ObjectEvent $fsw Created -Action {

 $path = $Event.SourceEventArgs.FullPath;
 $name = $Event.SourceEventArgs.Name;
 $changeType = $Event.SourceEventArgs.ChangeType;
 $timeStamp = $Event.TimeGenerated;

 Write-Host "The file '$name' was $changeType at $timeStamp";

};

new-object PSObject -Property @{ Watcher = $fsw; OnCreated = $oc };

});

是否可以创建一个数组或列表或类似的 IO.FileSystemWatcher?

是的。使用管道将类似于:

$fsws = $directoriesOfInterest | ? { Test-Path -Path $_ } | % {
          new-object IO.FileSystemWatcher …
        }

and $fsws如果创建了多个观察者,则将是一个数组(如果只有一个观察者将是一种浪费,$null如果没有)。为了始终获得数组,请将管道放入@(…):

$fsws = @($directoriesOfInterest | ? { Test-Path -Path $_ } | % {
          new-object IO.FileSystemWatcher …
        })

如果是这样,我如何为每个实例编写 Register-ObjectEvent 代码?

将对象注册放在同一个循环中,并返回观察者和事件注册:

$result = @($directoriesOfInterest | ? { Test-Path -Path $_ } | % {
            # $_ may have a different value in code in inner pipelines, so give it a
            # different name.
            $dir = $_;
            $fsw = new-object IO.FileSystemWatcher $dir, …;
            $oc =  Register-ObjectEvent $fsw Created …;
            new-object PSObject -props @{ Watcher = $fsw; OnCreated = $oc };
          })

and $result这两个属性将是一个对象数组。

请注意-Action代码传递给Register-ObjectEvent可以参考$fsw and $dir从而知道哪个观察者触发了它的事件。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Powershell监控多个目录 的相关文章

  • 如何在 PowerShell 中处理命令行参数

    处理命令行参数的 最佳 方法是什么 似乎有几个关于 最佳 方法是什么的答案 因此我陷入了如何处理像这样简单的事情上 script ps1 n name d domain AND script ps1 d domain n name 有没有一
  • 阻止单引号在 WMI 查询 powershell 中转义字符串

    我有一些使用 WMI 查询的代码 但我遇到了一个问题 我使用的变量有一个 单引号 这会导致代码出现故障 这是一个例子 path SERVER1 Mike O Leary servername path Split 2 Split 0 sha
  • 在 cygwin 和 powershell 中查看不同的 gem 列表

    我用的是gem listpowershell 和 cygwin 中的命令都显示不同的 gem 列表 Cygwin 显示 LOCAL GEMS bundler 1 2 3 json 1 7 5 minitest 2 12 1 rake 0 9
  • Foreach-object Parallel 块内的错误处理 - Powershell 7

    在下面的 Foreach Object Parallel 块中捕获错误的最佳方法是什么 因为将有三个单独的线程 运行空间运行并执行块中写入的代码 并且可能同时发生多个错误 异常 是否可以捕获列表 变量中的所有错误并在脚本执行结束时显示 1
  • Invoke-WebRequest SSL 失败?

    当我尝试使用时Invoke WebRequest我收到一些奇怪的错误 Invoke WebRequest Uri https idp safenames com Invoke WebRequest The underlying connec
  • 使用 shell_exec 将 PHP 转换为 Powershell

    如果我运行 output shell exec powershell get service dhcp 我得到了 dhcp 服务的完美输出 显示正在运行 但如果我运行 output shell exec powershell get use
  • 隐藏powershell输出

    我有以下脚本 param 参数 强制 true 字符串 dest New Item force path dest 1 itemtype directory New Item force path dest 2 itemtype direc
  • 无法在 cmd 中通过管道传递 cmdlet 对象

    我正在使用这个 powershell 命令来获取特定的用户配置文件 获取 WmiObject Class Win32 UserProfile Where Object LocalPath eq C Users Pela 但是当我通过调用 p
  • Invoke-Sqlcmd 运行脚本两次

    我遇到了一个非常奇怪的问题并且可以重复 基本上 我使用invoke sqlcmd通过使用 inputfile来调用脚本文件 但是如果脚本文件存在一些执行错误 例如插入到列不应为空的表中 则脚本文件将被执行两次 我也可以从探查器中看到这两个执
  • Start-Job 输出去了哪里?

    W gt job start job Write Output hi there throw an error Wait Job W gt job select State Failed HasMoreData True StatusMes
  • 用于检查 URL 状态的 PowerShell 脚本

    与此问题类似 https stackoverflow com questions 18500832 script to check the status of a url我正在尝试监视一组网站链接是否已启动并运行或没有响应 我在 Inter
  • 获取文件夹及其子文件夹中最长文件路径的长度

    我正在寻找一个可以从命令行 批处理 PowerShell 运行的脚本 该脚本将遍历文件夹及其子文件夹 并返回一个数字 该数字是最长文件路径的长度 我已经看到了一些批处理和 PowerShell 脚本 例如 如何在 Windows 中查找路径
  • 如何获取管道对象的数量?我不想累积管道来缓冲

    假设我有一些 powershell 代码 function count pipe CmdletBinding param Parameter ValueFromPipeline true object inputObject process
  • 如何从 PowerShell 指定应用程序池身份用户和密码

    我在自动化 Web 应用程序设置和使用应用程序池标识适当配置 IIS 方面遇到了很多困难 我在用 PowerShell 编写的 Web 应用程序部署脚本中执行此操作 我的要求是我需要 PowerShell 脚本将应用程序池身份用户设置为特定
  • Powershell:根据属性过滤属性

    我对 PowerShell 的自学经验有限 所以这可能是一些基本的东西 但我似乎无法正确理解 我在 Active Directory 中 需要提取电子邮件地址不以 SamAccountName 开头的用户列表 因此 如果您的登录名是 jdo
  • 替换 CSV 文件中的引号和特殊字符的一些难题

    我在处理一些需要清理并加载到数据库中的 CSV 文件时遇到了一些难题 我相当擅长使用 PowerShell 但不擅长正则表达式和 csv 列操作 这是我遇到的问题 我正在使用的 CSV 文件中有一个 注释 字段 可以包含各种不同的字符 主要
  • 使用 Powershell SQL 将数据提取到 Excel

    我想使用 powershell 将数据从 SQL Server 提取到新的 excel 文件 对于小型数据集 我的代码可以工作 但某些表的行数超过 100 000 行 这将需要很长时间 我不在 SQl 服务器中使用该实用程序的原因是因为我想
  • 带有多个附件的电子邮件

    我正在为服务台编写一个 PowerShell 脚本 以便在将 userhome 文件夹从服务器迁移到 NAS 设备时使用 帮助台用户将用户名输入到 userhomelist txt 文件中 我的问题是我无法获取脚本来附加所有日志文件 电子邮
  • 如何在 Powershell 中自动对提示回答“是”?

    如何在 PowerShell 会话中输入 是 作为交互式问题的答案 我知道 在 Bash 中 Yes是在提示上回答 是 的工具 在我的情况下 我无法抑制提示 我正在运行的脚本停止于 如果您想继续 请回复 是 powershell 如何运行脚
  • Powershell 中的“$”是什么?

    是什么意思 在 Powershell 中 Edit TechNet 答案 http technet microsoft com en us library hh847768 aspx同义反复 没有解释 成功 或 失败 的含义 包含上次操作的

随机推荐