8
0
2024-01-24 16:42:38 +01:00

752 lines
40 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# PowerShell 4.0 Script
# Script for handeling rejected files, with SQL-DB support
# Digital Data
# Ludwig-Rinn-Strasse 16
# 35452 Heuchelheim
# Tel.: 0641 / 202360
# E-Mail: info@didalog.de
# Version Number: 1.0.0.0 Pre-Release
# Version Date: 30.09.2015
# Minimum Requirement for this Script:
# Microsoft Windows XP SP3 / Server 2008 R2 SP1 -> look at KB976932
# Microsoft .NET Framework 4.5 -> look at KB2858728
# Microsoft PowerShell 4.0 -> look at KB2819745
# Microsoft SQL Client is needed!
# WICHTIG: Falls sich dieses Skript nicht ausführen lässt,
# gibt es mehrere Möglichkeiten die Ausführung zuzulassen.
# Zwei Möglichkeiten lauten wie folgt:
#
# 1. PowerShell Skripte generell erlauben.
# Dazu muss dieser PowerShell-Befehl noch mit administrativen Rechten ausgeführt werden:
# "set-executionpolicy unrestricted"
#
# 2. Die Ausführungseinschränkung für dieses Skript durch einen veränderten Aufruf umgehen:
# PowerShell.exe -ExecutionPolicy Bypass -File <ThisScript.ps1>
#Requires Version 4.0
#-----------------------------------------------------------------------------------------------------#
###################################### add additional assemblys #######################################
#-----------------------------------------------------------------------------------------------------#
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#-----------------------------------------------------------------------------------------------------#
############################################ set variables ############################################
#-----------------------------------------------------------------------------------------------------#
Set-Variable -Name ScriptName -Value (($MyInvocation.MyCommand.Name) -split "\.")[0].ToString() -Scope script
Set-Variable -Name ScriptPath -Value (Split-Path ($MyInvocation.MyCommand.Path)) -Scope script
Set-Variable -Name ConfigFile -Value (Get-ChildItem -Path "$ScriptPath" -Recurse -Filter "$ScriptName`_Settings.ini" -File -Force).FullName -Scope script
Set-Variable -Name ConfigValues -Value $NULL -Scope script
Set-Variable -Name Timestamp1 -Value $(Get-Date -Format 'ddMMyyyy') -Scope script
Set-Variable -Name Timestamp2 -Value $(Get-Date -Format 'ddMMyyyy_HHmmss') -Scope script
Set-Variable -Name Timestamp3 -Value $(Get-Date -Format 'ddMMyyyy_HHmmssffff') -Scope script
Set-Variable -Name Timestamp4 -Value $(Get-Date -Format 'yyyyMMdd HH:mm:ss.fff') -Scope script
Set-Variable -Name LogFile -Value "$ScriptName`_$Timestamp2.log" -Scope script
Set-Variable -Name LogFileKeepTime -Value 900 -Scope script
Set-Variable -Name LogLine -Value $NULL -Scope local
Set-Variable -Name LogPath -Value $NULL -Scope script
Set-Variable -Name LogPathError -Value $NULL -Scope local
Set-Variable -Name LogPathValue -Value $NULL -Scope script
Set-Variable -Name LogPathValues -Value $NULL -Scope script
Set-Variable -Name FileDelayAge -Value 5 -Scope script
Set-Variable -Name FilePath_and_FileName -Value $NULL -Scope local
Set-Variable -Name FileCheckResult -Value $NULL -Scope local
Set-Variable -Name SourcePathSplit -Value $NULL -Scope script
Set-Variable -Name Item -Value $NULL -Scope local
Set-Variable -Name Items -Value $NULL -Scope local
Set-Variable -Name Path -Value $NULL -Scope local
Set-Variable -Name PathTest -Value $NULL -Scope local
Set-Variable -Name VersionSeperator -Value '~' -Scope script
Set-Variable -Name FileSeperator -Value '_' -Scope script
Set-Variable -Name Counter -Value $NULL -Scope local
Set-Variable -Name FileName -Value $NULL -Scope local
Set-Variable -Name FileName_noExt -Value $NULL -Scope local
Set-Variable -Name FileHash -Value $NULL -Scope local
Set-Variable -Name FileQuery -Value $NULL -Scope local
Set-Variable -Name FileQuery_Limit -Value $NULL -Scope local
Set-Variable -Name filestream -Value $NULL -Scope local
Set-Variable -Name SourcePath -Value $NULL -Scope local
Set-Variable -Name Destinationpath -Value $NULL -Scope local
Set-Variable -Name newfilename -Value $NULL -Scope local
Set-Variable -Name newfilenamelength -Value $NULL -Scope local
Set-Variable -Name DBSQLDataSet -Value $NULL -Scope script
Set-Variable -Name DBSQLQuery -Value $NULL -Scope script
Set-Variable -Name DBSQLCommand -Value $NULL -Scope script
Set-Variable -Name DBSQLAdapter -Value $NULL -Scope script
Set-Variable -Name DBSQLConnection -Value $NULL -Scope script
Set-Variable -Name DBSQLConnectServer -Value $NULL -Scope script
Set-Variable -Name DBSQLConnectUser -Value $NULL -Scope script
Set-Variable -Name DBSQLConnectPassword -Value $NULL -Scope script
Set-Variable -Name DBSQLConnectDatabase -Value $NULL -Scope script
Set-Variable -Name DBSQLConnectIntegratedSecurity -Value $NULL -Scope script
Set-Variable -Name DBSQLErrorAction -Value 'continue' -Scope script
Set-Variable -Name DBSQL_NewFilename -Value $NULL -Scope script
#-----------------------------------------------------------------------------------------------------#
############################################ set functions ############################################
#-----------------------------------------------------------------------------------------------------#
#Most of the necessery functions will be loaded as moduls (.psm1 files) in PSSession in the preparing part.
#-----------------------------------------------------------------------------------------------------#
########################################### preparing part ############################################
#-----------------------------------------------------------------------------------------------------#
#Clear Console Window
Clear-Host
#Load Functions into current PSSession
$RegTest = (Test-Path -Path "HKLM:\SOFTWARE\Digital Data\FunctionModules")
IF ($RegTest -eq $True) {
Try {
$PSModuleItems = Get-ChildItem -Path (Get-ItemPropertyValue "HKLM:\SOFTWARE\Digital Data\FunctionModules" -Name PSRepositoryPath1) -Filter *.psm1
Write-Host "DEBUG: $PSModuleItems"
FOREACH ($PSModuleItem in $PSModuleItems) {
Try {
Write-Host "DEBUG: Loading PowerShell Module:" $PSModuleItem.FullName
#Import-Module $PSModuleItem.FullName
$var1 =Import-Module -PassThru -Function $PSModuleItem.FullName -Global -name $PSModuleItem.FullName #-Force -DisableNameChecking
write-host $var1
} #end try
Catch {
Write-Host "DEBUG: WARNING Error while loading PowerShell Module:" $PSModuleItem.FullName
} #end catch
} #end foreach
} #end try
Catch {
$form = new-object Windows.Forms.Form
$form.Text = Beispielfenster
$button = new-object Windows.Forms.Button
$button.Text = Yes
$button.Left = 100
$button.Top = 100
$button.Width = 50
$button.Height = 25
$button.Add_Click({$form.close()})
$button2 = new-object Windows.Forms.Button
$button2.Text = No
$button2.Left = 100
$button2.Top = 100
$button2.Width = 50
$button2.Height = 25
$button2.Add_Click({$form.close()})
$form.Controls.Add($button)
$form.ShowDialog()
} #end catch
} #end if
ELSE {
$form = new-object Windows.Forms.Form
$form.Text = Beispielfenster
$button = new-object Windows.Forms.Button
$button.Text = Yes
$button.Left = 100
$button.Top = 100
$button.Width = 50
$button.Height = 25
$button.Add_Click({$form.close()})
$button2 = new-object Windows.Forms.Button
$button2.Text = No
$button2.Left = 200
$button2.Top = 200
$button2.Width = 50
$button2.Height = 25
$button2.Add_Click({$form.close()})
$form.Controls.Add($button)
$form.Controls.Add($button2)
$form.ShowDialog()
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowser.Description = "Select Folder within the PowerShell Function Scripts (PSM1 Files), please!"
$FolderBrowser.ShowDialog()
$folder = $FolderBrowser.SelectedPath
write-host $Folder
Try {
New-Item -Path "HKLM:\SOFTWARE\Digital Data\FunctionModules" -Force -ErrorAction Stop | Out-Null
New-ItemProperty -Path "HKLM:\SOFTWARE\Digital Data\FunctionModules" -Name PSRepositoryPath1 -Value $Folder -ErrorAction Stop | Out-Null
} #end try
Catch {
$form = new-object Windows.Forms.Form
$form.Text = Beispielfenster
$form.AcceptButton
$button = new-object Windows.Forms.Button
#$button.Text = „Yes“
$button.Left = 100
$button.Top = 100
$button.Width = 50
$button.Height = 25
$button.Add_Click({$form.close()})
$form.Controls.Add($button)
$form.Controls.Add($button2)
$form.ShowDialog()
} #end catch
} #end elseif
pause
#Read ConfigValues from ConfigFile
Func-Read-ConfigFile -ConfigFile $ConfigFile
write-warning $ConfigValues
#Read each ConfigValue from ConfigValues
Set-Variable -Name LogPathValues -Value (Func-Read-ConfigValue -ConfigLabel LogPathValue)
Set-Variable -Name LogFileKeepTime -Value (Func-Read-ConfigValue -ConfigLabel LogFileKeepTime)
Set-Variable -Name FileDelayAge -Value (Func-Read-ConfigValue -ConfigLabel FileDelayAge)
Set-Variable -Name FileSeperator -Value (Func-Read-ConfigValue -ConfigLabel FileSeperator)
Set-Variable -Name VersionSeperator -Value (Func-Read-ConfigValue -ConfigLabel VersionSeperator)
Set-Variable -Name FileNameConstructor -Value (Func-Read-ConfigValue -ConfigLabel FileNameConstructor)
Set-Variable -Name DBSQLConnectServer -Value (Func-Read-ConfigValue -ConfigLabel DBSQLConnectServer)
Set-Variable -Name DBSQLConnectUser -Value (Func-Read-ConfigValue -ConfigLabel DBSQLConnectUser)
Set-Variable -Name DBSQLConnectPassword -Value (Func-Read-ConfigValue -ConfigLabel DBSQLConnectPassword)
Set-Variable -Name DBSQLConnectDatabase -Value (Func-Read-ConfigValue -ConfigLabel DBSQLConnectDatabase)
Set-Variable -Name DBSQLConnectIntegratedSecurity -Value (Func-Read-ConfigValue -ConfigLabel DBSQLConnectIntegratedSecurity)
Set-Variable -Name DBSQLErrorAction -Value (Func-Read-ConfigValue -ConfigLabel DBSQLErrorAction)
Set-Variable -Name feh_rulelist -Value (Func-Read-ConfigValue -ConfigLabel feh_rule??)
pause
FOREACH ($feh_rule in $feh_rulelist) {
Write-Host ""
Write-Host "DEBUG Info: Folgende Zeilen wurden ausgelesen: --> $feh_rule <--"
$Counter = $Counter -as [int]
$Counter++ | Out-Null
#If Counter is less then 10, we need to fill it with a zero
IF ($counter -lt 10) {
$counter = $counter -as [String]
$counter = ("0"+$counter)
} #end if
IF ($feh_rule -is [String]) {
$feh_rule = @($feh_rule)
$feh_rule = ($feh_rule -split ";")
#$feh_rule = ($feh_rule -split ",")
} #end if
ELSE {
$feh_rule = @($feh_rule)
$feh_rule = ($feh_rule -Split "=" )
$feh_rule = ($feh_rule[1])
$feh_rule = ($feh_rule -split ";")
#$feh_rule = ($feh_rule -split ",")
} #end else
[System.Collections.ArrayList]$feh_rulevalues = @()
# Split an merge Array to remove blanks
FOREACH ($feh_rulevalue in $feh_rule) {
$feh_rulevalue = ($feh_rulevalue.TrimStart())
$feh_rulevalue = ($feh_rulevalue.TrimEnd())
$feh_rulevalues.Add($feh_rulevalue)
} #end foreach
IF ($feh_rulevalues -eq $NULL) {
Write-Host "DEBUG Info: Der erwarteter Wert ist leer!"
Write-Host "DEBUG Info: Standardwert wird eingetragen!"
Set-Variable -Name ("feh_rule"+$Counter) -value "Standard" -Scope script
} #end if
ELSE {
Write-Host DEBUG Info: ("feh_rule"+$Counter) wurde als Variable gesetzt und hat folgende Werte bekommen: $feh_rulevalues
Set-Variable -Name ("feh_rule"+$Counter) -value $feh_rulevalues -Scope script
} #end else
} #end foreach
#-----------------------------------------------------------------------------------------------------#
############################################# main part ###############################################
#-----------------------------------------------------------------------------------------------------#
Write-Host ""
Func-Write-Logfile -LogLine "*******************************************************************************************"
Func-Write-Logfile -LogLine "Program Startup: $ScriptName on $env:computername from Account $env:USERDOMAIN\$env:USERNAME."
Func-Write-Logfile -LogLine "*******************************************************************************************"
IF ($($feh_rulelist.count) -gt 0) {
Func-Write-Logfile -LogLine "There is/are $($feh_rulelist.count) out of 99 Rule(s) to process, starting now."
#Reset counter
Set-Variable -Name Counter -Value $NULL
#Loop for each rule
FOREACH ($feh_rule in $(get-variable -Name feh_rule??)) {
$Counter = $Counter -as [int]
$Counter++ | out-null
#If Counter is less then 10, we need to fill it with a zero
IF ($counter -lt 10) {
$counter = $counter -as [String]
$counter = ("0"+$counter)
} #end if
Func-Write-Logfile -LogLine ""
Func-Write-Logfile -LogLine "==========================================================================================="
Func-Write-Logfile -LogLine "Processing $((Get-Variable -name ("feh_rule"+$Counter)).name) now."
Func-Write-Logfile -LogLine "==========================================================================================="
Func-Write-Logfile -LogLine ""
Func-Write-Logfile -LogLine "These Values are setup: $(Get-Variable -Name ("feh_rule"+$Counter) -ValueOnly)."
# Save all given parameters of this profile.
Set-Variable -Name feh_rulevalues -Value $(get-variable -name ("feh_rule"+$Counter) -ValueOnly) -Scope local
# Save the first array value in the given parameters of this profile.
Set-Variable -Name FileTyps -Value $(get-variable -name ("feh_rule"+$Counter) -ValueOnly)[0] -Scope local
# Save the second array value in the given parameters of this profile.
Set-Variable -Name SourcePath -Value $(get-variable -name ("feh_rule"+$Counter) -ValueOnly)[1] -Scope local
# Save the third array value in the given parameters of this profile.
Set-Variable -Name DestinationPath -Value $(get-variable -name ("feh_rule"+$Counter) -ValueOnly)[2] -Scope local
# Save the fourth array value in the given parameters of this profile.
Set-Variable -Name EscalationPath -Value $(get-variable -name ("feh_rule"+$Counter) -ValueOnly)[3] -Scope local
# Save the fifth array value in the given parameters of this profile.
Set-Variable -Name FileQuery_Limit -Value $(get-variable -name ("feh_rule"+$Counter) -ValueOnly)[4] -Scope local
$FileQuery_Limit = $FileQuery_Limit -as [Int]
# If more than five parameters are given, check optional parameters.
IF ($(Get-Variable -Name ("feh_rule"+$Counter) -ValueOnly).count -gt 5) {
# Set value for date if given in the parameters.
IF ($(Get-Variable -Name ("feh_rule"+$Counter) -ValueOnly) -contains "%date") {
Set-Variable -Name newfilename -Value ($Timestamp1+$FileSeperator)
} #end if
ELSEIF ($(Get-Variable -Name ("feh_rule"+$Counter) -ValueOnly) -contains "%datetime") {
Set-Variable -Name newfilename -Value ($Timestamp2+$FileSeperator)
} #end elseif
ELSEIF ($(Get-Variable -Name ("feh_rule"+$Counter) -ValueOnly) -contains "%datetime2") {
Set-Variable -Name newfilename -Value ($Timestamp3+$FileSeperator)
} #end elseif
# Set value for sourcepath if given in the parameters.
IF ($(get-variable -Name ("feh_rule"+$Counter) -ValueOnly) -contains "%sourcefolder") {
Set-Variable -Name SourcePathSplit -Value ($SourcePath -split "\\")
Set-Variable -Name SourcePathSplit -Value ($SourcePathSplit[-1])
Set-Variable -Name newfilename -Value ($newfilename+$SourcePathSplit+$FileSeperator)
} #end if
ELSEIF ($(get-variable -Name ("feh_rule"+$Counter) -ValueOnly) -contains "%sourcefolder2") {
Set-Variable -Name SourcePathSplit -Value ($SourcePath -split "\\")
Set-Variable -Name SourcePathSplit -Value ($SourcePathSplit[-2])
Set-Variable -Name newfilename -Value ($newfilename+$SourcePathSplit+$FileSeperator)
} #end elseif
ELSEIF ($(get-variable -Name ("feh_rule"+$Counter) -ValueOnly) -contains "%sourcefolder3") {
Set-Variable -Name SourcePathSplit -Value ($SourcePath -split "\\")
Set-Variable -Name SourcePathSplit -Value ($SourcePathSplit[-3])
Set-Variable -Name newfilename -Value ($newfilename+$SourcePathSplit+$FileSeperator)
} #end elseif
# Set value for static text if given in the parameters.
IF ($(get-variable -name ("feh_rule"+$Counter) -ValueOnly) -like "%%*%%") {
Set-Variable -Name statictext -Value ((($(get-variable -name ("feh_rule"+$Counter) -ValueOnly) -like "%%*%%") | Where-Object {$_ like "%%*%%"}).ToString()).Replace("%%",'')
Set-Variable -Name newfilename -Value ($newfilename+$statictext+$FileSeperator)
} #end if
ELSE {
Set-Variable -Name statictext -value $NULL
} #end else
} #end if
Func-Write-Logfile -LogLine "This run, searches for files in folder: $SourcePath"
Set-Variable -Name Items -Value (Func-File-Collector -SearchPath $SourcePath -SearchWhiteList $filetyps | where-object {$_.lastwritetime -lt (get-date).addminutes(-$FileDelayAge)})
#Loop for each file found in the SourcePath, processing by current profile.
FOREACH ($Item in $Items) {
Func-Write-Logfile -LogLine ""
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
Func-Write-Logfile -LogLine "Processing file: $Item"
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
Set-Variable -Name FileHash -Value (Get-FileHash -Path "$($Item.FullName)" -Algorithm SHA512 | select Hash | Foreach-Object {$_ -replace "@{Hash=", ""} | Foreach-Object {$_ -replace "}", ""})
Write-Host "DEBUG Info: Filehash: $FileHash"
$DBSQLDataSet.clear
Set-Variable -Name DBSQLDataSet -Value (Func-DB-SQL-Query -DBSQLQueryType read -DBSQLErrorAction stop -DBSQLQuery "SELECT DISTINCT * FROM TBDD_LURATECH_ERROR WHERE FILENAME_AND_FILEPATH = '$($Item.FullName)' AND FILEHASH = '$FileHash'")
#$DBSQLDataSet.Tables[0].rows | Out-GridView
#If there are rows in DB for this file.
IF ($($DBSQLDataSet.Tables[0].Rows.Count) -gt 0) {
Set-Variable -Name FileQuery -Value $DBSQLDataSet.Tables[0].rows[0][2].ToString()
$FileQuery = $FileQuery -as [Int]
Func-Write-Logfile -LogLine "File: $Item already exits in SQL Table TBDD_LURATECH_ERROR - current FileQuery counter is at: $FileQuery"
#If Limit for retrys is reached
IF (($FileQuery -eq $FileQuery_Limit) -or ($FileQuery -gt $FileQuery_Limit)) {
Func-Path-Check -Path $EscalationPath
#If file is available
IF ((Func-File-check-state -FilePath_and_FileName $Item.FullName -filechecktyp write) -eq 'writeable') {
#If File will be renamed by a set prefix
IF (($newfilename -gt $NULL) -and ($FileNameConstructor -eq "prefix")) {
Try {
Set-Variable -Name FileQuery -Value $DBSQLDataSet.Tables[0].rows[0][2].ToString()
$FileQuery = $FileQuery -as [Int]
$FileQuery++
Set-Variable -Name DBSQL_NewFilename -Value (Split-Path -Leaf (Func-File-copy-or-move -FilePath_and_FileName $Item.FullName -DestinationPath $EscalationPath -move_or_copy move -newfilename_prefix $newfilename))
Func-DB-SQL-Query -DBSQLQueryType write -DBSQLErrorAction stop -DBSQLQuery "UPDATE TBDD_LURATECH_ERROR SET FILEQUERY=$FileQuery,COMMENT='Die Verarbeitung der Datei, ist zu oft im Luratech Prozess fehlgeschlagen! Die Datei wurde umbenannt zu - $DBSQL_NewFilename - und in den Ordner - $EscalationPath - verschoben.',CHANGED_WHO='Script: $ScriptName - User: $env:username',CHANGED_WHEN='$Timestamp4' WHERE FILENAME_AND_FILEPATH = '$($Item.FullName)' AND FILEHASH = '$FileHash';"
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
} #end try
Catch {
Func-Write-Logfile -LogLine "ERROR: File: $Item could not be moved, or something went wrong by updating the DB."
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
break
} #end catch
} #end if
#If File will be renamed
ELSEIF (($newfilename -gt $NULL) -and ($FileNameConstructor -eq "complete")) {
IF ($newfilename -like "*_") {
#If exisits, remove last "_" from new filename
Set-Variable -Name newfilenamelength -Value (($newfilename.Length)-1)
Set-Variable -Name newfilename -Value ($newfilename.Remove($newfilenamelength,1))
} #end if
Try {
Set-Variable -Name FileQuery -Value $DBSQLDataSet.Tables[0].rows[0][2].ToString()
$FileQuery = $FileQuery -as [Int]
$FileQuery++
Set-Variable -Name DBSQL_NewFilename -Value (Split-Path -Leaf (Func-File-copy-or-move -FilePath_and_FileName $Item.FullName -DestinationPath $EscalationPath -move_or_copy move -newfilename $newfilename))
Func-DB-SQL-Query -DBSQLQueryType write -DBSQLErrorAction stop -DBSQLQuery "UPDATE TBDD_LURATECH_ERROR SET FILEQUERY=$FileQuery,COMMENT='Die Verarbeitung der Datei, ist zu oft im Luratech Prozess fehlgeschlagen! Die Datei wurde umbenannt zu - $DBSQL_NewFilename - und in den Ordner - $EscalationPath - verschoben.',CHANGED_WHO='Script: $ScriptName - User: $env:username',CHANGED_WHEN='$Timestamp4' WHERE FILENAME_AND_FILEPATH = '$($Item.FullName)' AND FILEHASH = '$FileHash';"
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
} #end try
Catch {
Func-Write-Logfile -LogLine "ERROR: File: $Item could not be moved, or something went wrong by updating the DB."
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
break
} #end catch
} #end elseif
#If File will be renamed by a set suffix
ELSEIF (($newfilename -gt $NULL) -and ($FileNameConstructor -eq "suffix")) {
IF ($newfilename -like "*_") {
#If exisits, remove last "_" from new filename
Set-Variable -Name newfilenamelength -Value (($newfilename.Length)-1)
Set-Variable -Name newfilename -Value ($newfilename.Remove($newfilenamelength,1))
} #end if
Try {
Set-Variable -Name FileQuery -Value $DBSQLDataSet.Tables[0].rows[0][2].ToString()
$FileQuery = $FileQuery -as [Int]
$FileQuery++
Set-Variable -Name DBSQL_NewFilename -Value (Split-Path -Leaf (Func-File-copy-or-move -FilePath_and_FileName $Item.FullName -DestinationPath $EscalationPath -move_or_copy move -newfilename_suffix $newfilename))
Func-DB-SQL-Query -DBSQLQueryType write -DBSQLErrorAction stop -DBSQLQuery "UPDATE TBDD_LURATECH_ERROR SET FILEQUERY=$FileQuery,COMMENT='Die Verarbeitung der Datei, ist zu oft im Luratech Prozess fehlgeschlagen! Die Datei wurde umbenannt zu - $DBSQL_NewFilename - und in den Ordner - $EscalationPath - verschoben.',CHANGED_WHO='Script: $ScriptName - User: $env:username',CHANGED_WHEN='$Timestamp4' WHERE FILENAME_AND_FILEPATH = '$($Item.FullName)' AND FILEHASH = '$FileHash';"
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
} #end try
Catch {
Func-Write-Logfile -LogLine "ERROR: File: $Item could not be moved, or something went wrong by updating the DB."
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
break
} #end catch
} #end elseif
ELSE {
Try {
Set-Variable -Name FileQuery -Value $DBSQLDataSet.Tables[0].rows[0][2].ToString()
$FileQuery = $FileQuery -as [Int]
$FileQuery++
Func-File-copy-or-move -FilePath_and_FileName $Item.FullName -DestinationPath $DestinationPath -move_or_copy move
Func-DB-SQL-Query -DBSQLQueryType write -DBSQLErrorAction stop -DBSQLQuery "UPDATE TBDD_LURATECH_ERROR SET FILEQUERY=$FileQuery,COMMENT='Die Verarbeitung der Datei, ist zu oft im Luratech Prozess fehlgeschlagen! Die Datei wurde in den Ordner - $EscalationPath - verschoben.',CHANGED_WHO='Script: $ScriptName - User: $env:username',CHANGED_WHEN='$Timestamp4' WHERE FILENAME_AND_FILEPATH = '$($Item.FullName)' AND FILEHASH = '$FileHash';"
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
} #end try
Catch {
Func-Write-Logfile -LogLine "ERROR: File: $Item could not be moved, or something went wrong by updating the DB."
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
break
} #end catch
} #end else
} #end if
ELSE {
Func-Write-Logfile "ERROR: File: $Item is not available for this Script."
} #end else
} #end if
#If Limit for retrys is not reached
ELSE {
Func-Path-Check -Path $DestinationPath
IF ((Func-File-check-state -FilePath_and_FileName $Item.FullName -filechecktyp write) -eq 'writeable') {
Try {
Set-Variable -Name FileQuery -Value $DBSQLDataSet.Tables[0].rows[0][2].ToString()
$FileQuery = $FileQuery -as [Int]
$FileQuery++
Func-File-copy-or-move -FilePath_and_FileName $Item.FullName -DestinationPath $DestinationPath -move_or_copy move
Func-DB-SQL-Query -DBSQLQueryType write -DBSQLErrorAction stop -DBSQLQuery "UPDATE TBDD_LURATECH_ERROR SET FILEQUERY=$FileQuery,COMMENT='Die Verarbeitung der Datei, ist wiederholt im Luratech Prozess fehlgeschlagen! Die Datei wurde in den Ordner - $DestinationPath - verschoben.',CHANGED_WHO='Script: $ScriptName - User: $env:username',CHANGED_WHEN='$Timestamp4' WHERE FILENAME_AND_FILEPATH = '$($Item.FullName)' AND FILEHASH = '$FileHash';"
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
} #end try
Catch {
Func-Write-Logfile -LogLine "ERROR: File: $Item could not be moved, or something went wrong by updating the DB."
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
break
}
} #end if
ELSE {
Func-Write-Logfile "ERROR: File: $Item is not available for this Script."
} #end else
} #end else
} #end if
#If there are no rows in DB for this file.
ELSE {
Try {
Func-File-copy-or-move -FilePath_and_FileName $Item.FullName -DestinationPath $DestinationPath -move_or_copy move
Func-DB-SQL-Query -DBSQLQueryType write -DBSQLErrorAction stop -DBSQLQuery "INSERT INTO TBDD_LURATECH_ERROR (FILENAME_AND_FILEPATH,FILEQUERY,FILEHASH,COMMENT,ADDED_WHO,ADDED_WHEN) VALUES ('$($Item.FullName)',1,'$FileHash','Die Verarbeitung der Datei, ist erstmals im Luratech Prozess fehlgeschlagen! Die Datei wurde in den Ordner - $DestinationPath - verschoben.','Script: $ScriptName - User: $env:username','$Timestamp4');"
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
} #end try
Catch {
Func-Write-Logfile -LogLine "ERROR: File: $Item could not be moved, or something went wrong by updating the DB."
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
break
} #end catch
} #end else
} #end foreach
Func-Write-Logfile -LogLine "==========================================================================================="
} #end foreach
} #end if
ELSE {
Func-Write-Logfile -LogLine "There is/are no Rule(s) to process."
} #end else
#-----------------------------------------------------------------------------------------------------#
########################################### finishing part ############################################
#-----------------------------------------------------------------------------------------------------#
#Delete old LogFiles, depending on Variable $LogFileKeepTime.
Func-Write-Logfile -LogLine ""
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
Func-Write-Logfile -LogLine "Checking LogFiles:"
Func-Write-Logfile -LogLine "-------------------------------------------------------------------------------------------"
IF ($LogFileKeepTime -gt 0) {
Func-Write-Logfile -LogLine "Log Files should be removed which are older than $LogFileKeepTime Day(s)."
Set-Variable -Name Items -Value (Func-File-Collector -SearchPath "$LogPath\*" -SearchWhiteList *.log | where {$_.Name -like "*$ScriptName*" -and $_.lastwritetime -lt $((Get-Date).AddDays(-$LogFileKeepTime)) -and -not $_.psiscontainer})
IF ($Items -eq $null) {
Func-Write-Logfile -LogLine "Deleting no old LogFiles."
} #end if
ELSE {
Func-Write-Logfile -LogLine "Deleting old LogFiles:"
FOREACH ($Item in $Items) {
Try {
Remove-Item -Path $Item -Force -Verbose
Func-Write-Logfile -LogLine "LogFile: $Item was removed."
} #end try
Catch {
Func-Write-Logfile -LogLine "LogFile: $Item cannot been removed."
Func-Write-Logfile -LogLine "Please check your privileges!"
} #end catch
} #end foreach
} # end else
} #end if
ELSE {
Func-Write-Logfile -LogLine "You disabled LogFile deleting in ConfigFile, they all will be kept."
} #end else
Func-Write-Logfile -LogLine ""
Func-Write-Logfile -LogLine "*******************************************************************************************"
Func-Write-Logfile -LogLine "Program Completed: $ScriptName on $env:computername from Account $env:USERDOMAIN\$env:USERNAME."
Func-Write-Logfile -LogLine "*******************************************************************************************"
#Delete Variables used by this Script, to clean up the RAM.
Remove-Variable -Name ScriptName
Remove-Variable -Name ScriptPath
Remove-Variable -name ConfigFile
Remove-Variable -name ConfigValues
Remove-Variable -Name Timestamp1
Remove-Variable -Name Timestamp2
Remove-Variable -Name Timestamp3
Remove-Variable -Name Timestamp4
Remove-Variable -Name feh_rule??
Remove-Variable -Name feh_rulelist
Remove-Variable -Name feh_rulevalue
Remove-Variable -Name feh_rulevalues
Remove-Variable -Name Counter
Remove-Variable -Name FileQuery_Limit
Remove-Variable -Name SourcePath
Remove-Variable -Name newfilename
Remove-Variable -Name statictext
Remove-Variable -Name filetyps
Remove-Variable -Name FileHash
Remove-Variable -Name DBSQLDataSet
Remove-Variable -Name FileName
Remove-Variable -Name FileName_noExt
Remove-Variable -Name FileQuery
Remove-Variable -Name filestream
Remove-Variable -Name EscalationPath
Remove-Variable -Name FileNameConstructor
Remove-Variable -Name newfilenamelength
Remove-Variable -Name DestinationPath
Remove-Variable -Name LogFileKeepTime
Remove-Variable -Name LogPath
Remove-Variable -Name LogPathError
Remove-Variable -Name LogPathValue
Remove-Variable -Name LogPathValues
Remove-Variable -Name DBSQLConnectDatabase
Remove-Variable -Name DBSQLConnectIntegratedSecurity
Remove-Variable -Name DBSQLConnectPassword
Remove-Variable -Name DBSQLConnectServer
Remove-Variable -Name DBSQLConnectUser
Remove-Variable -Name DBSQLQuery
Remove-Variable -Name DBSQLCommand
Remove-Variable -Name DBSQLAdapter
Remove-Variable -Name DBSQL_NewFilename
Remove-Variable -Name FileCheckResult
Remove-Variable -Name FilePath_and_FileName
Remove-Variable -Name VersionSeperator
Remove-Variable -Name SourcePathSplit
Remove-Variable -Name FileSeperator
Remove-Variable -name FileDelayAge
Remove-Variable -Name Item
Remove-Variable -Name Items
$error.clear()