效果预览:
实现代码:
$LogFile="$HOME\Desktop\log.txt" $PathToMonitor="$home\test" $TimeOut=1000 $FileSystemWatcher=New-Object System.IO.FileSystemWatcher $PathToMonitor $FileSystemWatcher.IncludeSubdirectories=$true Write-Host "Monitoring Content of $PathToMonitor" While($true) { $change=$FileSystemWatcher.WaitForChanged('All',$TimeOut) if($change.TimedOut -eq $false) { ##get information about change detected Write-Host "Change Detected:" $change|Out-Default (Get-Date),$change.ChangeType.ToString(),$change.Name|Out-File $LogFile -Append } else { Write-Host '.' -NoNewline } }