Powershell 检查循环中的文件创建

Powershell 检查循环中的文件创建,powershell,Powershell,当.exe显示在文件夹上时,我可以运行此脚本一次吗 Get-ChildItem -Filter *.exe | ForEach { &$_.Fullname /s 我需要一个脚本来检查.exe文件是否显示在文件夹中,然后运行该命令,否则再次检查.exe文件。您可以使用FileSystemWatcher来侦听这些事件 Get-EventSubscriber -SourceIdentifier ExeCreated -ErrorAction SilentlyContinue | U

当.exe显示在文件夹上时,我可以运行此脚本一次吗

Get-ChildItem -Filter *.exe | ForEach {
    &$_.Fullname /s

我需要一个脚本来检查.exe文件是否显示在文件夹中,然后运行该命令,否则再次检查.exe文件。

您可以使用
FileSystemWatcher
来侦听这些事件

Get-EventSubscriber -SourceIdentifier ExeCreated -ErrorAction SilentlyContinue | Unregister-Event;
$Watcher = New-Object -TypeName System.IO.FileSystemWatcher;
$Watcher.Filter = '*.exe';
$Watcher.Path = 'c:\test';

$Action = { Write-Host -Object ('New file created: {0}' -f $event.SourceArgs[1].Name); };
Register-ObjectEvent -InputObject $Watcher -EventName Created -Action $Action -SourceIdentifier ExeCreated;