8
0
Skriptentwickung/current/DD-Core/Modules/Update-ReplacePlaceholder-withLogging.psm1

606 lines
41 KiB
PowerShell
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Function Update-ReplacePlaceholder-withLogging {
<#
.SYNOPSIS
Function will replace Placeholder in an input String.
.DESCRIPTION
This Function will replace specific or/and standard PlaceHolders in an input Strings.
Specific PlaceHolders must be defined at Function call.
Standard PlaceHolders are USERNAME, USERDOMAIN, COMPUTERNAME, LOGONSERVER, APPDATA, LOCALAPPDATA and DATE.
It also replaces global Variables as: SourceFile, SourceFilePath, SourceFileFullName, TargetFile,
TargetFilePath, TargetFileFullName, DestinationFile, DestinationFilePath, DestinationFileFullName.
And date/time Variables will also be replaced:
Timestamp1 = 'ddMMyyyy'
Timestamp2 = 'ddMMyyyy_HHmmss'
Timestamp3 = 'ddMMyyyy_HHmmssffff'
Timestamp4 = 'yyyyMMdd HHmmssfff'
Timestamp5 = 'yyyyMMdd_HHmmss'
Timestamp6 = 'dd.MM.yyyy'
Timestamp7 = 'dd.MM.yyyy_HH:mm:ss'
Timestamp8 = 'dd.MM.yyyy_HH:mm:ss.ffff'
Timestamp9 = 'yyyy/MM/dd HH:mm:ss.fff'
Timestamp10 = 'yyyy/MM/dd HH:mm:ss'
If the typical Scan Tag (%SCAN%&) was found, it will be removed not replaced.
.REQUIREMENT General
PowerShell V3
.REQUIREMENT Assembly
<NONE>
.REQUIREMENT Variables
StringWithPlaceHolder, FindPlaceHolder, ReplacePlaceHolder, TrimStartandEnd, RemoveSpecialCharacter, FunctionName, Counter1,
Timestamp1, Timestamp2, Timestamp3, Timestamp4, Timestamp5, Timestamp6, Timestamp7, Timestamp8, Timestamp9, Timestamp10
.REQUIREMENT Variables preSet
<NONE>
.REQUIREMENT Functions
Write-LogFile, Remove-SpecialCharacter-withLogging
.VERSION
Number: 1.2.0.0 / Date: 17.03.2019
.PARAMETER StringWithPlaceHolder
Give the String which contains Placeholders (One or more).
.PARAMETER FindPlaceHolder
Give the String which should be find and replaced.
.PARAMETER ReplacePlaceHolder
Give the String which should replace the found String before.
.PARAMETER TrimStartandEnd
Enable or disable, if the output String should be cleaned from blank at the start and End of it. Default Value is $True
.PARAMETER RemoveSpecialCharacter
Enable or disable, if the output String should be cleaned from SpecialCharacters. Default Value is $True
.EXAMPLE
Update-ReplacePlaceholder-withLogging -StringWithPlaceHolder "String with standard PlaceHolder: %username%"
.EXAMPLE
Update-ReplacePlaceholder-withLogging -StringWithPlaceHolder "String with specific PlaceHolder: %username%" -ReplacePlaceHolder -ReplacePlaceHolder
#>
[cmdletbinding()]
Param (
[Parameter(Position=0,Mandatory=$True,HelpMessage='Give the String which contains Placeholders (One or more).')]
[ValidateNotNullOrEmpty()]
[STRING]$StringWithPlaceHolder,
[Parameter(Position=1,Mandatory=$False,HelpMessage='Give the String which should be find and replaced.')]
[ValidateNotNullOrEmpty()]
[ARRAY]$FindPlaceHolder,
[Parameter(Position=2,Mandatory=$False,HelpMessage='Give the String which should replace the found String before.')]
[ValidateNotNullOrEmpty()]
[ARRAY]$ReplacePlaceHolder,
[Parameter(Position=3,Mandatory=$False,HelpMessage='Enable or disable, if the output String should be cleaned from blank at the start and End of it. Default Value is $True')]
[ValidateSet($True,$False)]
[BOOL]$TrimStartandEnd=$True,
[Parameter(Position=4,Mandatory=$False,HelpMessage='Enable or disable, if the output String should be cleaned from SpecialCharacters. Default Value is $True')]
[ValidateSet($True,$False)]
[BOOL]$RemoveSpecialCharacter=$True
) #end param
BEGIN {
#Clear Error Variable
$error.clear()
$FunctionName = $((($MyInvocation.MyCommand.Name) -split "\.")[0].ToString())
Write-Host "DEBUG Info - $($FunctionName): Begin Function."
#Checking if "Write-LogFile" Module was loaded
IF (Get-Module -All -Name "Write-LogFile") {
Write-Host "DEBUG Info - $($FunctionName): Write-LogFile - Module exists."
} #end if
ELSE {
Write-Host ""
Write-Host "DEBUG Info - $($FunctionName): Write-LogFile - Module does not exist!"
Write-Host "DEBUG Info - $($FunctionName): Please load the Module and try again, running this Function/Module!"
Write-Host "DEBUG Info - $($FunctionName): Exiting, because of this Issue."
EXIT
} #end else
#Checking if "Remove-SpecialCharacter-withLogging" Module was loaded
IF (Get-Module -All -Name "Remove-SpecialCharacter-withLogging") {
Write-Host "DEBUG Info - $($FunctionName): Remove-SpecialCharacter-withLogging - Module exists."
} #end if
ELSE {
Write-Host ""
Write-Host "DEBUG Info - $($FunctionName): Remove-SpecialCharacter-withLogging - Module does not exist!"
Write-Host "DEBUG Info - $($FunctionName): Please load the Module and try again, running this Function/Module!"
Write-Host "DEBUG Info - $($FunctionName): Exiting, because of this Issue."
EXIT
} #end else
} #end begin
PROCESS {
#Prepare Variables
[INT]$Counter1 = 0
[STRING]$Timestamp1 = $(Get-Date -Format 'ddMMyyyy')
[STRING]$Timestamp2 = $(Get-Date -Format 'ddMMyyyy_HHmmss')
[STRING]$Timestamp3 = $(Get-Date -Format 'ddMMyyyy_HHmmssffff')
[STRING]$Timestamp4 = $(Get-Date -Format 'yyyyMMdd HHmmssfff')
[STRING]$Timestamp5 = $(Get-Date -Format 'yyyyMMdd_HHmmss')
[STRING]$Timestamp6 = $(Get-Date -Format 'dd.MM.yyyy')
[STRING]$Timestamp7 = $(Get-Date -Format 'dd.MM.yyyy_HH:mm:ss')
[STRING]$Timestamp8 = $(Get-Date -Format 'dd.MM.yyyy_HH:mm:ss.ffff')
[STRING]$Timestamp9 = $(Get-Date -Format 'yyyy/MM/dd HH:mm:ss.fff')
[STRING]$Timestamp10 = $(Get-Date -Format 'yyyy/MM/dd HH:mm:ss')
Write-Logfile -LogLine " "
Write-Logfile -LogLine "Replace Function gets this String: $StringWithPlaceHolder"
IF ($StringWithPlaceHolder -match "%USERNAME%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %USERNAME%."
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%username%","$env:username")
Write-Host "DEBUG Info - $($FunctionName): Replaced it with $env:username"
} #end if
IF ($StringWithPlaceHolder -match "%USERDOMAIN%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %USERDOMAIN%."
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%USERDOMAIN%","$env:USERDOMAIN")
Write-Host "DEBUG Info - $($FunctionName): Replaced it with $env:USERDOMAIN"
} #end if
IF ($StringWithPlaceHolder -match "%COMPUTERNAME%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %COMPUTERNAME%."
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%COMPUTERNAME%","$env:COMPUTERNAME")
Write-Host "DEBUG Info - $($FunctionName): Replaced it with $env:COMPUTERNAME"
} #end if
IF ($StringWithPlaceHolder -match "%LOGONSERVER%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %LOGONSERVER%."
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%LOGONSERVER%","$env:LOGONSERVER")
Write-Host "DEBUG Info - $($FunctionName): Replaced it with $env:LOGONSERVER"
} #end if
IF ($StringWithPlaceHolder -match "%APPDATA%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %APPDATA%."
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%LOGONSERVER%","$env:APPDATA")
Write-Host "DEBUG Info - $($FunctionName): Replaced it with $env:APPDATA"
} #end if
IF ($StringWithPlaceHolder -match "%LOCALAPPDATA%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %LOCALAPPDATA%."
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%LOGONSERVER%","$env:LOCALAPPDATA")
Write-Host "DEBUG Info - $($FunctionName): Replaced it with $env:LOCALAPPDATA"
} #end if
IF ($StringWithPlaceHolder -match "%SCAN%&") {
Write-Host "DEBUG Info - $($FunctionName): Found: %SCAN%&."
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%SCAN%&","")
Write-Host "DEBUG Info - $($FunctionName): Replaced it with ... nothing"
} #end if
IF ($StringWithPlaceHolder -match "%date%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %date%."
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%date%",$Timestamp6)
Write-Host "DEBUG Info - $($FunctionName): Replaced it with $Timestamp6"
} #end if
IF ($StringWithPlaceHolder -match "\%Timestamp[0-9]{1,2}\%") {
Write-Host "DEBUG Info - $($FunctionName): Found: $($StringWithPlaceHolder | Select-String -pattern "%Timestamp[0-9]{1,}\%" -AllMatches | %{$_.matches} | %{$_.value})."
Write-Host "DEBUG Info - $($FunctionName): Will be replaced with $(Get-Variable -Name ($StringWithPlaceHolder | Select-String -pattern "%Timestamp[0-9]{1,}\%" -AllMatches | %{$_.matches} | %{$_.value}).replace('%','') -ValueOnly)"
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace($($StringWithPlaceHolder | Select-String -pattern "%Timestamp[0-9]{1,}\%" | %{$_.matches} | %{$_.value}),(Get-Variable -Name ($StringWithPlaceHolder | Select-String -pattern "%Timestamp[0-9]{1,}\%" | %{$_.matches} | %{$_.value}).replace('%','') -ValueOnly ))
} #end if
IF ($StringWithPlaceHolder -match "%SourceFile%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %SourceFile%"
IF ($(Get-Variable -name SourceFile -Scope Global -ValueOnly -ErrorAction SilentlyContinue).Name) {
TRY {
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%SourceFile%",$(Get-Variable -name SourceFile -Scope global -ValueOnly).Name)
Write-Host "DEBUG Info - $($FunctionName): Will be replaced with $($(Get-Variable -Name SourceFile -Scope Global -ValueOnly).Name)"
} #end try
CATCH {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %SourceFile%"
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %SourceFile%"
Write-Logfile -LogLine "Could not get Value for Placeholder %SourceFile%"
} #end else
} #end if
IF ($StringWithPlaceHolder -match "%SourceFilePath%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %SourceFilePath%"
IF ($(Get-Variable -name SourceFile -Scope Global -ValueOnly -ErrorAction SilentlyContinue).DirectoryName) {
TRY {
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%SourceFilePath%",$(Get-Variable -name SourceFile -Scope global -ValueOnly).DirectoryName)
Write-Host "DEBUG Info - $($FunctionName): Will be replaced with $($(Get-Variable -Name SourceFile -Scope Global -ValueOnly).DirectoryName)"
} #end try
CATCH {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %SourceFilePath%"
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %SourceFilePath%"
Write-Logfile -LogLine "Could not get Value for Placeholder %SourceFilePath%"
} #end else
} #end if
IF ($StringWithPlaceHolder -match "%SourceFileFullName%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %SourceFileFullName%"
IF ($(Get-Variable -name SourceFile -Scope Global -ValueOnly -ErrorAction SilentlyContinue).Fullname) {
TRY {
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%SourceFileFullName%",$(Get-Variable -name SourceFile -Scope global -ValueOnly).FullName)
Write-Host "DEBUG Info - $($FunctionName): Will be replaced with $($(Get-Variable -Name SourceFile -Scope Global -ValueOnly).FullName)"
} #end try
CATCH {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %SourceFileFullName%"
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %SourceFileFullName%"
Write-Logfile -LogLine "Could not get Value for Placeholder %SourceFileFullName%"
} #end else
} #end if
IF ($StringWithPlaceHolder -match "%TargetFile%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %TargetFile%"
IF ($(Get-Variable -name TargetFile -Scope Global -ValueOnly -ErrorAction SilentlyContinue).Name) {
TRY {
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%TargetFile%",$(Get-Variable -name TargetFile -Scope global -ValueOnly).Name)
Write-Host "DEBUG Info - $($FunctionName): Will be replaced with $($(Get-Variable -Name TargetFile -Scope Global -ValueOnly).Name)"
} #end try
CATCH {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %TargetFile%"
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %TargetFile%"
Write-Logfile -LogLine "Could not get Value for Placeholder %TargetFile%"
} #end else
} #end if
IF ($StringWithPlaceHolder -match "%TargetFilePath%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %TargetFilePath%"
IF ($(Get-Variable -name TargetFile -Scope Global -ValueOnly -ErrorAction SilentlyContinue).DirectoryName) {
TRY {
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%TargetFilePath%",$(Get-Variable -name TargetFile -Scope global -ValueOnly).DirectoryName)
Write-Host "DEBUG Info - $($FunctionName): Will be replaced with $($(Get-Variable -Name TargetFile -Scope Global -ValueOnly).DirectoryName)"
} #end try
CATCH {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %TargetFilePath%"
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %TargetFilePath%"
Write-Logfile -LogLine "Could not get Value for Placeholder %TargetFilePath%"
} #end else
} #end if
IF ($StringWithPlaceHolder -match "%TargetFileFullName%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %TargetFileFullName%"
IF ($(Get-Variable -name TargetFile -Scope Global -ValueOnly -ErrorAction SilentlyContinue).Fullname) {
TRY {
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%TargetFileFullName%",$(Get-Variable -name TargetFile -Scope global -ValueOnly).FullName)
Write-Host "DEBUG Info - $($FunctionName): Will be replaced with $($(Get-Variable -Name TargetFile -Scope Global -ValueOnly).FullName)"
} #end try
CATCH {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %TargetFileFullName%"
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %TargetFileFullName%"
Write-Logfile -LogLine "Could not get Value for Placeholder %TargetFileFullName%"
} #end else
} #end if
IF ($StringWithPlaceHolder -match "%DestinationFile%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %DestinationFile%"
IF ($(Get-Variable -name DestinationFile -Scope Global -ValueOnly -ErrorAction SilentlyContinue).Name) {
TRY {
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%DestinationFile%",$(Get-Variable -name DestinationFile -Scope global -ValueOnly).Name)
Write-Host "DEBUG Info - $($FunctionName): Will be replaced with $($(Get-Variable -Name DestinationFile -Scope Global -ValueOnly).Name)"
} #end try
CATCH {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %DestinationFile%"
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %DestinationFile%"
Write-Logfile -LogLine "Could not get Value for Placeholder %DestinationFile%"
} #end else
} #end if
IF ($StringWithPlaceHolder -match "%DestinationFilePath%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %DestinationFilePath%"
IF ($(Get-Variable -name DestinationFile -Scope Global -ValueOnly -ErrorAction SilentlyContinue).DirectoryName) {
TRY {
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%DestinationFilePath%",$(Get-Variable -name DestinationFile -Scope global -ValueOnly).DirectoryName)
Write-Host "DEBUG Info - $($FunctionName): Will be replaced with $($(Get-Variable -Name DestinationFile -Scope Global -ValueOnly).DirectoryName)"
$StringWithPlaceHolder = Remove-SpecialCharacter-withLogging -StringWithSpecialCharacter $StringWithPlaceHolder
} #end try
CATCH {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %DestinationFilePath%"
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %DestinationFilePath%"
Write-Logfile -LogLine "Could not get Value for Placeholder %DestinationFilePath%"
} #end else
} #end if
IF ($StringWithPlaceHolder -match "%DestinationFileFullName%") {
Write-Host "DEBUG Info - $($FunctionName): Found: %DestinationFileFullName%"
IF ($(Get-Variable -name DestinationFile -Scope Global -ValueOnly -ErrorAction SilentlyContinue).Fullname) {
TRY {
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace("%DestinationFileFullName%",$(Get-Variable -name DestinationFile -Scope global -ValueOnly).FullName)
Write-Host "DEBUG Info - $($FunctionName): Will be replaced with $($(Get-Variable -Name DestinationFile -Scope Global -ValueOnly).FullName)"
} #end try
CATCH {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %DestinationFileFullName%"
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine " "
Write-Logfile -LogLine "WARNING: Could not replace %DestinationFileFullName%"
Write-Logfile -LogLine "Could not get Value for Placeholder %DestinationFileFullName%"
} #end else
} #end if
IF (($FindPlaceHolder) -and ($ReplacePlaceHolder)) {
Write-Host "DEBUG Info - $($FunctionName): Count $($FindPlaceHolder.count) cycle(s)"
FOREACH ($PlaceHolder in $FindPlaceHolder) {
IF ($StringWithPlaceHolder -match $PlaceHolder) {
Write-Host "DEBUG Info - $($FunctionName): Found: $($PlaceHolder)."
$StringWithPlaceHolder = $StringWithPlaceHolder -Replace($PlaceHolder,$ReplacePlaceHolder[$Counter1])
Write-Host "DEBUG Info - $($FunctionName): Replaced it with $($ReplacePlaceHolder[$Counter1])"
} #end if
$Counter1++ | Out-Null
} #end foreach
} #end if
ELSE {
Write-Host "DEBUG Info - $($FunctionName): FindPlaceHolder/ReplacePlaceHolder incomplete"
} #end else
IF ($TrimStartandEnd -eq $True) {
Write-Logfile -LogLine "TrimStartandEnd Function is enabled and engaging..."
TRY {
$StringWithPlaceHolder = $StringWithPlaceHolder.TrimStart()
$StringWithPlaceHolder = $StringWithPlaceHolder.TrimEnd()
} #end try
CATCH {
Write-Logfile -LogLine "Error while removing Blanks at the Start and the End of the String."
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine "TrimStartandEnd Function is disabled."
} #end else
IF ($RemoveSpecialCharacter -eq $True) {
Write-Logfile -LogLine "RemoveSpecialCharacter Function is enabled and engaging..."
TRY {
$StringWithPlaceHolder = Remove-SpecialCharacter-withLogging -StringWithSpecialCharacter $StringWithPlaceHolder
} #end try
CATCH {
Write-Logfile -LogLine "Error while removing SpecialCharacters of the String."
} #end catch
} #end if
ELSE {
Write-Logfile -LogLine "RemoveSpecialCharacter Function is disabled."
} #end else
Write-Logfile -LogLine "Replace Function returns this String: $StringWithPlaceHolder"
RETURN [STRING]$StringWithPlaceHolder
} #end process
END {
Write-Host "DEBUG Info - $($FunctionName): End Function."
} #end end
} #end function