user1438567
user1438567

Reputation:

FileSystemWatcher cmdlet in Powershell not monitoring system32 folder and subfolders

I noticed that when using PowerShell's FileSystemWatcher cmdlet that it doesnt seem to monitor System32 or it's subfolders in my Windows 7 computer. A script Í have that works just fine when monitoring subfolders of My Documents (i/e "C:\Users\W\Documents\Fichiers PowerShell" is a fplder path that works) but doesn't work when I substitute a folder path in System32 (i/e C:\Windows\System32\Recovery is a path that doesn't work)

Here is the script i'm working with but System32 paths haven't worked in other FileSystemWatcher scripts. Any advice as to a workaround would be appreciated . I must monitor C:\Windows\System32\Recovery. Thank You.

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

Upvotes: 1

Views: 1361

Answers (1)

Shay Levy
Shay Levy

Reputation: 126842

Give this a try:

$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

Upvotes: 1

Related Questions