【问题标题】:FileSystemWatcher cmdlet in Powershell not monitoring system32 folder and subfoldersPowershell 中的 FileSystemWatcher cmdlet 不监视 system32 文件夹和子文件夹
【发布时间】:2023-03-16 03:34:01
【问题描述】:

我注意到,当使用 PowerShell 的 FileSystemWatcher cmdlet 时,它似乎没有监视 System32 或其在我的 Windows 7 计算机中的子文件夹。一个脚本 Í 在监视“我的文档”的子文件夹时工作得很好(即“C:\Users\W\Documents\Fichiers PowerShell”是一个有效的 fplder 路径)但是当我替换文件夹路径时不起作用System32(即 C:\Windows\System32\Recovery 是一个不起作用的路径)

这是我正在使用的脚本,但 System32 路径在其他 FileSystemWatcher 脚本中不起作用。任何有关解决方法的建议将不胜感激。我必须监控 C:\Windows\System32\Recovery。谢谢。

function FileSystemWatcher([string]$log = "C:\Users\W\Documents\Fichiers     PowerShell\newfiles.txt",
[string]$folder = "C:\Windows\System32\Recovery",
[string]$filter  = "*ps1",
[char]$timeout = 1000
){
$FileSystemWatcher = New-object System.IO.FileSystemWatcher $folder, $filter

Write-Host "Press any key to abort monitoring $folder"
do {

$result = $FileSystemWatcher.WaitForChanged("created", $timeout)

if ($result.TimedOut -eq $false) {
$result.Name |
  Out-File $log -Append 
Write-Warning ("Detected new file: " + $result.name)
 $filename = (gc $log -ea 0)



ii "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" 
remove-item "C:\Users\W\Documents\Fichiers PowerShell\newfiles.txt"


} 
} until ( [System.Console]::KeyAvailable )

Write-Host "Monitoring aborted."
Invoke-Item $log}



FileSystemWatcher

【问题讨论】:

    标签: powershell filesystemwatcher subdirectory system32


    【解决方案1】:

    试试这个:

    $fsw = New-Object System.IO.FileSystemWatcher C:\Windows\System32 -Property @{
        IncludeSubdirectories = $true              
        NotifyFilter = [System.IO.NotifyFilters]::DirectoryName
    }
    
    $event = Register-ObjectEvent $fsw Created -SourceIdentifier DirectoryCreated -Action { 
        Write-Host "$($event.SourceArgs | fl * | Out-String)"
    }
    
    md C:\Windows\System32\temp2
    

    【讨论】:

    • 感谢 Shay 帮了大忙
    猜你喜欢
    • 1970-01-01
    • 2013-01-18
    • 2014-08-31
    • 2017-12-19
    • 2020-01-05
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2012-02-22
    相关资源
    最近更新 更多