如何使用 powershell 在显示屏上打开文件

如何使用 powershell 在显示屏上打开文件

我正在尝试创建一个脚本,每次特定目录发生变化时通知我。

我想确保我看到通知,因此一旦注册了新通知,我就会在全部显示中弹出日志文件。这是我正在使用的脚本:

$filewatcher = New-Object System.IO.FileSystemWatcher
#Mention the folder to monitor
$filewatcher.Path = "C:\Users\zeevh\Downloads\"
$filewatcher.Filter = "*.*"
#include subdirectories $true/$false
$filewatcher.IncludeSubdirectories = $true
$filewatcher.EnableRaisingEvents = $true  

$writeaction = { $path = $Event.SourceEventArgs.FullPath
                 $changeType = $Event.SourceEventArgs.ChangeType
                 $logline = "$(Get-Date), $changeType, $path"
                 Add-content "C:\Users\zeevh\Documents\FileWatcher_log.txt" -value $logline
Invoke-Item "C:\Users\zeevh\Documents\FileWatcher_log.txt"
               }    

Register-ObjectEvent $filewatcher "Created" -Action $writeaction
Register-ObjectEvent $filewatcher "Changed" -Action $writeaction
Register-ObjectEvent $filewatcher "Deleted" -Action $writeaction
Register-ObjectEvent $filewatcher "Renamed" -Action $writeaction
while ($true) {sleep 5}

现在我使用“Invoke-Item”(也尝试过“start”)命令,但它只是打开文件而不将其置于最前面。为什么要这样做

答案1

为什么不直接使用 toast 通知来代替您的努力呢?查看/使用 BurntToast 模块和您的 FileSystemWatcher 代码。这就是 Windows 中存在通知的原因。

Find-Module -Name '*toast*' | 
Format-List
# Results
<#


Name                       : BurntToast
Version                    : 0.8.3
Type                       : Module
Description                : Module for creating and displaying Toast Notifications on Microsoft Windows 10.
Author                     : Joshua (Windos) King
CompanyName                : Windos
Copyright                  : (c) 2015 Joshua (Windos) King. All rights reserved.
PublishedDate              : 04-Oct-20 21:26:21
...
#>

模块星期一:BurntToast

https://www.youtube.com/watch?v=TwZjr66yfc8&feature=youtu.be

相关内容