I have a powershell script which runs FSW to monitor a directory into which new files are added.
When I run this script from the desktop, using Powershell running as administrator, it works perfectly. But when I run the same script in Task Manager as Admin it runs partially but does not appear to see the FSW response when a new file appears in
the monitored directory so it does not execute the code to respond to a new file.
I have checked permissions on the relevant folders and granted as much access as possible.
The outlog file containts just the following two lines:
On 08/25/2016 13:53:49 The script is unregistering the File creation event if it exists so that this script can register it..
On 08/25/2016 13:53:49 FSW started monitoring folder C:\Program Files (x86)\TradeStation 9.5\Scans\Strategy Scans\ATM Options Daily Scan.tsscan for the creation of a new scanner output file.
When a new file appears in the directory the script does not respond - though when run in powershell from the desktop it works perfectly... Steps 1 & 2 execute but Step 3 and subsequent steps do not run.
If anyone can suggest why this may be I would very much appreciate it.
Rapier
The script is as follows:
#By BigTeddy
September 2011 &
# modified by SRJ May & August 2016
#This script uses the .NET FileSystemWatcher class to monitor file events in folder(s).
#The advantage of this method over using WMI eventing is that this can monitor sub-folders.
#The -Action parameter can contain any valid Powershell commands. I have just included two for example.
# The script can be set to a wildcard filter, and IncludeSubdirectories can be changed to $true.
# You need not subscribe to all three types of event. All three are shown for example.
# Version 2.9
# Modified to address the errors in v2.7 - see the v2.7.txt file for details
# Version 2.11
# Revised to make it clearer how the fsw monitor is working.
# Version 3.o
# Minor amendments to debug operation under task manager.
# ------------------------------------------------------------------------
# Step 1: Unregister the file creation event before loading the new script
$Today = Get-Date
Unregister-Event -SourceIdentifier FileCreated
Write-Host "Unregistering the File creation event if it exists so that this script can register it."
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "On $Today The script is unregistering the File creation event if it exists so that this script can register it.."
# -------------------------------------------------------------------------------------
# Step 2 Monitor the scanner output folder for the creation of a new csv files (*.txt).
$folder = 'C:\Program Files (x86)\TradeStation 9.5\Scans\Strategy Scans\ATM Options Daily Scan.tsscan'
$filter = '*.txt'
Write-Host "FSW is now monitoring folder $folder for the creation of a new scanner output file."
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "On $Today FSW started monitoring folder $folder for the creation of a new scanner output file."
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
# -------------------------------------------------------------------------------------
# Step 3 Register the new file creation event.
Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
# -----------------------------------------------
# Step 4 When a file is created, do the following
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
$folder = 'C:\Program Files (x86)\TradeStation 9.5\Scans\Strategy Scans\ATM Options Daily Scan.tsscan'
$Today = Get-Date
Write-Host "FSW has detected a new scanner output file in $folder."
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "On $Today FSW detected a new scanner output file in $folder."
# clear the existing scan file
Write-Host "Deleting the existing scanner output file at C:\SRJ_ATM\DailyScan.txt."
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "On $Today Powershell is deleting the existing scanner output file at C:\SRJ_ATM\DailyScan.txt."
rm C:\SRJ_ATM\DailyScan.txt -ErrorAction Ignore
Write-Host "The file C:\SRJ_ATM\DailyScan.txt was removed if it existed"
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "On $Today the file C:\SRJ_ATM\DailyScan.txt was removed if it existed"
# get details of the new file
Write-Host "The file $folder'\'$name was $changeType at $timeStamp" -fore green
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "On $Today the file $folder\$name was $changeType at $timeStamp"
# If this is the CSV file store the file name in the $CSVName variable
Write-Host "Verifying that this is the Scanner Output csv file."
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "On $Today Powershell is verifying that this is the Scanner Output csv file."
$csv = $Event.SourceEventArgs.Name.Endswith(".txt")
if ($csv)
{
$CSVName = $Event.SourceEventArgs.Name
Write-Host "Powershell identified $folder\$CSVName as the scanner output txt file"
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "On $Today Powershell identified $folder'\'$CSVName as the scanner output txt file."
# rename the scanner output file
Write-Host "Powershell is renaming the scanner output file"
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "On $Today Powershell is renaming the scanner output txt file."
Rename-Item $folder\$CSVname $folder\DailyScan.txt
Write-Host "The file $folder\$CSVname was renamed to DailyScan.txt"
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "On $Today The file $folder\$CSVname was renamed to DailyScan.txt"
Write-Host "Move $folder\DailyScan.txt to c:\SRJ_ATM\"
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "On $Today the file $folder\DailyScan.txt' is being moved to c:\SRJ_ATM\"
Move-Item $folder\DailyScan.txt c:\SRJ_ATM\
Write-Host "The file $folder\DailyScan.txt was moved to c:\SRJ_ATM\"
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "On $Today the file $folder\DailyScan.txt' was moved to c:\SRJ_ATM"
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "------------------------------------------------------------------------------------------------------."
}
else {
Write-Host "Powershell determined that $CSVName is NOT a txt file"
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "On $Today Powershell determined this was not the csv file."
Out-File -FilePath c:\SRJ_ATM\outlog.txt -Append -InputObject "-----------------------------------------------------------------."
}
}