# Export-DBDataToFile # ---------------------------------------------------------------------------- # This Script imports File contents to a database # # Returns: - # ---------------------------------------------------------------------------- # Copyright (c) 2024 by Digital Data GmbH # # Digital Data GmbH • Ludwig-Rinn-Strasse 16 • D-35452 Heuchelheim # Tel.: 0641/202360 • E-Mail: info-flow@digitaldata.works # ---------------------------------------------------------------------------- # Creation Date / Author: 30.11.2018 / MK # Version Date / Editor: 21.10.2024 / MK # Version Number: 2.2.0.1 #Requires –Version 4.0 #-----------------------------------------------------------------------------------------------------# ######################################## check for arguments ########################################## #-----------------------------------------------------------------------------------------------------# Param ( [Parameter(Mandatory=$False)] [ValidateNotNull()] [Int]$pProfileID ) #end Param #-----------------------------------------------------------------------------------------------------# ################################## add additional buildin assemblys ################################### #-----------------------------------------------------------------------------------------------------# #Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop #Add-Type -AssemblyName PresentationCore -ErrorAction Stop #Add-Type -AssemblyName PresentationFramework -ErrorAction Stop #-----------------------------------------------------------------------------------------------------# ############################################ set variables ############################################ #-----------------------------------------------------------------------------------------------------# Set-Variable -Scope Global -Name ScriptName -Value (($MyInvocation.MyCommand.Name) -split "\.")[0].ToString() Set-Variable -Scope Global -Name ScriptPath -Value (Split-Path ($MyInvocation.MyCommand.Path)) Set-Variable -Scope Global -Name ConfigFile -Value (Get-ChildItem -Path "$ScriptPath" -Recurse -Filter "$ScriptName`_Settings.ini" -File -Force).FullName Set-Variable -Scope Global -Name ConfigFileContent -Value $NULL Set-Variable -Scope Global -Name Timestamp1 -Value $(Get-Date -Format 'ddMMyyyy') Set-Variable -Scope Global -Name Timestamp2 -Value $(Get-Date -Format 'ddMMyyyy_HHmmss') Set-Variable -Scope Global -Name Timestamp3 -Value $(Get-Date -Format 'ddMMyyyy_HHmmssffff') Set-Variable -Scope Global -Name Timestamp4 -Value $(Get-Date -Format 'yyyyMMdd HH:mm:ss.fff') Set-Variable -Scope Global -Name Item -Value $NULL Set-Variable -Scope Global -Name Items -Value $NULL Set-Variable -Scope Global -Name VersionSeperator -Value "~" Set-Variable -Scope Global -Name Counter1 -Value 0 Set-Variable -Scope Global -Name LogFile -Value "$ScriptName`_$Timestamp2.log" Set-Variable -Scope Global -Name LogFileKeepTime -Value 60 Set-Variable -Scope Global -Name LogPath -Value $NULL Set-Variable -Scope Global -Name FileDelayAge -Value 5 Set-Variable -Scope Global -Name FileTest -Value $NULL Set-Variable -Scope Global -Name PathTest -Value $NULL Set-Variable -Scope Global -Name Module -Value $NULL Set-Variable -Scope Global -Name Modules -Value ("Read-ConfigFile2","Write-LogFile","Remove-Item-withLogging","MoveOrCopy-Item-withLogging","Start-SQLDB-Query-withLogging","Update-ReplacePlaceholder-withLogging", "Remove-SpecialCharacter-withLogging","Test-Path-withLogging", "Test-FileState-withLogging") Set-Variable -Scope Global -Name ModuleOverrideSourcePath -Value $NULL Set-Variable -Scope Global -Name ModuleDefaultSourcePath -Value "P:\Skriptentwickung\current\Modules" Set-Variable -Scope Global -Name ModuleHKLMRegistryPath -Value "HKLM:\SOFTWARE\Digital Data\Modules" Set-Variable -Scope Global -Name ModuleHKCURegistryPath -Value "HKCU:\SOFTWARE\Digital Data\Modules" Set-Variable -Scope Global -Name Profile -Value $NULL Set-Variable -Scope Global -Name DBType -Value $NULL Set-Variable -Scope Global -Name DBConnString -Value $NULL Set-Variable -Scope Global -Name SQLColumnPlaceholder -Value "\%column[0-9]{1,}\%" #-----------------------------------------------------------------------------------------------------# ############################################ set functions ############################################ #-----------------------------------------------------------------------------------------------------# Function Load-PowerShellModule { <# .SYNOPSIS Function will load external - additional - PowerShell Modules into current PSSession. .DESCRIPTION By working with Modules, this Function is necessary to load external Modul Functions into the current PowerShell Session. In a productive Enviroment it is recommanded to let this Function set the Registry Key in HKLM for the ModuleSourcePath. In develepment and Test Enviroment it is possible, to work with distributed Folders with different Modules. Therefor the Parameter "ModuleOverrideSourcePath" and the preset Variable "ModuleDefaultSourcePath" are made for. After a successful Import of a Module, Function will Return $True, otherwise a $False. .REQUIREMENT General PowerShell V3 .REQUIREMENT Assembly System.Windows.Forms, PresentationCore, PresentationFramework .REQUIREMENT Variables ModuleOverrideSourcePath, ModuleName, Path, Paths, PathTest, FileTest, Result .REQUIREMENT Variables preSet ScriptName, ScriptPath, ModuleDefaultSourcePath (optional) .REQUIREMENT Functions .VERSION 1.1.0.0 / 22.02.2017 .PARAMETER ModuleName Give the Module Name, you want to load into the current PSSession (without File-Extension). .PARAMETER ModuleOverrideSourcePath Optional Parameter. By giving the ModuleOverrideSourcePath, Function will not check other Paths for the Function you want to load. .PARAMETER ModuleFileExtensions Optional Parameter. Give the Module File-Extension (regular: "psm1" or "dll") without a dot ("."), this is just for the checking routine, not the Import itself.')] .PARAMETER Force Optional Parameter. By using the Force Parameter, Module will be unload and reload. .EXAMPLE Load-PowerShellModule -ModuleName Write-LogFile -ModuleFileExtensions psm1 .EXAMPLE Load-PowerShellModule -ModuleName Write-LogFile -Force .EXAMPLE Load-PowerShellModule -ModuleName Write-LogFile -ModuleFileExtensions psm1 -ModuleOverrideSourcePath D:\ScriptFiles\Modules #> Param ( [Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True,HelpMessage='Give the ModuleName, you want to load into the current PSSession (without File-Extension)')] [ValidateNotNullOrEmpty()] [String]$ModuleName, [Parameter(Position=1,Mandatory=$False,HelpMessage='Optional Parameter. By giving the ModuleOverrideSourcePath, Function will not check other Paths for the Function you want to load.')] [ValidateNotNullOrEmpty()] [String]$ModuleOverrideSourcePath, [Parameter(Position=2,Mandatory=$False,HelpMessage='Optional Parameter. Give the Module File-Extension (regular: "psm1" or "dll") without a dot ("."), this is just for the checking routine, not the Import itself.')] [ValidateSet("psm1","dll")] [Array]$ModuleFileExtensions = @("psm1","dll"), [Parameter(Position=3,Mandatory=$False,HelpMessage='Optional Parameter. By using the Force Parameter, Module will be unload and reload.')] [Switch]$Force ) #end param Process { #Clear Error Variable $error.clear() #Loop for every possible File Extension (eg. psm1 and dll) FOREACH ($ModuleFileExtension in $ModuleFileExtensions) { #If FileExtension was given, remove it! Because otherwise "Import-Module" Function will have trouble importing. $ModuleName = $ModuleName -Replace("\.[a-zA-Z0-9]{3,4}","") Write-Host "" Write-Host "DEBUG Info - Load-PowerShellModule: You want to load Module $ModuleName.$ModuleFileExtension" IF ((([String]::IsNullOrWhiteSpace($ScriptName) -ne $True) -and ([String]::IsNullOrEmpty($ScriptName) -ne $True)) -and (([String]::IsNullOrWhiteSpace($ScriptPath) -ne $True) -and ([String]::IsNullOrEmpty($ScriptPath) -ne $True))) { #Try this if $ModuleOverrideSourcePath was given by calling the function IF ((([String]::IsNullOrWhiteSpace($ModuleOverrideSourcePath)) -ne $True) -and (([String]::IsNullOrEmpty($ModuleOverrideSourcePath)) -ne $True)) { Write-Host "DEBUG Info - Load-PowerShellModule: Function has been called with 'ModuleOverrideSourcePath' Parameter input!" Write-Host "DEBUG Info - Load-PowerShellModule: Testing for existence: $ModuleOverrideSourcePath\$ModuleName.$ModuleFileExtension" $FileTest = Test-Path -Path "$ModuleOverrideSourcePath\$ModuleName.$ModuleFileExtension" -PathType Leaf IF ($FileTest -eq $True) { Write-Host "DEBUG Info - Load-PowerShellModule: $ModuleOverrideSourcePath and ModuleName seems to exist." Write-Host "DEBUG Info - Load-PowerShellModule: Trying to import Module: $ModuleName.$ModuleFileExtension" Try { $Result = Import-Module $ModuleOverrideSourcePath\$ModuleName -Verbose -DisableNameChecking -Scope Global -Force:$Force -PassThru -ErrorAction SilentlyContinue IF ("$Result" -eq "$ModuleName") { Write-Host "DEBUG Info - Load-PowerShellModule: Successfully loaded Module: $ModuleName.$ModuleFileExtension" Return $True } #end if ELSE { Write-Error "DEBUG Info - Load-PowerShellModule: Unsuccessfully loaded Module: $ModuleName.$ModuleFileExtension" Return $False } #end else } #end try Catch { Write-Host "DEBUG Info - Load-PowerShellModule: Error while importing the Module:" Write-Host "DEBUG Info - Load-PowerShellModule: $ModuleName" Write-Host $Error Return $False } #end catch } #end if ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: ModuleOverrideSourcePath and/or ModuleName seems not to exist." Write-Host "DEBUG Info - Load-PowerShellModule: Cannot load Module, please check your input!" Return $False } #end else } #end if #If $ModuleOverrideSourcePath was not given, try to find a matching folder ELSE { Write-Host "" Write-Host "DEBUG Info - Load-PowerShellModule: Function has been called without 'ModuleOverrideSourcePath' Parameter input!" Write-Host "DEBUG Info - Load-PowerShellModule: Trying to find Module Files on some Places of this Computer." #Set dynamic Array for locations Modul Path could be, even for some testing. #The first value of the array is just a dummy and will never be used -but keep it for the array value starting with 0! [System.Collections.ArrayList]$Paths = @() Write-Host "" $Paths.Add("$env:systemroot\") | Out-Null IF (([String]::IsNullOrEmpty($ModuleDefaultSourcePath)) -or ([String]::IsNullOrWhiteSpace($ModuleDefaultSourcePath))) { Write-Host "DEBUG Info - Load-PowerShellModule: ModuleDefaultSourcePath was not set! That could be a normal behavior in productive enviroment!" } #end if ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: Possible Path (1): $ModuleDefaultSourcePath" -ErrorAction SilentlyContinue $Paths.Add("$ModuleDefaultSourcePath") | Out-Null } #end else IF ([String]::IsNullOrEmpty($ScriptPath) -or ([String]::IsNullOrWhiteSpace($ScriptPath))) { Write-Host "DEBUG Info - Load-PowerShellModule: ScriptPath is invalid! That is terrifying! How could that be???" } #end if ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: Possible Path (2): $ScriptPath" -ErrorAction SilentlyContinue $Paths.Add("$ScriptPath") | Out-Null Write-Host "DEBUG Info - Load-PowerShellModule: Possible Path (3): $($ScriptPath+"\Module")" -ErrorAction SilentlyContinue $Paths.Add("$($ScriptPath+"\Module")") | Out-Null Write-Host "DEBUG Info - Load-PowerShellModule: Possible Path (4): $($ScriptPath+"\Modules")" -ErrorAction SilentlyContinue $Paths.Add("$($ScriptPath+"\Modules")") | Out-Null } #end else IF (([String]::IsNullOrEmpty((Get-Item $ScriptPath).Parent.FullName)) -or ([String]::IsNullOrWhiteSpace((Get-Item $ScriptPath).Parent.FullName))) { Write-Host "DEBUG Info - Load-PowerShellModule: ScriptPath has no Parent Folders!" } #end if ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: Possible Path (5): $((Get-Item $ScriptPath).Parent.FullName)" -ErrorAction SilentlyContinue $Paths.Add("$((Get-Item $ScriptPath).Parent.FullName)") | Out-Null Write-Host "DEBUG Info - Load-PowerShellModule: Possible Path (6): $(((Get-Item $ScriptPath).Parent.FullName)+"\Module")" -ErrorAction SilentlyContinue $Paths.Add("$(((Get-Item $ScriptPath).Parent.FullName)+"\Module")") | Out-Null Write-Host "DEBUG Info - Load-PowerShellModule: Possible Path (7): $(((Get-Item $ScriptPath).Parent.FullName)+"\Modules")" -ErrorAction SilentlyContinue $Paths.Add("$(((Get-Item $ScriptPath).Parent.FullName)+"\Modules")") | Out-Null } #end else IF (([String]::IsNullOrEmpty($((Get-ItemProperty -Path "$ModuleHKLMRegistryPath" -Name ModuleSourcePath -ErrorAction SilentlyContinue).ModuleSourcePath))) -or ([String]::IsNullOrWhiteSpace($((Get-ItemProperty -Path "$ModuleHKLMRegistryPath" -Name ModuleSourcePath -ErrorAction SilentlyContinue).ModuleSourcePath)))) { Write-Host "DEBUG Info - Load-PowerShellModule: ModuleSourcePath was not set to Windows Registry (HKLM)!" } #end if ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: Possible Path (8): $((Get-ItemProperty -Path "$ModuleHKLMRegistryPath" -Name ModuleSourcePath -ErrorAction SilentlyContinue).ModuleSourcePath)" -ErrorAction SilentlyContinue $Paths.Add("$((Get-ItemProperty -Path "$ModuleHKLMRegistryPath" -Name ModuleSourcePath -ErrorAction SilentlyContinue).ModuleSourcePath)") | Out-Null } #end else IF (([String]::IsNullOrEmpty($((Get-ItemProperty -Path "$ModuleHKCURegistryPath" -Name ModuleSourcePath -ErrorAction SilentlyContinue).ModuleSourcePath))) -or ([String]::IsNullOrWhiteSpace($((Get-ItemProperty -Path "$ModuleHKCURegistryPath" -Name ModuleSourcePath -ErrorAction SilentlyContinue).ModuleSourcePath)))) { Write-Host "DEBUG Info - Load-PowerShellModule: ModuleSourcePath was not set to Windows Registry (HKCU)!" } #end if ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: Possible Path (8): $((Get-ItemProperty -Path "$ModuleHKCURegistryPath" -Name ModuleSourcePath -ErrorAction SilentlyContinue).ModuleSourcePath)" -ErrorAction SilentlyContinue $Paths.Add("$((Get-ItemProperty -Path "$ModuleHKCURegistryPath" -Name ModuleSourcePath -ErrorAction SilentlyContinue).ModuleSourcePath)") | Out-Null } #end else [Int]$Counter = 0 [String]$ModuleSourcePath = $Null #Loop for multiple Pathtests - for each Path, where Module files could be DO { $Counter++ Write-Host "" Write-Host "DEBUG Info - Load-PowerShellModule: Testing mutiple Paths ( $Counter of"($($Paths.Count)-1)") for existence, now testing:" Write-Host "DEBUG Info - Load-PowerShellModule: $($Paths[$Counter])" IF ((([String]::IsNullOrWhiteSpace($($Paths[$Counter]))) -ne $True) -and (([String]::IsNullOrEmpty($($Paths[$Counter])) -ne $True))) { $PathTest = (Test-Path $($Paths[$Counter]) -ErrorAction SilentlyContinue) IF ($PathTest -eq $True) { Write-Host "DEBUG Info - Load-PowerShellModule: Yes, Path seems to exist." Write-Host "DEBUG Info - Load-PowerShellModule: Lets check, if there are any Module Files, in it." $FileTest = Get-ChildItem -Path $($Paths[$Counter]) -Filter *.$ModuleFileExtension IF ($($FileTest.count) -gt 0) { Write-Host "DEBUG Info - Load-PowerShellModule: Found $($FileTest.count) $ModuleFileExtension Module Files in Path!" Set-Variable -Name ModuleSourcePath -Value $($Paths[$Counter]) -Scope local } #end if ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: Found no $ModuleFileExtension Module Files in Path!" } #end else } #end if ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: No, Path seems not to exist." } #end else } #end if ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: Path seems to be invalid!" } #end else } #end do UNTIL ($Counter -ge ($($Paths.Count)-1) -or ($ModuleSourcePath -eq $($Paths[$Counter]))) IF ($ModuleSourcePath -eq $($Paths[$Counter])) { $FileTest = (Test-Path -Path $ModuleSourcePath\$ModuleName.$ModuleFileExtension -PathType Leaf -ErrorAction SilentlyContinue) IF ($FileTest -eq $True) { Write-Host "" Write-Host "DEBUG Info - Load-PowerShellModule: Trying to import Module: $ModuleName.$ModuleFileExtension" Try { $Result = Import-Module $ModuleSourcePath\$ModuleName -Verbose -DisableNameChecking -Scope Global -Force:$Force -PassThru -ErrorAction SilentlyContinue IF ("$Result" -eq "$ModuleName") { Write-Host "DEBUG Info - Load-PowerShellModule: Successfully loaded Module: $ModuleName.$ModuleFileExtension" Set-Variable -Name ModuleDefaultSourcePath -Value $ModuleSourcePath -Scope Global Return $True } #end if ELSE { Write-Error "DEBUG Info - Load-PowerShellModule: Unsuccessfully loaded Module: $ModuleName.$ModuleFileExtension" Return $False } #end else } #end try Catch { Write-Host "" Write-Host "DEBUG Info - Load-PowerShellModule: Loading Module: $ModuleName went wrong." Write-Host "DEBUG Info - Load-PowerShellModule: Exiting Script, because of this error!" Write-Host $Error Return $False exit } #end catch } #end if ELSE { Write-Host "" Write-Host "DEBUG Info - Load-PowerShellModule: Module does not exist!" Write-Host "DEBUG Info - Load-PowerShellModule: Skipping: $ModuleName.$ModuleFileExtension" } #end else } #end if ELSE { Write-Host "" Write-Host "DEBUG Info - Load-PowerShellModule: Cant locate Module Files automaticlly!" Write-Host "DEBUG Info - Load-PowerShellModule: Please select Folder in Dialog." #Prepare Folder Browser Dialog, to choose the Directory with the .psm1 Files. $FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog $FolderBrowserDialog.Rootfolder = "Desktop" $FolderBrowserDialog.Description = "Please, choose the Folder, where the Module ""$ModuleName.$ModuleFileExtension"" is stored." $FolderBrowserDialog.SelectedPath = "$ScriptPath" $FolderBrowserDialog.ShowNewFolderButton = $True DO { #Now show the Folder Browser, if neccessary in a loop $FolderBrowserDialogShow = $FolderBrowserDialog.ShowDialog() #By pressing the OK Button.. If ($FolderBrowserDialogShow -eq "OK") { #Save selected folder path in the variable $ModuleSourcePath = ($FolderBrowserDialog.SelectedPath) Write-Host "" Write-Host "DEBUG Info - Load-PowerShellModule: You choose: $ModuleSourcePath" Write-Host "DEBUG Info - Load-PowerShellModule: ...testing, if Module ""$ModuleName.$ModuleFileExtension"" can be found there." $FileTest = (Test-Path $ModuleSourcePath\$ModuleName.$ModuleFileExtension -PathType Leaf -ErrorAction SilentlyContinue) IF ($FileTest -eq $True) { Write-Host "DEBUG Info - Load-PowerShellModule: Module seems to exist, in the selected Folder." Write-Host "DEBUG Info - Load-PowerShellModule: Now trying to load Module: $ModuleName.$ModuleFileExtension" Try { $Result = Import-Module $ModuleSourcePath\$ModuleName -Verbose -DisableNameChecking -Scope Global -Force:$Force -PassThru -ErrorAction SilentlyContinue IF ("$Result" -eq "$ModuleName") { Set-Variable -Name ModuleDefaultSourcePath -Value $ModuleSourcePath -Scope Global $MessageBoxBody = "Module: $ModuleName.$ModuleFileExtension - successsfully loaded into current PSSession!" $MessageBoxTitle = "ScriptName: $ScriptName - Module/Section: Load-PowerShellModule" $MessageBoxButtonType = "OK" $MessageBoxIcon = "Information" $MessageBox = [Windows.Forms.MessageBox]::Show($MessageBoxBody,$MessageBoxTitle,$MessageBoxButtonType,$MessageBoxIcon) | Out-Null } #end if ELSE { $MessageBoxBody = "Module: $ModuleName.$ModuleFileExtension - cannot load into current PSSession!" $MessageBoxTitle = "ScriptName: $ScriptName - Module/Section: Load-PowerShellModule" $MessageBoxButtonType = "OK" $MessageBoxIcon = "Warning" $MessageBox = [Windows.Forms.MessageBox]::Show($MessageBoxBody,$MessageBoxTitle,$MessageBoxButtonType,$MessageBoxIcon) | Out-Null } #end else } #end try Catch { Write-Host "" Write-Host "DEBUG Info - Load-PowerShellModule: Loading Module: $ModuleName.$ModuleFileExtension went wrong." Write-Host "DEBUG Info - Load-PowerShellModule: Exiting Script, because of this error!" Write-Host $Error exit } #end catch } #end if ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: Module seems not to exist, in the selected Folder." Write-Host "DEBUG Info - Load-PowerShellModule: Please, select another one! ...this Time the right!" $MessageBoxBody = "Module seems not to exist, in the selected Folder! Please, select another one!" $MessageBoxTitle = "ScriptName: $ScriptName - Module/Section: Load-PowerShellModule" $MessageBoxButtonType = "OK" $MessageBoxIcon = "Warning" $MessageBox = [Windows.Forms.MessageBox]::Show($MessageBoxBody,$MessageBoxTitle,$MessageBoxButtonType,$MessageBoxIcon) | Out-Null } #end else } #end if #If you didnt pressed the OK Button.. Else { Write-Host "" Write-Host "DEBUG Info - Load-PowerShellModule: Operation cancelled by user." Write-Host "DEBUG Info - Load-PowerShellModule: Exiting Script, because of this!" exit } #end else } #end do #Variable "$?" is $True when last operation was ok UNTIL ((($FileTest -eq $True) -and ($? -eq $True)) -or ($FolderBrowserDialogShow -eq "Cancel")) IF (([String]::IsNullOrWhiteSpace($ModuleSourcePath) -ne $True) -and ([String]::IsNullOrEmpty($ModuleSourcePath) -ne $True)) { Write-Host "" Write-Host "DEBUG Info - Load-PowerShellModule: Should ModuleSourcePath written to Windows Registry?" $MessageBoxBody = "Would you like to save the ModulePath to the Windows Registry?" $MessageBoxTitle = "ScriptName: $ScriptName - Module/Section: Load-PowerShellModule" $MessageBoxButtonType = "YesNo" $MessageBoxIcon = "Question" $MessageBox = [Windows.Forms.MessageBox]::Show($MessageBoxBody,$MessageBoxTitle,$MessageBoxButtonType,$MessageBoxIcon) IF ($MessageBox -eq 'Yes') { $PathTest = (Test-Path -Path "$ModuleHKLMRegistryPath" -ErrorAction SilentlyContinue) $MessageBox = $NULL IF ($PathTest -eq $False) { Write-Host "DEBUG Info - Load-PowerShellModule: Registry Key seems not to exist." Write-Host "DEBUG Info - Load-PowerShellModule: Trying to write ModuleSourcepath to HKLM." Try { New-Item -Path "$ModuleHKLMRegistryPath" -Force -ErrorAction Stop | Out-Null New-ItemProperty -Path "$ModuleHKLMRegistryPath" -Name ModuleSourcePath -Value $ModuleSourcePath -ErrorAction Stop | Out-Null } #end try Catch { Write-Host $Error $MessageBoxBody = "Could not save ModuleSourcePath to Windows Registry! Check your access rights! To bypass this issue you can write the ModuleSourcePath to User Registry (HKCU). Would you?" $MessageBoxTitle = "ScriptName: $ScriptName - Module/Section: Load-PowerShellModule" $MessageBoxButtonType = "YesNo" $MessageBoxIcon = "Question" $MessageBox = [Windows.Forms.MessageBox]::Show($MessageBoxBody,$MessageBoxTitle,$MessageBoxButtonType,$MessageBoxIcon) } #end catch } #end if ELSEIF ($PathTest -eq $True) { Write-Host "DEBUG Info - Load-PowerShellModule: Registry Key seems to exist." Write-Host "DEBUG Info - Load-PowerShellModule: Trying to write ModuleSourcepath to HKLM." Try { Set-ItemProperty -Path "$ModuleHKLMRegistryPath" -Name ModuleSourcePath -Value $ModuleSourcePath -ErrorAction Stop | Out-Null } #end try Catch { Write-Host $Error $MessageBoxBody = "Could not save ModuleSourcePath to Windows Registry! Check your access rights! To Bypass Error: Write ModuleSource Path to User Registry (HKCU)?" $MessageBoxTitle = "ScriptName: $ScriptName - Module/Section: Load-PowerShellModule" $MessageBoxButtonType = "YesNo" $MessageBoxIcon = "Question" $MessageBox = [Windows.Forms.MessageBox]::Show($MessageBoxBody,$MessageBoxTitle,$MessageBoxButtonType,$MessageBoxIcon) } #end catch } #end elseif ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: Something went wrong, by getting the ModuleSourcePath!" Write-Host "DEBUG Info - Load-PowerShellModule: Exiting Script, because of this!" exit } #end else #Block for trying to write the ModuleSourcePath to the User Registry, if System Registry failed. IF ($MessageBox -eq 'Yes') { $PathTest = (Test-Path -Path "$ModuleHKCURegistryPath" -ErrorAction SilentlyContinue) $MessageBox = $NULL IF ($PathTest -eq $False) { Write-Host "DEBUG Info - Load-PowerShellModule: Registry Key seems not to exist." Write-Host "DEBUG Info - Load-PowerShellModule: Trying to write ModuleSourcepath to HKCU." Try { New-Item -Path "$ModuleHKCURegistryPath" -Force -ErrorAction Stop | Out-Null New-ItemProperty -Path "$ModuleHKCURegistryPath" -Name ModuleSourcePath -Value $ModuleSourcePath -ErrorAction Stop | Out-Null } #end try Catch { Write-Host $Error $MessageBoxBody = "Could not save ModuleSourcePath to Windows Registry! Not even to User Registry! Check your access rights! Exiting now.." $MessageBoxTitle = "ScriptName: $ScriptName - Module/Section: Load-PowerShellModule" $MessageBoxButtonType = "OK" $MessageBoxIcon = "Warning" $MessageBox = [Windows.Forms.MessageBox]::Show($MessageBoxBody,$MessageBoxTitle,$MessageBoxButtonType,$MessageBoxIcon) exit } #end catch } #end if ELSEIF ($PathTest -eq $True) { Write-Host "DEBUG Info - Load-PowerShellModule: Registry Key seems to exist." Write-Host "DEBUG Info - Load-PowerShellModule: Trying to write ModuleSourcepath to HKCU." Try { Set-ItemProperty -Path "$ModuleHKCURegistryPath" -Name ModuleSourcePath -Value $ModuleSourcePath -ErrorAction Stop | Out-Null } #end try Catch { Write-Host $Error $MessageBoxBody = "Could not save ModuleSourcePath to Windows Registry! Not even to User Registry! Check your access rights! Exiting now.." $MessageBoxTitle = "ScriptName: $ScriptName - Module/Section: Load-PowerShellModule" $MessageBoxButtonType = "OK" $MessageBoxIcon = "Warning" $MessageBox = [Windows.Forms.MessageBox]::Show($MessageBoxBody,$MessageBoxTitle,$MessageBoxButtonType,$MessageBoxIcon) exit } #end catch } #end elseif ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: Something went wrong, by getting the ModuleSourcePath!" Write-Host "DEBUG Info - Load-PowerShellModule: Exiting Script, because of this!" exit } #end else } #end if } #end if ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: You choose, not to save the ModuleSourcePath to the Windows Registry!" $MessageBoxBody = "You choose, not to save the ModuleSourcePath to the Windows Registry!" $MessageBoxTitle = "ScriptName: $ScriptName - Module/Section: Load-PowerShellModule" $MessageBoxButtonType = "OK" $MessageBoxIcon = "Warning" $MessageBox = [Windows.Forms.MessageBox]::Show($MessageBoxBody,$MessageBoxTitle,$MessageBoxButtonType,$MessageBoxIcon) } #end else } #end if ELSE { Write-Host "DEBUG Info - Load-PowerShellModule: Something went wrong, by getting the ModuleSourcePath!" Write-Host "DEBUG Info - Load-PowerShellModule: Exiting Script, because of this!" exit } #end else } #end else } #end else } #end if ELSE { Write-Host "" Write-Host "DEBUG Info - Load-PowerShellModule: Required Variables (ScriptName and ScriptPath) were not set!" Write-Host "DEBUG Info - Load-PowerShellModule: Module Import is unvailable without this Variables!" } #end else } #end foreach } #end process } #end function Function Import-FileContentToDB { <# .SYNOPSIS Private function to import data #> Param ( [Parameter(Position=0,Mandatory=$true,HelpMessage='Give the SQL Connection string')] [ValidateNotNullOrEmpty()] [STRING]$ConnString, [Parameter(Position=1,Mandatory=$true,HelpMessage='Give the SQL Query')] [ValidateNotNullOrEmpty()] [STRING]$SQLQuery, [Parameter(Position=2,Mandatory=$true,HelpMessage='Give the source array')] [ValidateCount(1,9999)] $SourceArray ) #end param Write-Host "" Write-Host "DEBUG Info - Starting Import-FileContentToDB" $SQLQuery = Update-ReplacePlaceholder-withLogging -StringWithPlaceHolder $SQLQuery [INT]$LoopCounter = 0 Write-Logfile -LogLine "Found $($SourceArray.count) values, that can replace column placeholder" Do { $LoopCounter++ | Out-Null $SQLQuery = $SQLQuery -replace("%column$($LoopCounter)%",$($SourceArray[$LoopCounter-1])) } Until ($LoopCounter -eq $($SourceArray.count)) Write-Logfile -LogLine " " Write-Logfile -LogLine "SQL Query before invalid character removal" Write-Logfile -LogLine $SQLQuery $SQLQuery = $SQLQuery.Replace(",,",",") $SQLQuery = $SQLQuery.Replace("' '","''") $SQLQuery = $SQLQuery.Replace("' ''","'") $SQLQuery = $SQLQuery.Replace("'' '","'") $SQLQuery = $SQLQuery.Replace("NULLNULLNULL","NULL") $SQLQuery = $SQLQuery.Replace("NULLNULL","NULL") $SQLQuery = $SQLQuery.Replace(",'',",",NULL,") $SQLQuery = $SQLQuery.Replace("'NULL'","NULL") $SQLQuery = $SQLQuery.Replace("'NULL'","NULL") $SQLQuery = $SQLQuery.Replace(",NULL,'',",",NULL,NULL,") $SQLQuery = $SQLQuery.Replace(",'',NULL,",",NULL,NULL,") Write-Logfile -LogLine "Running this SQL Query:" Write-Logfile -LogLine $SQLQuery IF ($DBType -eq "MSSQL") { $DBConnString = $DBConnString.Replace("\;",";") $DBConnString = $DBConnString.Replace('"','') Try { Invoke-Sqlcmd -ConnectionString $DBConnString -Query $SQLQuery -AbortOnError -ErrorAction Stop Write-Host "DEBUG Info - Ending Import-FileContentToDB" Return $True } Catch { Write-Logfile -LogLine $Error[0].Exeption Write-Host "DEBUG Info - Ending Import-FileContentToDB" Return $False } } ELSE { Write-Logfile -LogLine " " Write-Logfile -LogLine "DBType $DBType is currently not implemented!" Write-Logfile -LogLine "Skipping Profile because of that reason!" Write-Host "DEBUG Info - Ending Import-FileContentToDB" Return $False } #end if/else } #end function Function Update-FileContent { <# .SYNOPSIS Private function to modify file content #> Param ( [Parameter(Position=0,Mandatory=$true,HelpMessage='Give the full path of the source file')] [ValidateNotNullOrEmpty()] [STRING]$SourceFile, [Parameter(Position=1,Mandatory=$true,HelpMessage='Give the full path, of the target file')] [ValidateNotNullOrEmpty()] [STRING]$TargetFile, [Parameter(Position=2,Mandatory=$true,HelpMessage='Give the Encoding type')] [ValidateSet('Default','ascii','ansi','bigendianunicode','bigendianutf32','oem','unicode','utf7','utf8','utf8BOM','utf8NoBOM','utf32')] [STRING]$Encoding, [Parameter(Position=3,Mandatory=$true,HelpMessage='Give the string you want to replace')] [ValidateNotNullOrEmpty()] [STRING]$OldValue, [Parameter(Position=4,Mandatory=$true,HelpMessage='Give the string which replaces the OldValue')] [ValidateNotNullOrEmpty()] [STRING]$NewValue ) #end param Write-Host "" Write-Logfile -LogLine "FileContent Replace Command is commencing" Write-Host "DEBUG Info: Paramter1 = $SourceFile" Write-Host "DEBUG Info: Paramter2 = $TargetFile" Write-Host "DEBUG Info: Paramter3 = $Encoding" Write-Host "DEBUG Info: Paramter4 = $OldValue" Write-Host "DEBUG Info: Paramter5 = $NewValue" Try { $TargetFile = Update-ReplacePlaceholder-withLogging -StringWithPlaceHolder $TargetFile $SourceFileContent = (Get-Content -Path $SourceFile -Encoding $Encoding -Force -ErrorAction Stop) $SourceFileContent = $SourceFileContent.Replace($OldValue,$NewValue) Set-Content -Path $TargetFile -Value $SourceFileContent -Encoding $Encoding -Force -ErrorAction Stop } Catch { Write-Logfile -LogLine "Error updating File!" Write-Logfile -LogLine "Exiting because of this Issue!" Write-Logfile $Error[0].Exception break } } #end function #-----------------------------------------------------------------------------------------------------# ########################################### preparing part ############################################ #-----------------------------------------------------------------------------------------------------# #Clear Console Content Clear-Host #Load external Modules - use Force ( -Force) Parameter, to reload in every run FOREACH ($Module in $Modules) { $Result = Load-PowerShellModule -ModuleName $Module -Force IF ($Result -eq $False) { Write-Host "DEBUG Info: Module: $Module was not successful been loaded!" Write-Host "DEBUG Info: Please load the Module and try again, running this Function/Module!" Write-Host "DEBUG Info: Exiting, because of this Issue." Write-Host $Error EXIT } #end if } #end foreach #Read ConfigFile and load its content into a Object [OBJECT]$ConfigFileContent = Read-ConfigFile2 -ConfigFile $ConfigFile #Allocate Variable Values, depending on the read ConfigFile [ARRAY]$LogPaths = $ConfigFileContent."LogPath_$($ConfigFileContent.LogPath[0])" [STRING]$LogFileKeepTime = $ConfigFileContent."LogFileKeepTime_$($ConfigFileContent.LogFileKeepTime[0])" [STRING]$FileDelayAge = $ConfigFileContent."FileDelayAge_$($ConfigFileContent.FileDelayAge[0])" [STRING]$DBType = $ConfigFileContent."DBType_$($ConfigFileContent.DBType[0])" [STRING]$DBConnString = $ConfigFileContent."DBConnString_$($ConfigFileContent.DBConnString[0])" TRY { #Create Object for Profile, the most Propertys of it will be rewritten in the main loop [OBJECT]$Profile = New-Object PSObject -ErrorAction Stop Add-Member -InputObject $Profile -MemberType NoteProperty -Name Counter -Value 0 -Force -ErrorAction Stop #Check if Script was called with arguments If (($PSBoundParameters.values | Measure-Object | Select-Object -ExpandProperty Count) -gt 0) { Write-Host "DEBUG Info: Running Script in 'Argument call mode'" IF ((!$pProfileID) -and ($pProfileID -lt 1)) { Add-Member -InputObject $Profile -MemberType NoteProperty -Name Count -Value 1 -Force -ErrorAction Stop } ELSE { Add-Member -InputObject $Profile -MemberType NoteProperty -Name Count -Value "$($ConfigFileContent.Profile[0])" -ErrorAction Continue } #end if/else } ELSE { Write-Host "DEBUG Info: Running Script in 'Settings file mode'" Add-Member -InputObject $Profile -MemberType NoteProperty -Name Count -Value "$($ConfigFileContent.Profile[0])" -ErrorAction Continue } #end if/else #Create Object for Profile, the most Propertys of it will be rewritten in the main loop Try { Add-Member -InputObject $Profile -MemberType NoteProperty -Name ProfileType -Value $NULL -ErrorAction Stop Add-Member -InputObject $Profile -MemberType NoteProperty -Name ImportFormat -Value $NULL -ErrorAction Stop Add-Member -InputObject $Profile -MemberType NoteProperty -Name SourcePath -Value $NULL -ErrorAction Stop Add-Member -InputObject $Profile -MemberType NoteProperty -Name SourceFileEncoding -Value $NULL -ErrorAction Stop Add-Member -InputObject $Profile -MemberType NoteProperty -Name HeaderLine -Value $NULL -ErrorAction Stop Add-Member -InputObject $Profile -MemberType NoteProperty -Name SourceFileStartLine -Value $NULL -ErrorAction Stop Add-Member -InputObject $Profile -MemberType NoteProperty -Name SourceFileEndLine -Value $NULL -ErrorAction Stop Add-Member -InputObject $Profile -MemberType NoteProperty -Name ValueSeperator -Value $NULL -ErrorAction Stop Add-Member -InputObject $Profile -MemberType NoteProperty -Name ArchivPath -Value $NULL -ErrorAction Stop Add-Member -InputObject $Profile -MemberType NoteProperty -Name ErrorPath -Value $NULL -ErrorAction Stop [OBJECT]$PreparingFile = New-Object PSObject -ErrorAction Stop Add-Member -InputObject $PreparingFile -MemberType NoteProperty -Name Counter -Value 0 -ErrorAction Stop Add-Member -InputObject $PreparingFile -MemberType NoteProperty -Name Count -Value "$($ConfigFileContent.PreparingFile[0])" -ErrorAction Continue Add-Member -InputObject $PreparingFile -MemberType NoteProperty -Name Command -Value $NULL -ErrorAction Stop [OBJECT]$CheckSQL = New-Object PSObject -ErrorAction Stop Add-Member -InputObject $CheckSQL -MemberType NoteProperty -Name Counter -Value 0 -ErrorAction Stop Add-Member -InputObject $CheckSQL -MemberType NoteProperty -Name Count -Value "$($ConfigFileContent.CheckSQL[0])" -ErrorAction Continue Add-Member -InputObject $CheckSQL -MemberType NoteProperty -Name Query -Value $NULL -ErrorAction Stop Add-Member -InputObject $CheckSQL -MemberType NoteProperty -Name Result -Value $True -ErrorAction Stop [OBJECT]$InitialSQL = New-Object PSObject -ErrorAction Stop Add-Member -InputObject $InitialSQL -MemberType NoteProperty -Name Counter -Value 0 -ErrorAction Stop Add-Member -InputObject $InitialSQL -MemberType NoteProperty -Name Count -Value "$($ConfigFileContent.InitialSQL[0])" -ErrorAction Continue Add-Member -InputObject $InitialSQL -MemberType NoteProperty -Name Query -Value $NULL -ErrorAction Stop Add-Member -InputObject $InitialSQL -MemberType NoteProperty -Name Result -Value $True -ErrorAction Stop [OBJECT]$PreparingSQL = New-Object PSObject -ErrorAction Stop Add-Member -InputObject $PreparingSQL -MemberType NoteProperty -Name Counter -Value 0 -ErrorAction Stop Add-Member -InputObject $PreparingSQL -MemberType NoteProperty -Name Count -Value "$($ConfigFileContent.PreparingSQL[0])" -ErrorAction Continue Add-Member -InputObject $PreparingSQL -MemberType NoteProperty -Name Query -Value $NULL -ErrorAction Stop Add-Member -InputObject $PreparingSQL -MemberType NoteProperty -Name Result -Value $True -ErrorAction Stop [OBJECT]$MainLoopSQL = New-Object PSObject -ErrorAction Stop Add-Member -InputObject $MainLoopSQL -MemberType NoteProperty -Name Counter -Value 0 -ErrorAction Stop Add-Member -InputObject $MainLoopSQL -MemberType NoteProperty -Name Count -Value "$($ConfigFileContent.MainLoopSQL[0])" -ErrorAction Continue Add-Member -InputObject $MainLoopSQL -MemberType NoteProperty -Name Query -Value $NULL -ErrorAction Stop Add-Member -InputObject $MainLoopSQL -MemberType NoteProperty -Name Result -Value $True -ErrorAction Stop [OBJECT]$FinalSQL = New-Object PSObject -ErrorAction Stop Add-Member -InputObject $FinalSQL -MemberType NoteProperty -Name Counter -Value 0 -ErrorAction Stop Add-Member -InputObject $FinalSQL -MemberType NoteProperty -Name Count -Value "$($ConfigFileContent.FinalSQL[0])" -ErrorAction Continue Add-Member -InputObject $FinalSQL -MemberType NoteProperty -Name Query -Value $NULL -ErrorAction Stop Add-Member -InputObject $FinalSQL -MemberType NoteProperty -Name Result -Value $True -ErrorAction Stop } Catch { Write-Logfile -LogLine " " Write-Logfile -LogLine "Error creating Object for: Profile" Write-Logfile -LogLine "Property Value is missing or invalid!" Add-Member -InputObject $Profile -MemberType NoteProperty -Name Count -Value 0 -Force -ErrorAction Stop Add-Member -InputObject $PreparingFile -MemberType NoteProperty -Name Count -Value 0 -Force -ErrorAction Stop Add-Member -InputObject $CheckSQL -MemberType NoteProperty -Name Count -Value 0 -Force -ErrorAction Stop Add-Member -InputObject $InitialSQL -MemberType NoteProperty -Name Count -Value 0 -Force -ErrorAction Stop Add-Member -InputObject $PreparingSQL -MemberType NoteProperty -Name Count -Value 0 -Force -ErrorAction Stop Add-Member -InputObject $MainLoopSQL -MemberType NoteProperty -Name Count -Value 0 -Force -ErrorAction Stop Add-Member -InputObject $FinalSQL -MemberType NoteProperty -Name Count -Value 0 -Force -ErrorAction Stop } #end try/catch } CATCH { Write-Logfile -LogLine " " Write-Logfile -LogLine "Error creating Object for: Profile" Write-Logfile -LogLine "Property Value is missing or invalid!" #Removeing incomplete Object Remove-Variable -Name Profile -Force -ErrorAction SilentlyContinue } #end try/catch #-----------------------------------------------------------------------------------------------------# ############################################# main part ############################################### #-----------------------------------------------------------------------------------------------------# #Clear Error Variable $Error.Clear() | Out-Null #Clear Console Output Clear-Host Write-Logfile -LogLine " " Write-Logfile -LogLine "********************************************************************************" Write-Logfile -LogLine "Program Startup: $ScriptName on $env:COMPUTERNAME," Write-Logfile -LogLine "from Account $env:USERDOMAIN\$env:USERNAME." Write-Logfile -LogLine "********************************************************************************" #Start main part, if there is at least one profile defined IF ($Profile.Count -gt 0) { Write-Logfile -LogLine " " Write-Logfile -LogLine "Found $($Profile.Count) Profile(s) to process." #Loop for every Profile configured. Every Profile is one line in the ConfigFile. DO { $Profile.Counter++ | Out-Null $PreparingFile.Counter++ | Out-Null $CheckSQL.Counter++ | Out-Null $InitialSQL.Counter++ | Out-Null $PreparingSQL.Counter++ | Out-Null $MainLoopSQL.Counter++ | Out-Null $FinalSQL.Counter++ | Out-Null Write-Logfile -LogLine " " Write-Logfile -LogLine "--------------------------------------------------------------------------------" Write-Logfile -LogLine "This is Profile: $($Profile.Counter) of $($Profile.Count)" Write-Logfile -LogLine "--------------------------------------------------------------------------------" #If argument call is used... IF (($PSBoundParameters.values | Measure-Object | Select-Object -ExpandProperty Count) -gt 0) { If ($pProfileID -gt 0) { Write-Logfile -LogLine "Argument call says, run Profile: $pProfileID" If ($Profile.Counter -ne $pProfileID) { Write-LogFile "ProfileID mismatch!" continue } ELSE { Write-LogFile "ProfileID match!" }#end if/else } ELSE { Write-Logfile -LogLine "Cannot build Profile. Argument call is missing Parameter or/and Parameter Values!" Write-Logfile -LogLine "Exiting because of this Issue!" Write-Logfile -LogLine $Error[0].Exception exit } #end if/else } ELSE { #If argument call is not used... Write-Logfile -LogLine " " $Profile.ProfileType = ($ConfigFileContent."Profile_$($Profile.Counter)")[0] Write-Logfile -LogLine "Configured ProfileType is: $($Profile.ProfileType)" IF (($Profile.ProfileType -eq "CSVImport") -or ($Profile.ProfileType -eq "CSVBulkImport") -or ($Profile.ProfileType -eq "ExcelImport")) { Write-Host "DEBUG Info: ProfileType is valid." $Profile.ImportFormat = ($ConfigFileContent."Profile_$($Profile.Counter)")[1] Write-Logfile -LogLine "Configured ImportFormat is: $($Profile.ImportFormat)" $Profile.SourcePath = ($ConfigFileContent."Profile_$($Profile.Counter)")[2] Write-Logfile -LogLine "Configured SourcePath is: $($Profile.SourcePath)" $Profile.SourceFileEncoding = ($ConfigFileContent."Profile_$($Profile.Counter)")[3] Write-Logfile -LogLine "Configured SourceFileEncoding is: $($Profile.SourceFileEncoding)" $Profile.HeaderLine = ($ConfigFileContent."Profile_$($Profile.Counter)")[4] Write-Logfile -LogLine "Configured HeaderLine is: $($Profile.HeaderLine)" $Profile.SourceFileStartLine = ($ConfigFileContent."Profile_$($Profile.Counter)")[5] $Profile.SourceFileStartLine = ($Profile.SourceFileStartLine-1) Write-Logfile -LogLine "Configured SourceFileStartLine is: $($Profile.SourceFileStartLine)" $Profile.SourceFileEndLine = ($ConfigFileContent."Profile_$($Profile.Counter)")[6] $Profile.SourceFileEndLine = ($Profile.SourceFileEndLine-1) Write-Logfile -LogLine "Configured SourceFileEndLine is: $($Profile.SourceFileEndLine)" $Profile.ValueSeperator = ($ConfigFileContent."Profile_$($Profile.Counter)")[7] Write-Logfile -LogLine "Configured ValueSeperator is: $($Profile.ValueSeperator)" $Profile.ArchivPath = ($ConfigFileContent."Profile_$($Profile.Counter)")[8] $Profile.ArchivPath = Update-ReplacePlaceholder-withLogging -StringWithPlaceHolder $PROFILE.ArchivPath Write-Logfile -LogLine "Configured ArchivPath is: $($Profile.ArchivPath)" $Profile.ErrorPath = ($ConfigFileContent."Profile_$($Profile.Counter)")[9] $Profile.ErrorPath = Update-ReplacePlaceholder-withLogging -StringWithPlaceHolder $PROFILE.ErrorPath Write-Logfile -LogLine "Configured ErrorPath is: $($Profile.ErrorPath)" $PreparingFile.Command = ($ConfigFileContent."PreparingFile_$($PreparingFile.Counter)")[0] $CheckSQL.Query = ($ConfigFileContent."CheckSQL_$($CheckSQL.Counter)")[0] $InitialSQL.Query = ($ConfigFileContent."InitialSQL_$($InitialSQL.Counter)")[0] $PreparingSQL.Query = ($ConfigFileContent."PreparingSQL_$($PreparingSQL.Counter)")[0] $MainLoopSQL.Query = ($ConfigFileContent."MainLoopSQL_$($MainLoopSQL.Counter)")[0] $FinalSQL.Query = ($ConfigFileContent."FinalSQL_$($FinalSQL.Counter)")[0] $CheckSQL.Result = $True $InitialSQL.Result = $True $PreparingSQL.Result = $True $MainLoopSQL.Result = $True $FinalSQL.Result = $True #Check if every Mandatory Value is set. IF ($Profile.ImportFormat -and $Profile.SourcePath -and $Profile.SourceFileEndLine -and $Profile.ValueSeperator -and $Profile.ArchivPath -and $Profile.ErrorPath) { Write-Logfile -LogLine " " Write-Logfile -LogLine "All required Variables are set for this Profile." } ELSE { Write-Logfile -LogLine " " Write-Logfile -LogLine "Not all required Variables are set for this Profile." Write-Logfile -LogLine "Skipping Profile because of that reason!" continue } #end if/else TRY { $SourceFiles = (Get-ChildItem -Path $Profile.SourcePath -ErrorAction Stop | Where-Object { $_.lastwritetime -lt $((Get-Date).AddMinutes(-$FileDelayAge)) -and -not $_.psiscontainer}) Write-Logfile -LogLine " " Write-Logfile -LogLine "Found $($SourceFiles.count) File(s) in SourcePath." } CATCH { Write-Logfile -LogLine " " Write-Logfile -LogLine "Error getting File(s) in SourcePath." } #end try/catch IF ($($SourceFiles.count) -gt 0) { FOREACH ($SourceFile in $SourceFiles) { Write-Logfile -LogLine " " Write-Logfile -LogLine "================================================================================" Write-Logfile -LogLine "Processing file: $($SourceFile.FullName)" Write-Logfile -LogLine "================================================================================" Add-Member -InputObject $SourceFile -MemberType NoteProperty -Name Header -Value $NULL -ErrorAction Stop -Force Add-Member -InputObject $SourceFile -MemberType NoteProperty -Name Content -Value $NULL -ErrorAction Stop -Force Add-Member -InputObject $SourceFile -MemberType NoteProperty -Name ContentTemp -Value $NULL -ErrorAction Stop -Force Add-Member -InputObject $SourceFile -MemberType NoteProperty -Name ContentLine -Value $NULL -ErrorAction Stop -Force IF ((Test-FileState-withLogging -SourceFile $SourceFile -Action writetest) -eq "writeable" ) { #Converting foreach Object to a global object, otherwise called subfunctions cannot access properties [OBJECT]$Global:SourceFile = $SourceFile IF ((($Profile.ProfileType) -eq "ExcelImport") -or (($Profile.ProfileType) -eq "CSVImport")) { IF (($Profile.ProfileType) -eq "ExcelImport") { Try { Write-Logfile -LogLine " " Write-Logfile -LogLine "Excel Import Methode was choosen. Starting Excel now..." $ExcelPIDsBefore = @(Get-Process -ErrorAction Ignore Excel | Select-Object -ExpandProperty Id) $ExcelApp = New-Object -comobject Excel.Application } Catch { Write-Logfile -LogLine " " Write-Logfile -LogLine "Cannot initialize COM Object from Excel App" Write-Logfile -LogLine "Is Excel installed?" Write-Logfile -LogLine "Exiting, because of this Issue." break } #end try/catch TRY { $ExcelApp.Visible = $False $ExcelApp.DisplayAlerts = $False $ExcelWorkbook = $ExcelApp.Workbooks.Open($SourceFile) [OBJECT]$Global:Tempfile1 = "$env:TEMP\$($SourceFile.BaseName)~1_$Timestamp3.csv" [OBJECT]$Global:Tempfile2 = "$env:TEMP\$($SourceFile.BaseName)~2_$Timestamp3.csv" [OBJECT]$Global:Tempfile3 = "$env:TEMP\$($SourceFile.BaseName)~3_$Timestamp3.csv" #Cleanup forgotten files Remove-Item -Path "$env:TEMP\$($SourceFile.BaseName)*" -Recurse:$no -Force -ErrorAction SilentlyContinue #Export Excel content to CSV $($ExcelWorkbook.Worksheets | Where-Object {$_.Name -match $($Profile.ValueSeperator)}).SaveAs($Tempfile1, 6) Write-Logfile -LogLine "Closing Excel with HandleID: $($ExcelApp.Hwnd) now!" $ExcelApp.quit() $ExcelComPid = Compare-Object -PassThru $ExcelPIDsBefore (Get-Process -ErrorAction Ignore Excel).Id Stop-Process -Id $ExcelComPid -Force #If set Startline is to high If ((Get-Content $Tempfile1).Length -lt $($Profile.SourceFileStartLine)) { $Profile.SourceFileStartLine = (Get-Content $Tempfile1).Length } #end if #If set Endline is to high If ((Get-Content $Tempfile1).Length -lt $($Profile.SourceFileEndLine)) { $Profile.SourceFileEndLine = (Get-Content $Tempfile1).Length } #end if IF ($PreparingFile.Command) { Write-Logfile -LogLine " " Write-Logfile -LogLine "Configured PreparingFile.Command is: $($PreparingFile.Command)" If ($PreparingFile.Command -match "~|~") { [ARRAY]$CommandsArray = $PreparingFile.Command.split('~|~') } ELSE { [ARRAY]$CommandsArray = $PreparingFile.Command } #end if/else FOREACH ($Command in $CommandsArray) { $Command = $Command.TrimStart() $Command = $Command.TrimEnd() If ($Command -match "Replace") { Write-Logfile -LogLine "Replace Command is commencing" $Command = $Command.replace("Replace","").Replace("(","").Replace(")","") $CommandArray = $Command.split('~,~') Update-FileContent -SourceFile $Tempfile1 -TargetFile $Tempfile2 -Encoding $($Profile.SourceFileEncoding) -OldValue $($CommandArray[0].ToString()) -NewValue $($CommandArray[1].ToString()) Write-Host "DEBUG Info: File replace is done!" } #end if } #end foreach } ELSE { $Tempfile2 = $Tempfile1 } #end if/else [Array]$SourceFile.Header = (Get-Content -Path $Tempfile2 -ErrorAction Stop | Select-Object -Skip $($($Profile.HeaderLine)-1) -First 1) -split(",") Write-Logfile -LogLine "Got this Header, count $($SourceFile.Header.Count) columns:" Write-Logfile -LogLine "Columns: $($SourceFile.Header)" Write-Logfile -LogLine "Trying to get content of $SourceFile" Set-Content -Path $Tempfile3 -Encoding $Profile.SourceFileEncoding -Force -Value (Get-Content -Path $Tempfile2 -Encoding $Profile.SourceFileEncoding -ErrorAction Stop | Select-Object -Skip $Profile.SourceFileStartLine -First $Profile.SourceFileEndLine) $SourceFile.ContentTemp = Import-Csv -Path $Tempfile3 -Delimiter "," -Encoding $Profile.SourceFileEncoding -Header $SourceFile.Header -ErrorAction Stop $SourceFile.Content = [System.Collections.ArrayList]@() FOREACH ($SourceFileLine in $SourceFile.ContentTemp) { #Convert Object to needed Array $SourceFileLineArray = @() $SourceFileLine.PSObject.Properties | ForEach-Object { $SourceFileLineArrayTempItem = $_.Value $SourceFileLineArrayTempItem = $SourceFileLineArrayTempItem.replace("'","''") $SourceFileLineArrayTempItem = $SourceFileLineArrayTempItem.replace('"','') $SourceFileLineArray += $SourceFileLineArrayTempItem } #end foreach $SourceFileLine = $SourceFileLineArray $OrderedHashtable = [ordered]@{} [INT]$LoopCounter = 0 $SourceFileLine = $SourceFileLine -split $Profile.ValueSeperator FOREACH ($Header in $SourceFile.Header) { $Header = $Header.replace("'","''") $Header = $Header.replace('"','') $OrderedHashtable.Add($Header,$($SourceFileLine[$LoopCounter])) $LoopCounter++ | out-Null } #end foreach $SourceFile.Content.Add($OrderedHashtable) | out-Null } #end foreach Write-Host "DEBUG Info: File was read" Remove-Item -Path $Tempfile1 -Force -ErrorAction SilentlyContinue Remove-Item -Path $Tempfile2 -Force -ErrorAction SilentlyContinue Remove-Item -Path $Tempfile3 -Force -ErrorAction SilentlyContinue } CATCH { Write-Logfile $Error[0].Exception Write-Logfile -LogLine "Error reading File!" Write-Logfile -LogLine "Exiting because of this Issue!" Remove-Item -Path $Tempfile1 -Force -ErrorAction SilentlyContinue Remove-Item -Path $Tempfile2 -Force -ErrorAction SilentlyContinue Remove-Item -Path $Tempfile3 -Force -ErrorAction SilentlyContinue $ExcelApp.quit() $ExcelComPid = Compare-Object -PassThru $ExcelPIDsBefore (Get-Process -ErrorAction Ignore Excel).Id Stop-Process -Id $excelComPid -Force break } #end try/catch } #end if ElseIF (($Profile.ProfileType) -eq "CSVImport") { TRY { Write-Logfile -LogLine " " Write-Logfile -LogLine "CSV Import Methode was choosen." [OBJECT]$Global:Tempfile1 = "$env:TEMP\$($SourceFile.BaseName)~1_$Timestamp3.csv" #Cleanup forgotten files Remove-Item -Path "$env:TEMP\$($SourceFile.BaseName)*" -Recurse:$no -Force -ErrorAction SilentlyContinue #If set Startline is to high If ((Get-Content $SourceFile.FullName).Length -lt $($Profile.SourceFileStartLine)) { $Profile.SourceFileStartLine = (Get-Content $SourceFile.FullName).Length } #end if #If set Endline is to high If ((Get-Content $SourceFile.FullName).Length -lt $($Profile.SourceFileEndLine)) { $Profile.SourceFileEndLine = (Get-Content $SourceFile.FullName).Length } #end if IF ($PreparingFile.Command) { Write-Logfile -LogLine " " Write-Logfile -LogLine "Configured PreparingFile.Command is: $($PreparingFile.Command)" Copy-Item -Path $SourceFile.FullName -Destination $Tempfile1 -Force -ErrorAction Stop If ($PreparingFile.Command -match "~|~") { [ARRAY]$CommandsArray = $PreparingFile.Command.split('~|~') } ELSE { [ARRAY]$CommandsArray = $PreparingFile.Command } #end if/else FOREACH ($Command in $CommandsArray) { $Command = $Command.TrimStart() $Command = $Command.TrimEnd() If ($Command -match "Replace") { Write-Logfile -LogLine "Replace Command is commencing" $Command = $Command.replace("Replace","").Replace("(","").Replace(")","") $CommandArray = $Command.split('~,~') Update-FileContent -SourceFile $Tempfile1 -TargetFile $Tempfile1 -Encoding $($Profile.SourceFileEncoding) -OldValue $($CommandArray[0].ToString()) -NewValue $($CommandArray[1].ToString()) Write-Host "DEBUG Info: File replace is done!" } #end if } #end foreach } ELSE { $Tempfile1 = $SourceFile.FullName } #end if/else [Array]$SourceFile.Header = (Get-Content -Path $Tempfile1 -Encoding $Profile.SourceFileEncoding -ErrorAction Stop | Select-Object -Skip $($($Profile.HeaderLine)-1) -First 1) -split($Profile.ValueSeperator) | Where-Object -Property Length Write-Logfile -LogLine "Got this Header, count $($SourceFile.Header.Count) columns:" Write-Logfile -LogLine "Columns: $($SourceFile.Header)" Write-Logfile -LogLine "Trying to get content of $SourceFile" $SourceFile.ContentTemp = (Get-Content -Path $Tempfile1 -Encoding $Profile.SourceFileEncoding -ErrorAction Stop | Select-Object -Skip $Profile.SourceFileStartLine -First $Profile.SourceFileEndLine) | Where-Object -Property Length $SourceFile.Content = [System.Collections.ArrayList]@() FOREACH ($SourceFileLine in $SourceFile.ContentTemp) { $OrderedHashtable = [ordered]@{} [INT]$LoopCounter = 0 #If String starts with the Seperator If ($SourceFileLine.Substring(0,1) -match $Profile.ValueSeperator) { $SourceFileLine = $SourceFileLine.Substring(1,$($SourceFileLine.Length-1)) } #end if #If String ends with the Seperator If ($SourceFileLine.Substring($($SourceFileLine.Length-1),1) -match $Profile.ValueSeperator) { $SourceFileLine = $SourceFileLine.Substring(0,$(($SourceFileLine.Length)-1)) } #end if $SourceFileLine = $SourceFileLine -split $Profile.ValueSeperator $SourceFileLine = $SourceFileLine.replace("'","''") $SourceFileLine = $SourceFileLine.replace('"','') FOREACH ($Header in $SourceFile.Header) { $Header = $Header.replace("'","''") $Header = $Header.replace('"','') $OrderedHashtable.Add($Header,$($SourceFileLine[$LoopCounter])) $LoopCounter++ | out-Null } #end foreach $SourceFile.Content.Add($OrderedHashtable) | out-Null } #end foreach Write-Host "DEBUG Info: File was read" Remove-Item -Path $Tempfile1 -Force -ErrorAction SilentlyContinue } CATCH { Write-Logfile $Error[0].Exception Write-Logfile -LogLine "Error reading File!" Write-Logfile -LogLine "Exiting because of this Issue!" Remove-Item -Path $Tempfile1 -Force -ErrorAction SilentlyContinue break } #end try/catch } #end elseif Write-Logfile -LogLine "File content is now at the main memory!" Write-Logfile -LogLine "Proceeding with the SQL part..." #Check if every Mandatory Value is set. IF ($CheckSQL.Query) { Write-Logfile -LogLine " " Write-Logfile -LogLine "Configured CheckSQLQuery is: $($CheckSQL.Query)" Write-Logfile -LogLine "Processing $($SourceFile.content.count) line(s) from file" If ($CheckSQL.Query -match $SQLColumnPlaceholder) { FOREACH ($SourceFileLine in $SourceFile.content) { $CheckSQL.Result = Import-FileContentToDB -ConnString $DBConnString -SQLQuery $($CheckSQL.Query) -SourceArray $SourceFileLine If ($CheckSQL.Result -eq $true) { Write-Logfile -LogLine "CheckSQLResult = SQL erfolgreich!" } ELSE { Write-Logfile -LogLine "CheckSQLResult = SQL NICHT erfolgreich!" break } #end if/else } #end foreach } ELSE { $CheckSQL.Result = Import-FileContentToDB -ConnString $DBConnString -SQLQuery $($CheckSQL.Query) -SourceArray $SourceFileLine If ($CheckSQL.Result -eq $true) { Write-Logfile -LogLine "CheckSQLResult = SQL erfolgreich!" } ELSE { Write-Logfile -LogLine "CheckSQLResult = SQL NICHT erfolgreich!" break } #end if/else } #end if/else } ELSE { Write-Logfile -LogLine " " Write-Logfile -LogLine "Skipping CheckSQL!" } #end if/else #Check if every Mandatory Value is set. IF (($InitialSQL.Query) -and ($CheckSQL.Result -eq $True)) { Write-Logfile -LogLine " " Write-Logfile -LogLine "Configured InitialSQLQuery is: $($InitialSQL.Query)" Write-Logfile -LogLine "Processing $($SourceFile.content.count) line(s) from file" If ($InitialSQL.Query -match $SQLColumnPlaceholder) { FOREACH ($SourceFileLine in $SourceFile.content) { $InitialSQL.Result = Import-FileContentToDB -ConnString $DBConnString -SQLQuery $($InitialSQL.Query) -SourceArray $SourceFileLine If ($InitialSQL.Result -eq $true) { Write-Logfile -LogLine "InitialSQLResult = SQL erfolgreich!" } ELSE { Write-Logfile -LogLine "InitialSQLResult = SQL NICHT erfolgreich!" break } } #end foreach } ELSE { $InitialSQL.Result = Import-FileContentToDB -ConnString $DBConnString -SQLQuery $($InitialSQL.Query) -SourceArray $SourceFileLine If ($InitialSQL.Result -eq $true) { Write-Logfile -LogLine "InitialSQLResult = SQL erfolgreich!" } ELSE { Write-Logfile -LogLine "InitialSQLResult = SQL NICHT erfolgreich!" break } } #end if/else } ELSE { Write-Logfile -LogLine " " Write-Logfile -LogLine "Skipping InitialSQL!" } #end if/else #Check if every Mandatory Value is set. IF (($PreparingSQL.Query) -and ($InitialSQL.Result -eq $True)) { Write-Logfile -LogLine " " Write-Logfile -LogLine "Configured PreparingSQLQuery is: $($PreparingSQL.Query)" Write-Logfile -LogLine "Processing $($SourceFile.content.count) line(s) from file" If ($PreparingSQL.Query -match $SQLColumnPlaceholder) { FOREACH ($SourceFileLine in $SourceFile.content) { $PreparingSQL.Result = Import-FileContentToDB -ConnString $DBConnString -SQLQuery $($PreparingSQL.Query) -SourceArray $SourceFileLine If ($PreparingSQL.Result -eq $true) { Write-Logfile -LogLine "PreparingSQLResult = SQL erfolgreich!" } ELSE { Write-Logfile -LogLine "PreparingSQLResult = SQL NICHT erfolgreich!" break } } #end foreach } ELSE { $PreparingSQL.Result = Import-FileContentToDB -ConnString $DBConnString -SQLQuery $($PreparingSQL.Query) -SourceArray $SourceFileLine If ($PreparingSQL.Result -eq $true) { Write-Logfile -LogLine "PreparingSQLResult = SQL erfolgreich!" } ELSE { Write-Logfile -LogLine "PreparingSQLResult = SQL NICHT erfolgreich!" break } #end if/else } #end if/else } ELSE { Write-Logfile -LogLine " " Write-Logfile -LogLine "Skipping PreparingSQL!" } #end if/else #Check if every Mandatory Value is set. IF (($MainLoopSQL.Query) -and ($PreparingSQL.Result -eq $True)) { Write-Logfile -LogLine " " Write-Logfile -LogLine "Configured MainLoopSQLQuery is: $($MainLoopSQL.Query)" Write-Logfile -LogLine "Processing $($SourceFile.content.count) line(s) from file" If ($MainLoopSQL.Query -match $SQLColumnPlaceholder) { FOREACH ($SourceFileLine in $SourceFile.content) { $MainLoopSQL.Result = Import-FileContentToDB -ConnString $DBConnString -SQLQuery $($MainLoopSQL.Query) -SourceArray $SourceFileLine If ($MainLoopSQL.Result -eq $true) { Write-Logfile -LogLine "MainLoopSQLResult = SQL erfolgreich!" } ELSE { Write-Logfile -LogLine "MainLoopSQLResult = SQL NICHT erfolgreich!" } #end if/else } #end foreach } ELSE { $MainLoopSQL.Result = Import-FileContentToDB -ConnString $DBConnString -SQLQuery $($MainLoopSQL.Query) -SourceArray $SourceFileLine If ($MainLoopSQL.Result -eq $true) { Write-Logfile -LogLine "MainLoopSQLResult = SQL erfolgreich!" } ELSE { Write-Logfile -LogLine "MainLoopSQLResult = SQL NICHT erfolgreich!" } #end if/else } #end if/else } ELSE { Write-Logfile -LogLine " " Write-Logfile -LogLine "Skipping MainLoopSQL!" } #end if/else #Check if every Mandatory Value is set. IF (($FinalSQL.Query) -and ($MainLoopSQL.Result -eq $True)) { Write-Logfile -LogLine " " Write-Logfile -LogLine "Configured FinalSQLQuery is: $($FinalSQL.Query)" Write-Logfile -LogLine "Processing $($SourceFile.content.count) line(s) from file" If ($MainLoopSQL.Query -match $SQLColumnPlaceholder) { FOREACH ($SourceFileLine in $SourceFile.content) { $FinalSQL.Result = Import-FileContentToDB -ConnString $DBConnString -SQLQuery $($FinalSQL.Query) -SourceArray $SourceFileLine If ($FinalSQL.Result -eq $true) { Write-Logfile -LogLine "FinalSQLResult = SQL erfolgreich!" } ELSE { Write-Logfile -LogLine "FinalSQLResult = SQL NICHT erfolgreich!" break } #end if/else } #end foreach } ELSE { $FinalSQL.Result = Import-FileContentToDB -ConnString $DBConnString -SQLQuery $($FinalSQL.Query) -SourceArray $SourceFileLine If ($FinalSQL.Result -eq $true) { Write-Logfile -LogLine "FinalSQLResult = SQL erfolgreich!" } ELSE { Write-Logfile -LogLine "FinalSQLResult = SQL NICHT erfolgreich!" break } #end if/else } #end if/else } ELSE { Write-Logfile -LogLine " " Write-Logfile -LogLine "Skipping FinalSQL!" } #end if/else #Finally after the sql part If (($CheckSQL.Result -eq $True) -and ($InitialSQL.Result -eq $True) -and ($PreparingSQL.Result -eq $True) -and ($MainLoopSQL.Result -eq $True) -and ($FinalSQL.Result -eq $True)) { Write-Logfile -LogLine " " Write-Logfile -LogLine "SQL part is successfully completed" Write-Logfile -LogLine "Source file can be moved to archive folder...." Test-Path-withLogging -Path $Profile.ArchivPath MoveOrCopy-Item-withLogging -Action "move" -SourceFile $($SourceFile.FullName) -DestinationPath $($Profile.ArchivPath) } ELSE { Write-Logfile -LogLine $Error[0].Exception Write-Logfile -LogLine "SQL part is completed with error(s)" Write-Logfile -LogLine "Source file will be moved to the error dir" Test-Path-withLogging -Path $Profile.ErrorPath MoveOrCopy-Item-withLogging -Action "move" -SourceFile $($SourceFile.FullName) -DestinationPath $($Profile.ErrorPath) } #end if/else } ELSE { Write-Logfile -LogLine " " Write-Logfile -LogLine "No valid Import Methode was choosen." Write-Logfile -LogLine "Use 'CSVImport' or 'ExcelImport' in ConfigFile, please!" Write-Logfile -LogLine "Exiting, because of this Issue." break } #end if/else } ELSE { Write-LogFile -LogLine "File is in a not writeable state, skipping..." } #end if/else } #end foreach } ELSE { Write-LogFile -LogLine "Nothing to process here!" } #end if/else } #end if #Resetting Vars for next Profile run $CheckSQL.Query = $NULL $CheckSQL.Result = $NULL $InitialSQL.Query = $NULL $InitialSQL.Result = $NULL $PreparingSQL.Query = $NULL $PreparingSQL.Result = $NULL $FinalSQL.Query = $NULL $FinalSQL.Result = $NULL } #end else } #end do UNTIL ($($Profile.Counter) -eq $($Profile.Count)) } ELSE { Write-Logfile -LogLine " " Write-Logfile -LogLine "No Profiles are defined!" Write-Logfile -LogLine "Check ConfigFile, please." } #end else Write-Logfile -LogLine " " Write-Logfile -LogLine "--------------------------------------------------------------------------------" Write-Logfile -LogLine "Checking for old LogFiles." Write-Logfile -LogLine "--------------------------------------------------------------------------------" Remove-Item-withLogging -Path $LogPath -FileKeepTime $LogFileKeepTime -FileBaseName $ScriptName Write-Logfile -LogLine " " Write-Logfile -LogLine "********************************************************************************" Write-Logfile -LogLine "Program Completed: $ScriptName on $env:COMPUTERNAME," Write-Logfile -LogLine "from Account $env:USERDOMAIN\$env:USERNAME." Write-Logfile -LogLine "********************************************************************************" #-----------------------------------------------------------------------------------------------------# ########################################### finishing part ############################################ #-----------------------------------------------------------------------------------------------------# #Enable only for debugging #exit Remove-Variable -Name ScriptName -Force -ErrorAction SilentlyContinue Remove-Variable -Name ScriptPath -Force -ErrorAction SilentlyContinue Remove-Variable -Name ConfigFile -Force -ErrorAction SilentlyContinue Remove-Variable -Name ConfigFileContent -Force -ErrorAction SilentlyContinue Remove-Variable -Name Timestamp1 -Force -ErrorAction SilentlyContinue Remove-Variable -Name Timestamp2 -Force -ErrorAction SilentlyContinue Remove-Variable -Name Timestamp3 -Force -ErrorAction SilentlyContinue Remove-Variable -Name Timestamp4 -Force -ErrorAction SilentlyContinue Remove-Variable -Name Item -Force -ErrorAction SilentlyContinue Remove-Variable -Name Items -Force -ErrorAction SilentlyContinue Remove-Variable -Name VersionSeperator -Force -ErrorAction SilentlyContinue Remove-Variable -Name FileDelayAge -Force -ErrorAction SilentlyContinue Remove-Variable -Name FileTest -Force -ErrorAction SilentlyContinue Remove-Variable -Name PathTest -Force -ErrorAction SilentlyContinue Remove-Variable -Name Result -Force -ErrorAction SilentlyContinue Remove-Variable -Name Counter1 -Force -ErrorAction SilentlyContinue Remove-Variable -Name LogFile -Force -ErrorAction SilentlyContinue Remove-Variable -Name LogFileKeepTime -Force -ErrorAction SilentlyContinue Remove-Variable -Name LogPath -Force -ErrorAction SilentlyContinue Remove-Variable -Name LogPaths -Force -ErrorAction SilentlyContinue Remove-Variable -Name Module -Force -ErrorAction SilentlyContinue Remove-Variable -Name Modules -Force -ErrorAction SilentlyContinue Remove-Variable -Name ModuleOverrideSourcePath -Force -ErrorAction SilentlyContinue Remove-Variable -Name ModuleDefaultSourcePath -Force -ErrorAction SilentlyContinue Remove-Variable -Name ModuleHKLMRegistryPath -Force -ErrorAction SilentlyContinue Remove-Variable -Name ModuleHKCURegistryPath -Force -ErrorAction SilentlyContinue Remove-Variable -Name Profile -Force -ErrorAction SilentlyContinue Remove-Variable -Name DBType -Force -ErrorAction SilentlyContinue Remove-Variable -Name DBConnString -Force -ErrorAction SilentlyContinue Remove-Variable -Name SQLColumnPlaceholder -Force -ErrorAction SilentlyContinue Remove-Variable -Name PreparingFile -Force -ErrorAction SilentlyContinue Remove-Variable -Name CheckSQL -Force -ErrorAction SilentlyContinue Remove-Variable -Name InitialSQL -Force -ErrorAction SilentlyContinue Remove-Variable -Name PreparingSQL -Force -ErrorAction SilentlyContinue Remove-Variable -Name MainLoopSQL -Force -ErrorAction SilentlyContinue Remove-Variable -Name FinalSQL -Force -ErrorAction SilentlyContinue Remove-Variable -Name SourceFile -Force -ErrorAction SilentlyContinue Remove-Variable -Name SourceFiles -Force -ErrorAction SilentlyContinue Remove-Variable -Name Header -Force -ErrorAction SilentlyContinue Remove-Variable -Name Command -Force -ErrorAction SilentlyContinue Remove-Variable -Name CommandArray -Force -ErrorAction SilentlyContinue Remove-Variable -Name Matches -Force -ErrorAction SilentlyContinue Remove-Variable -Name LoopCounter -Force -ErrorAction SilentlyContinue Remove-Variable -Name OrderedHashtable -Force -ErrorAction SilentlyContinue Remove-Variable -Name SourceFileLine -Force -ErrorAction SilentlyContinue Remove-Variable -Name SqlServerConnectionTimeout -Force -ErrorAction SilentlyContinue Remove-Variable -Name SqlServerIncludeSystemObjects -Force -ErrorAction SilentlyContinue Remove-Variable -Name SqlServerMaximumChildItems -Force -ErrorAction SilentlyContinue Remove-Variable -Name SqlServerMaximumTabCompletion -Force -ErrorAction SilentlyContinue Remove-Variable -Name ExcelApp -Force -ErrorAction SilentlyContinue Remove-Variable -Name ExcelWorkbook -Force -ErrorAction SilentlyContinue Remove-Variable -Name ExcelPIDsBefore -Force -ErrorAction SilentlyContinue Remove-Variable -Name ExcelComPid -Force -ErrorAction SilentlyContinue $error.clear()