Hey guys,
I have FileSystemWatcher app watching for any new files created in a directory and writing a log file of everything new created. This part works great. However, I'm also trying to write this same data to an Access
database and this is where everything get's screwy.
Randomly the FileSystemWatcher will stop processing files. It could be 2 files or 30 files and everything locks up and I need to restart the process. Any thoughts on this ? Here's my code im using. Like I said everything works great writing to a text file it's
only when I incorporate the Access part stuff fails. I'm running Windows 7 64bit with Office 2010 64bit and Powershell 4.0.
I'm assuming it's something with my Access Insert statement but really unsure. This is my first attempt at dumping data to an Access database from Powershell. It also appears my Database isn't closing properly as my Access Lock file still exists when this
whole process hangs.
If there's a better way to accomplish this im all ears. I do have the Log files with all the info I need a well. If I could import that to the Access database easier so the watcher doesn't hang I'm all ears on that one as well.
Thanks
Rich T.
#### DEFINE WATCH FOLDERS AND DEFAULT FILE EXTENSION TO WATCH FOR ####
#$cofa_folder = '\\cpsfs001\Data_pvs\TestCofA'
$cofa_folder = "c:\AdageCofA"
#$bulk_folder = '\\cpsfs001\PVS\Subsidiary\Nolwood\McWood\POD'
#$bulk_folder = 'c:\Bulk'
$filter = "*.tif"
$cofa = New-Object IO.FileSystemWatcher $cofa_folder, $filter -Property @{ IncludeSubdirectories = $false; EnableRaisingEvents= $true; NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite' }
#$bulk = New-Object IO.FileSystemWatcher $bulk_folder, $filter -Property @{ IncludeSubdirectories = $false; EnableRaisingEvents= $true; NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite' }
#### CERTIFICATE OF ANALYSIS AND PACKAGE SHIPPER PROCESSING ####
Register-ObjectEvent $cofa Changed -SourceIdentifier COFA/PACKAGE -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
#### CERTIFICATE OF ANALYSIS PROCESS BEGINS ####
$test=$name.StartsWith("I")
if ($test -eq $true) {
$pos = $name.IndexOf(".")
$left=$name.substring(0,$pos)
$pos = $left.IndexOf("L")
$tempItem=$left.substring(0,$pos)
$lot = $left.Substring($pos + 1)
$item=$tempItem.Substring(1)
#### FOR TESTING ONLY. WILL OUTPUT DATA TO A POWERSHELL PROMPT
Write-Host "in_item_key $item in_lot_key $lot imgfilename $name in_cofa_crtdt $timestamp" -fore green
#### LOGS ALL TRANSACTIONS FOR CERTIFICATE OF ANALYSIS ####
Out-File -FilePath c:\OutputLogs\CofA.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"
#### WRITES INFORMATION TO SQL DATABASE "AdageLIVE" ####
$conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=PVSNTDB33; Initial Catalog=adagecopy_daily; Integrated Security=TRUE")
$conn.Open()
$insert_stmt = "INSERT INTO in_cofa_pvs (in_item_key, in_lot_key, imgfileName, in_cofa_crtdt) VALUES ($item,$lot,$name,$timestamp)"
$cmd = $conn.CreateCommand()
$cmd.CommandText = $insert_stmt
$cmd.ExecuteNonQuery()
$conn.Close()
}
#### PACKAGE SHIPPER PROCESS BEGINS ####
elseif ($test -eq $false) {
$pos = $name.IndexOf(".")
$left=$name.substring(0,$pos)
$pos = $left.IndexOf("O")
$tempItem=$left.substring(0,$pos)
$order = $left.Substring($pos + 1)
$shipid=$tempItem.Substring(1)
#### FOR TESTING ONLY. WILL OUTPUT DATA TO A POWERSHELL PROMPT ####
Write-Host "so_hdr_key $order so_ship_key $shipid imgfilename $name in_cofa_crtdt $timestamp" -fore green
#### LOGS ALL TRANSACTIONS FOR PACKAGE SHIPPERS ####
Out-File -FilePath c:\OutputLogs\PackageShipper.txt -Append -InputObject "$order,$shipid,$name,$timeStamp"
#### WRITES INFORMATION TO MICROSOFT ACCESS DATABASE "G:\AdageCofA\CofA_140506.mdb" ####
#Start-Sleep -s 10
$conn=New-Object System.Data.OleDb.OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0; Data Source=c:\test.accdb")
$conn.Open()
$insert_stmt = "INSERT INTO dbo_so_shprimg_pvs (so_hdr_key, so_ship_key, imgfileName, so_shprimg_crtdt) VALUES ('$order','$shipid','$name','$timestamp')"
$cmd = $conn.CreateCommand()
$cmd.CommandText = $insert_stmt
$cmd.ExecuteNonQuery()
$conn.Close()
}
}
#### BULK SHIPPER PROCESSING ####
#Register-ObjectEvent $bulk Created -SourceIdentifier BULK -Action {
#$name = $Event.SourceEventArgs.Name
#$changeType = $Event.SourceEventArgs.ChangeType
#$timeStamp = $Event.TimeGenerated
#### FOR TESTING ONLY. WILL OUTPUT DATA TO A POWERSHELL PROMPT ####
#Write-Host "$name was $changeType at $timeStamp" -fore green
#### LOGS ALL TRANSACTIONS FOR BULK SHIPPERS ####
#Out-File -FilePath c:\OutputLogs\BulkShipper.txt -Append -InputObject "$name,$timeStamp"
#}
# To stop the Nolwood Watcher Registered Event open Powershell and Type Unregister-Event *
# To start the Nolwood Watcher Registered Event open Powershell and navigate to c:\Users\02DocScan\Desktop and run the NolwoodFileWatcher.ps1
Rich Thompson