1145 lines
110 KiB
PowerShell
1145 lines
110 KiB
PowerShell
# Join-ZUGFeRD_Invoice
|
||
# ----------------------------------------------------------------------------
|
||
# Dieses PowerShell Skript fügt PDF Dateien und XML Dateien zusammen.
|
||
# Das Ergebins ist eine fertige ZUGFeRD PDF-Datei.
|
||
#
|
||
# Please use the "_Settings.ini"-File for configurations.
|
||
# ----------------------------------------------------------------------------
|
||
# Copyright (c) 2022 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: 21.02.2022 / MK
|
||
# Version Date / Editor: 15.05.2022 / MK
|
||
# Version Number: 2.0.0.3
|
||
|
||
#Requires –Version 4.0
|
||
|
||
#-----------------------------------------------------------------------------------------------------#
|
||
######################################## check for arguments ##########################################
|
||
#-----------------------------------------------------------------------------------------------------#
|
||
|
||
|
||
#-----------------------------------------------------------------------------------------------------#
|
||
################################## 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" -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 Timestamp5 -Value $(Get-Date -Format 'yyyyMMdd_HHmmssffff')
|
||
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 FileTest1 -Value $NULL
|
||
Set-Variable -Scope Global -Name FileTest2 -Value $NULL
|
||
Set-Variable -Scope Global -Name FileTest3 -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","Test-Path-withLogging","Remove-Item-withLogging","GdPicture_NET_14")
|
||
Set-Variable -Scope Global -Name ModuleOverrideSourcePath -Value $NULL
|
||
Set-Variable -Scope Global -Name ModuleDefaultSourcePath -Value "P:\Skriptentwickung\released-and-published\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 FileTest -Value $NULL
|
||
Set-Variable -Scope Global -Name GdPictureRegisterKEY -Value $NULL
|
||
Set-Variable -Scope Global -Name PDFOutputFilename -Value $NULL
|
||
Set-Variable -Scope Global -Name PDFOutputFile -Value $NULL
|
||
Set-Variable -Scope Global -Name XMLTempPath -Value $Env:temp
|
||
Set-Variable -Scope Global -Name XMLTempFilename -Value "ZUGFeRD-invoice.xml"
|
||
|
||
# Add basic ZUGFeRD schema properties.
|
||
[String]$ZUGFeRDMetadata = '<rdf:Description xmlns:zf=\"urn:zugferd:pdfa:CrossIndustryDocument:invoice:2p0#\" rdf:about=\"\">\r\n'
|
||
[String]$ZUGFeRDMetadata += "<zf:ConformanceLevel>BASIC</zf:ConformanceLevel>\r\n"
|
||
[String]$ZUGFeRDMetadata += "<zf:DocumentFileName>zugferd-invoice.xml</zf:DocumentFileName>\r\n"
|
||
[String]$ZUGFeRDMetadata += "<zf:DocumentType>INVOICE</zf:DocumentType>\r\n"
|
||
[String]$ZUGFeRDMetadata += "<zf:Version>2p0</zf:Version>\r\n"
|
||
[String]$ZUGFeRDMetadata += "</rdf:Description>\r\n"
|
||
# Add PDF/A extension schema description for the ZUGFeRD schema.
|
||
# Here you should add the proper metadata according to the ZUGFeRD specification.
|
||
# Example: https:#www.pdflib.com/fileadmin/pdf-knowledge-base/zugferd/ZUGFeRD2_extension_schema.xmp
|
||
|
||
# Replace Substings in the final processed ZUGFeRD PDF File
|
||
[Array]$PDFOutputFilenameReplace1 = @("DigitalData-G-ARE","DigitalData-ZG-ARE")
|
||
[Array]$PDFOutputFilenameReplace2 = @("DigitalData-G","DigitalData-ZG")
|
||
[Array]$PDFOutputFilenameReplace3 = @("-G-ARE","-ZG-ARE")
|
||
[Array]$PDFOutputFilenameReplace4 = @(" "," ") #unused yet
|
||
[Array]$PDFOutputFilenameReplace5 = @(" "," ") #unused yet
|
||
|
||
#-----------------------------------------------------------------------------------------------------#
|
||
############################################ 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
|
||
<NONE>
|
||
|
||
.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
|
||
|
||
#-----------------------------------------------------------------------------------------------------#
|
||
########################################### 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 succesful 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]$VersionSeperator = $ConfigFileContent."VersionSeperator_$($ConfigFileContent.VersionSeperator[0])"
|
||
[STRING]$FileDelayAge = $ConfigFileContent."FileDelayAge_$($ConfigFileContent.FileDelayAge[0])"
|
||
[STRING]$GdPictureRegisterKEY = $ConfigFileContent."GdPictureRegisterKEY_$($ConfigFileContent.GdPictureRegisterKEY[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'"
|
||
Add-Member -InputObject $Profile -MemberType NoteProperty -Name Count -Value 1 -Force -ErrorAction Stop
|
||
Add-Member -InputObject $Profile -MemberType NoteProperty -Name ("Profile_1") -Value $NULL -Force
|
||
|
||
# $Profile.Profile_1 += @($ProfileType)
|
||
# Write-Logfile -LogLine "Argument called ProfileType is: $ProfileType"
|
||
#
|
||
# $Profile.Profile_1 += @($SourcePath)
|
||
# Write-Logfile -LogLine "Argument called SourcePath is: $SourcePath"
|
||
#
|
||
# $Profile.Profile_1 += @($FileType)
|
||
# Write-Logfile -LogLine "Argument called SourcePathFilter is: $FileType"
|
||
#
|
||
# $Profile.Profile_1 += @($ReclusiveSwitch)
|
||
# Write-Logfile -LogLine "Argument called SourcePathRecurse is: $ReclusiveSwitch"
|
||
#
|
||
# $Profile.Profile_1 += @($DeleteItemsOlderThen)
|
||
# Write-Logfile -LogLine "Argument called DeleteItemsOlderThen is: $DeleteItemsOlderThen"
|
||
#
|
||
# $Profile.Profile_1 += @($DeleteItemsFileSystemAttribute)
|
||
# Write-Logfile -LogLine "Argument called DeleteItemsFileSystemAttribute is: $DeleteItemsFileSystemAttribute"
|
||
|
||
} #end if
|
||
|
||
ELSE {
|
||
|
||
Write-Host "DEBUG Info: Running Script in 'Settings file mode'"
|
||
Write-Host " "
|
||
Add-Member -InputObject $Profile -MemberType NoteProperty -Name Count -Value 1 -Force -ErrorAction Stop
|
||
Add-Member -InputObject $Profile -MemberType NoteProperty -Name XMLInputPath -Value $ConfigFileContent."XMLInputPath_$($ConfigFileContent.XMLInputPath[0])" -Force -ErrorAction Stop
|
||
Add-Member -InputObject $Profile -MemberType NoteProperty -Name XMLArchivePath -Value $ConfigFileContent."XMLArchivePath_$($ConfigFileContent.XMLArchivePath[0])" -Force -ErrorAction Stop
|
||
Add-Member -InputObject $Profile -MemberType NoteProperty -Name XMLErrorPath -Value $ConfigFileContent."XMLErrorPath_$($ConfigFileContent.XMLErrorPath[0])" -Force -ErrorAction Stop
|
||
Add-Member -InputObject $Profile -MemberType NoteProperty -Name PDFInputPath -Value $ConfigFileContent."PDFInputPath_$($ConfigFileContent.PDFInputPath[0])" -Force -ErrorAction Stop
|
||
Add-Member -InputObject $Profile -MemberType NoteProperty -Name PDFOutputPath -Value $ConfigFileContent."PDFOutputPath_$($ConfigFileContent.PDFOutputPath[0])" -Force -ErrorAction Stop
|
||
|
||
} #end else
|
||
|
||
} #end try
|
||
|
||
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 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 "********************************************************************************"
|
||
|
||
Write-Logfile -LogLine " "
|
||
Write-Logfile -LogLine "Found $($Profile.Count) Profile(s) to process."
|
||
|
||
#Start main part, if there is at least one profile defined
|
||
IF ($Profile.Count -gt 0) {
|
||
|
||
#Loop for every Profile configured. Every Profile is one line in the ConfigFile.
|
||
DO {
|
||
|
||
$Profile.Counter++ | Out-Null
|
||
Write-Logfile -LogLine " "
|
||
Write-Logfile -LogLine "--------------------------------------------------------------------------------"
|
||
Write-Logfile -LogLine "This is Profile: $($Profile.Counter) of $($Profile.Count)"
|
||
Write-Logfile -LogLine "--------------------------------------------------------------------------------"
|
||
|
||
Write-Logfile -LogLine " "
|
||
Write-Logfile -LogLine "XMLInputPath: $($Profile.XMLInputPath)"
|
||
Write-Logfile -LogLine "XMLTempPath: $($XMLTempPath)"
|
||
Write-Logfile -LogLine "XMLArchivePath: $($Profile.XMLArchivePath)"
|
||
Write-Logfile -LogLine "XMLErrorPath: $($Profile.XMLErrorPath)"
|
||
Write-Logfile -LogLine "PDFInputPath: $($Profile.PDFInputPath)"
|
||
Write-Logfile -LogLine "PDFOutputPath: $($Profile.PDFOutputPath)"
|
||
|
||
IF ((Test-Path -Path $($Profile.XMLInputPath) -PathType Container) -and (Test-Path -Path $($Profile.XMLArchivePath) -PathType Container) -and (Test-Path -Path $($Profile.PDFInputPath) -PathType Container) -and (Test-Path -Path $($Profile.PDFOutputPath) -PathType Container)) {
|
||
|
||
Write-Logfile -LogLine " "
|
||
Write-Logfile -LogLine "All Path settings are set and existend, so we can go on."
|
||
|
||
IF ($GdPictureRegisterKEY -gt $null) {
|
||
|
||
Write-Logfile -LogLine "Configuration is complete."
|
||
|
||
Try {
|
||
|
||
$GdPictureImaging = [GdPicture14.GdPictureImaging]::new()
|
||
$GdPicturePDF = [GdPicture14.GdPicturePDF]::new()
|
||
$LicenseManager = [GdPicture14.LicenseManager]::new()
|
||
$LicenseManager.RegisterKEY($GdPictureRegisterKEY)
|
||
|
||
# Quellen:
|
||
# https://www.gdpicture.com/blog/generate-zugferd-facturx-electronic-invoices-with-gdpicture/
|
||
# https://www.gdpicture.com/forum/viewtopic.php?f=30&t=6436
|
||
|
||
} #end
|
||
|
||
Catch {
|
||
|
||
Remove-Variable -Name GdPictureImaging -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name GdPicturePDF -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name LicenseManager -Force -ErrorAction SilentlyContinue
|
||
|
||
Write-Logfile -LogLine " "
|
||
Write-Logfile -LogLine "Unable to set GdPicture Objects!"
|
||
Write-Logfile -LogLine "Check binding, please!"
|
||
Write-LogFile -LogLine "Error: $($global:Error[0].Exception)"
|
||
|
||
} #end catch
|
||
|
||
} #end if
|
||
#-------------------------------------------------------------------------------------------------
|
||
|
||
Try {
|
||
$XMLInputItems = Get-ChildItem -Path $($Profile.XMLInputPath) -Filter *.xml -Recurse:$false -Force -ErrorAction Stop | where-object {$_.CreationTime -le ((Get-Date).AddMinutes(-$FileDelayAge))}
|
||
}
|
||
Catch {
|
||
Write-Logfile -LogLine "Unable to get XML source files!"
|
||
Write-LogFile -LogLine "Error: $($global:Error[0].Exception)"
|
||
}
|
||
|
||
IF ($($XMLInputItems.Count) -gt 0) {
|
||
|
||
FOREACH ($XMLInputItem in $XMLInputItems) {
|
||
|
||
Write-Logfile -LogLine " "
|
||
Write-Logfile -LogLine "Processing $($XMLInputItem.Fullname)"
|
||
Write-Logfile -LogLine "Trying to find fitting PDF File... "
|
||
|
||
#Try to find fiting pdf source files, by seaching with xml filename as substring
|
||
Try {
|
||
$PDFInputItems = Get-ChildItem -Path $($Profile.PDFInputPath) -Filter *$($XMLInputItem.BaseName)*.pdf -Recurse:$false -Force -ErrorAction Stop | where-object {$_.CreationTime -le ((Get-Date).AddMinutes(-$FileDelayAge))} | Sort-Object LastAccessTime -Descending | Select-Object -First 1
|
||
}
|
||
Catch {
|
||
Write-Logfile -LogLine "Unable to get PDF File!"
|
||
Write-LogFile -LogLine "Error: $($global:Error[0].Exception)"
|
||
}
|
||
|
||
IF ($($PDFInputItems.Count) -eq 1) {
|
||
|
||
Write-Logfile -LogLine "Found $($PDFInputItems.Count) fitting PDF File(s)!"
|
||
|
||
[Object]$PDFInputFile = $PDFInputItems
|
||
|
||
#Build PDFOutputFile
|
||
$PDFOutputFilename = $($PDFInputFile.BaseName)
|
||
$PDFOutputFilename = $PDFOutputFilename.Replace($PDFOutputFilenameReplace1[0],$PDFOutputFilenameReplace1[1])
|
||
$PDFOutputFilename = $PDFOutputFilename.Replace($PDFOutputFilenameReplace2[0],$PDFOutputFilenameReplace2[1])
|
||
$PDFOutputFilename = $PDFOutputFilename.Replace($PDFOutputFilenameReplace3[0],$PDFOutputFilenameReplace3[1])
|
||
$PDFOutputFilename = $PDFOutputFilename.Replace($PDFOutputFilenameReplace4[0],$PDFOutputFilenameReplace4[1])
|
||
$PDFOutputFilename = $PDFOutputFilename.Replace($PDFOutputFilenameReplace5[0],$PDFOutputFilenameReplace5[1])
|
||
$PDFOutputFileExtension = $($PDFInputFile.Extension)
|
||
$PDFOutputFile = $($Profile.PDFOutputPath) + "\" + $PDFOutputFilename + $PDFOutputFileExtension
|
||
|
||
# Does file already exist in the target directory?
|
||
$FileTest = Test-Path $PDFOutputFile -PathType Leaf
|
||
|
||
IF ($FileTest -eq $True) {
|
||
|
||
Write-Logfile -LogLine "The File ($($PDFOutputFile)) already exists in the target directory, starting Version Check."
|
||
|
||
[Array]$FileNameSplit = ($PDFOutputFilename -split $VersionSeperator)
|
||
[Int]$FileVersion = $NULL
|
||
[Int]$FileVersion = $FileNameSplit[1]
|
||
|
||
# Has the new file already a Version tag?
|
||
IF ($FileVersion -eq 0) {
|
||
|
||
Write-Host "DEBUG Info: Sourcefile includes no VersionSeperator."
|
||
# To skip Version ~1.
|
||
$FileVersion++
|
||
|
||
} #end if
|
||
|
||
ELSE {
|
||
|
||
Write-Host "DEBUG Info: Sourcefile includes VersionSeperator."
|
||
$PDFOutputFilename = $FilenameSplit[0]
|
||
|
||
} #end else
|
||
|
||
DO {
|
||
|
||
Write-Host "DEBUG Info: Count file version:" $FileVersion; $FileVersion++
|
||
}
|
||
|
||
WHILE (($Filetest = Test-Path -Path "$($Profile.PDFOutputPath)\$PDFOutputFilename$VersionSeperator$FileVersion$PDFOutputFileExtension") -eq 'True')
|
||
|
||
$PDFOutputFile = "$($Profile.PDFOutputPath)\$PDFOutputFilename$VersionSeperator$FileVersion$PDFOutputFileExtension"
|
||
|
||
} #end if
|
||
|
||
Else {
|
||
|
||
Write-Logfile -LogLine "The File ($PDFOutputFile) doesnt exists in the target directory."
|
||
|
||
} #end else
|
||
|
||
#-------------------------------------------------------------------------------------------------
|
||
|
||
Try {
|
||
|
||
Write-Logfile -LogLine "Copy XML File to temp dir: $($XMLTempPath+"\"+$XMLTempFilename)"
|
||
Copy-Item -Path $($XMLInputItem.FullName) -Destination $($XMLTempPath+"\"+$XMLTempFilename) -Force -ErrorAction Stop
|
||
|
||
Try {
|
||
|
||
Write-Logfile -LogLine " "
|
||
Write-Logfile -LogLine "Beginning PDF creation..."
|
||
|
||
#To dont overwrite files...
|
||
$Profile.XMLArchivePath = Join-Path -Path $($Profile.XMLArchivePath) -ChildPath $Timestamp5
|
||
$Profile.XMLErrorPath = Join-Path -Path $($Profile.XMLErrorPath) -ChildPath $Timestamp5
|
||
New-Item -ItemType "Directory" -Path $($Profile.XMLArchivePath)
|
||
New-Item -ItemType "Directory" -Path $($Profile.XMLErrorPath)
|
||
|
||
$GdPicturePDF.LoadFromFile($($PDFInputFile.FullName), $false)
|
||
|
||
# Embedding xml part.
|
||
Write-Logfile -LogLine "Embedding xml part..."
|
||
$GdPicturePDF.EmbedFile($($XMLTempPath+"\"+$XMLTempFilename), "Rechnungsdaten im ZUGFeRD-XML-Format")
|
||
|
||
# Saving as PDF/A-3b.
|
||
Write-Logfile -LogLine "Saving as PDF/A-3b..."
|
||
$GdPicturePDF.ConvertToPDFA($PDFOutputFile, [GdPicture14.PdfConversionConformance]::PDF_A_3b, $true, $true)
|
||
$GdPicturePDF.CloseDocument()
|
||
|
||
$GdPicturePDF.LoadFromFile($PDFOutputFile, $true)
|
||
|
||
# Modifying the metadata.
|
||
Write-Logfile -LogLine "Modifying the metadata..."
|
||
[string]$CurrentMetadata = $GdPicturePDF.GetMetadata()
|
||
|
||
$CurrentMetadata = $currentMetadata.Insert($currentMetadata.IndexOf("<rdf:Description "), $ZUGFeRDMetadata)
|
||
$GdPicturePDF.SetMetadata($CurrentMetadata)
|
||
|
||
$GdPicturePDF.SaveToFile($PDFOutputFile)
|
||
Write-Logfile -LogLine "Done!"
|
||
|
||
Try {
|
||
Move-Item -Path $($XMLInputItem.FullName) -Destination $($Profile.XMLArchivePath) -Force -ErrorAction Stop
|
||
Remove-Item -Path $($XMLTempPath+"\"+$XMLTempFilename) -Force -ErrorAction SilentlyContinue
|
||
} #end try
|
||
|
||
Catch {
|
||
Write-Logfile -LogLine "Unable to move XML File to Archiv Folder!"
|
||
Write-LogFile -LogLine "Error: $($global:Error[0].Exception)"
|
||
} #end catch
|
||
|
||
} #end try
|
||
|
||
Catch {
|
||
Move-Item -Path $($XMLInputItem.FullName) -Destination $($Profile.XMLErrorPath) -Force
|
||
Write-Logfile -LogLine "Error while processing PDF File!"
|
||
Write-LogFile -LogLine "Error: $($global:Error[0].Exception)"
|
||
} #end catch
|
||
|
||
} #end try
|
||
|
||
Catch {
|
||
Write-Logfile -LogLine "Error while copying XML File to TEMP!"
|
||
Write-LogFile -LogLine "Error: $($global:Error[0].Exception)"
|
||
} #end catch
|
||
#-------------------------------------------------------------------------------------------------
|
||
|
||
} #end if
|
||
|
||
ElseIf ($($PDFInputItems.Count) -gt 1) {
|
||
|
||
Write-Logfile -LogLine "To many fitting PDF File(s)! $($PDFInputItems.Count)"
|
||
|
||
}
|
||
|
||
Else {
|
||
|
||
Write-Logfile -LogLine "Found no fitting PDF File(s)!"
|
||
|
||
} #end else
|
||
|
||
} #end foreach
|
||
|
||
} #end if
|
||
|
||
} #end if
|
||
|
||
} UNTIL ($($Profile.Counter) -eq $($Profile.Count))
|
||
|
||
} #end if
|
||
|
||
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 ############################################
|
||
#-----------------------------------------------------------------------------------------------------#
|
||
|
||
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 Timestamp5 -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 FileTest1 -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name FileTest2 -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name FileTest3 -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name PathTest -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name PathTest2 -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name PathTests -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 CurrentMetadata -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name FileNameSplit -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name FileTest -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name FileVersion -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name GDPictureImaging -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name GDPicturePDF -Force -ErrorAction SilentlyContinue
|
||
|
||
Remove-Variable -Name GdPictureRegisterKEY -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name LicenseManager -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name PDFInputfile -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name PDFInputItems -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name PDFOutputFile -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name PDFOutputFileExtension -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name PDFOutputFilename -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name PDFOutputFilenameReplace1 -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name PDFOutputFilenameReplace2 -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name PDFOutputFilenameReplace3 -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name PDFOutputFilenameReplace4 -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name PDFOutputFilenameReplace5 -Force -ErrorAction SilentlyContinue
|
||
|
||
Remove-Variable -Name XMLInputItem -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name XMLInputItems -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name XMLTempFilename -Force -ErrorAction SilentlyContinue
|
||
Remove-Variable -Name XMLTempPath -Force -ErrorAction SilentlyContinue
|
||
|
||
Remove-Variable -Name ZUGFeRDMetadata -Force -ErrorAction SilentlyContinue
|
||
|
||
$error.clear() |