Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

IO.FileSystemWatcher odd behaviour

$
0
0

Community,

I hope you can help me with this one - I'm stumped!

Take the following examples:

Example 1- Invoke another external script with arguments:

$folder = 'C:\In'
$filter = '*.*'
$FileSystemWatcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
Register-ObjectEvent $FileSystemWatcher Created -SourceIdentifier FileCreated -Action {
$folder = 'c:\In'
$OutFolder = 'c:\Out'
$name = $Event.SourceEventArgs.Name
$InputFile=Get-Item $folder\$name
$WorkerProcess="C:\Temp\Worker_Process.ps1 -FilePath $InputFile"
#Invoke-Expression $WorkerProcess 
Move-Item $folder\$name $OutFolder -Force
}

Example 2 - Call a Function

Function GetUIDGID
{
# Get A new Number to use for GID/ UID
$RangesFile="C:\Ranges\PowerShellRanges.txt"
$CurrentValue="C:\Ranges\PowerShellCurrentValue.txt"
$Ranges=Get-Content $RangesFile
[INT]$Value=Get-Content $CurrentValue
$Ranges=$Ranges.Split(",")
[INT]$RangeMin=$Ranges[0]
[INT]$RangeMax=$Ranges[1]
# Add 1 to the current value to get the new value
[INT]$i=$Value+1
#Check that the new value is within the range specified in the Ranges File
If (($i -in $RangeMin..$RangeMax) -ne "True")
{
# Need to kill the script here and send mail!
Break
}
Else
{
# Write $i to the CurrentValue file and pass the variable onto whoever wanted the value....
Set-Content $CurrentValue $i
}
# Check to see if we are approaching the end of the range, unlikely to ever happen...
If ($i -gt $RangeMax - 100)
{
# Send an Email!
}
Else
{
Return $i
# No Problem! :)
}
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Start Script here....
$folder = 'c:\In'
$filter = '*.*'
$FileSystemWatcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
Register-ObjectEvent $FileSystemWatcher Created -SourceIdentifier FileCreated -Action {
$folder = 'c:\In'
$OutFolder = 'c:\Out'
$name = $Event.SourceEventArgs.Name
$InputFile=Get-Content $folder\$name
$ht=@{}
ForEach ($Line in $InputFile)
{
      $data=$Line.Split('=')
      $ht.Add($data[0],$data[1])
}
Move-Item $folder\$name $OutFolder -Force
GetUIDGID
}

In the first example, the Invoke-Expression line is commented out, in this state the FSW works fine - taking files in, processing the file path/ name and them moving them to the out folder.

However, if I uncomment the Invoke-Expression line the first file is processed, but subsequent ones are not.

Additionally, after the first command is processed, no more are able to be processed - even after stopping monitoring the folder by issuing "Unregister-Event FileCreated". All PS windows need to be closed to get going again.

In the second example, I'm using the suggestion from jrv (http://social.technet.microsoft.com/Forums/scriptcenter/en-US/3c685525-5e35-4a49-9c27-071d15805afe/unable-to-reference-items-in-a-hashtable?forum=ITCG) to simplify this issue. The script works fine until it is asked to do additional work - i.e. by calling the function "GetUIDGID". I saw similar issues with my original hashtable; where, once lines were added to the hashtable, the FileSystemWatcher would stop processing any more files. Commenting out the line that added entries to the hashtable made the issue go away!

dot sourcing this script allows the function to be used and the script then runs. However, I cannot get the result of the function ($i) back.

Is there an issue with buffering or a file not being released properly and thus processing stops?

I have tried every workaround that I have run into - all come back to using the FileSystemWatcher!

I'd appreciate any suggestions.

Many thanks,

Jon


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>