使用Powershell进行文件夹监视不写入文件夹

时间:2018-06-19 14:07:12

标签: powershell

我已经设置了当前未写入我的outlog.txt文件的Power Shell脚本,有人能看到明显的东西吗?我正在监视第一行中指出的C驱动器。 代码:

  $folder = 'C:\Users\GeorgeH\Documents\Count' # Enter the root path you want to monitor.
  $filter = '*.*'  # You can enter a wildcard filter here.

  # In the following line, you can change 'IncludeSubdirectories to $true 
  if    required.                          
   $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @
  {IncludeSubdirectories = $false;NotifyFilter =    
   [IO.NotifyFilters]'FileName, LastWrite'}

 # Here, all three events are registerd.  You need only subscribe to events 
 that you need:

 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
 Out-File -FilePath C:\Users\GeorgeH\outlog.txt -Append -InputObject "The    
 file '$name' was $changeType at $timeStamp"}

  Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action {
  $name = $Event.SourceEventArgs.Name
  $changeType = $Event.SourceEventArgs.ChangeType
  $timeStamp = $Event.TimeGenerated
  Write-Host "The file '$name' was $changeType at $timeStamp" -fore white
  Out-File -FilePath C:\Users\GeorgeH\Desktop\outlog.txt -Append -\
  InputObject "The file '$name' was $changeType at $timeStamp"}

0 个答案:

没有答案