2021年4月6日星期二

filesystemwatcher powershell working on my machine, but doesnt work on my co-worker's. The script is same

I am using filesystemwatcher to monitor creation and changes to a specific file using powershell. I guess this script polls for every 5 sec. Works on my machine. It does not work on my coworkers machine. Below is the script. Basically issue happens in DEFINE ACTIONS AFTER AN EVENT IS DETECTED

$action = { $path = $Event.SourceEventArgs.FullPath. For some reason it does not detect an event and invoke an action in his machine. I am trying to figure out the troubleshooting steps. Why does it work fine on my machine and not on his? PS-I have an admin rights on my machine and he is not an admin on his. The way I run this script is right click and run as powershell. Execution-mode is restricted in both our machines.

$global:DownloadsPath = (New-Object -ComObject Shell.application).NameSpace('shell:Downloads').Self.Path   $watcher = New-Object System.IO.FileSystemWatcher  $watcher.Path = $global:DownloadsPath  $watcher.Filter = "frmh.seq"   $watcher.IncludeSubdirectories = $false  $watcher.EnableRaisingEvents = $true       Write-Host ("Within ScriptMonitoring")     ### DEFINE ACTIONS AFTER AN EVENT IS DETECTED      $action = { $path = $Event.SourceEventArgs.FullPath    $FolderPath = $global:DownloadsPath    Write-Host ("Within ScriptMonitoring")   $loglocation = $FolderPath + "\Watcherlogs.txt"    $frmhLocation = $FolderPath + "\frmh.seq"  $controlDocLocation=  $FolderPath + "\control.doc"              $changeType = $Event.SourceEventArgs.ChangeType       $logline = "$(Get-Date), $changeType, $path"              Add-content $loglocation $logline                & ".\anotherscript.ps1" -SourceOfPcforms $FolderPath -SourceFileFrmhSeq $frmhLocation -         SourceFileControlDoc $controlDocLocation                        } ### DECIDE WHICH EVENTS SHOULD BE WATCHED    $created = Register-ObjectEvent $watcher "Created" -SourceIdentifier 'FileCreated' -Action $action   $changed = Register-ObjectEvent $watcher "Changed" -SourceIdentifier 'FileChanged' -Action $action        Write-Host ($created)        Write-Host ($changed)    while ($true) {sleep 5}  
https://stackoverflow.com/questions/66978308/filesystemwatcher-powershell-working-on-my-machine-but-doesnt-work-on-my-co-wor April 07, 2021 at 09:06AM

没有评论:

发表评论